Mr. Cluey
: How To ...? : Object DeclarationsNo, there isn't. QAP's compiler doesn't let you do that.
If that is all the answer there was, I wouldn't be writing this. There are some things that you can do to accomplish the same thing.
The keyword private restricts the scope of a variable to a single file. Only methods inside that file are allowed to access the variable directly.
This means that we can restrict a declaration to a single method by isolating both in a single file, and making the declaration private, like so:
private window AnyWin Bar
{
tag "Bar" ;
}
Foo ()
{
print ( Bar.WndTag) ;
}
Bar is declared private, so methods outside this file cannot access it. Foo, being in the same file, can access Bar freely. Essentially, by using the private keyword and a little bit of discipline, we can create a method that, in all respects, acts like it has a declaration within it.
Instances of window classes (both predefined classes and custom classes) can be created on the fly. The syntax isn't all that common, but it may look familiar.
window wTemp = MyWin("MyTag") ;
wTemp now points to a MyWin with the WndTag "MyTag". In the execution of your scripts, a window defined this way is equivalent to one declared explicitly. The string passed in becomes the tag associated with this window. The special tag markers are allowed, of course. It is also permitted to create a window with no tag at all.
window wTemp = MyWin() ;
The downside is that this isn't a real declaration - you can't add methods to it, you can't add properties. You can do to it only what the interface of the class allows. It will automatically inherit the default property values for that class.
My favorite method for working with classes is to use a constructor. Unfortunately, QAP doesn't support constructors in the usual sense. However, you can fake it.
The general idea is that we want to be able to create objects, setting up the various properties automatically. This part is pretty easy - just create a method that returns a window, and use create a temporary object of the appropriate class. Set the properties of the temporary object, and return it.
winclass CFoo { ... }
window Foo ( string sName )
{
window wTemp = CFoo() ;
wTemp.Name = sName ;
return( wTemp ) ;
}
main()
{
window MyFoo = Foo( "Mr Cluey" ) ;
...
}
Not a tremendous gain, but a start. Certainly it makes initializing the properties of the class neater. Of course, you can pass more than one variable to the constructor. QAP doesn't permit function overloading - you can fake this by declaring the function to accept either optional parameters or a variable argument list, then identifying the inputs in the constructor itself.
One thing I don't like about the implementation above is that it keeps refering to windows. It is only an accident of 4Test that these are windows. However, a user defined type adds another layer of abstraction.
winclass CFoo { ... }
type FOO is window ;
FOO Foo ( string sName )
{
FOO wTemp = CFoo() ;
wTemp.Name = sName ;
return( wTemp ) ;
}
main()
{
FOO MyFoo = Foo( "Mr Cluey" ) ;
...
}
Much better - now when real objects get added to the language, you will be able to eliminate the baggage associated with windows without having to dive into all of your sources eliminating window variables.
My naming preference is the one I have used in these last few examples
To make sure people use only the constructor, which may be desireable for any of a number of reasons, you can put the constructor, class declaration, and type all in a single file, specifying that the class declaration is private. Should you choose to do this, keep in mind that any classes which are intended to inherit from the private class must also be in the same module.
| Mr. Cluey : How To ...? : Object Declarations |