AS3: inline way to write get and set? - actionscript-3

I was reading a tutorial and the user there was starting setters and getters like so
function get f():Number;
function set f(value:Number):void;
inside an interface, then saving it in the main file inside a variable
var testNode:INode;
and referencing them as
testNode.f;
I thought this was really handy, specially for when you have a lot of variables. Instead of having to create two functions for each private value.
However, I tried to do the same without instancing the get and set inside an interface (because I had no need for this) and I get an error saying the function doesn't have a body.
Why is that? Is there any way to write get and set in such a clean, short manner? So far I've been writing these as
public function get someVar():SomeClass {
return _someVar;
}
public function set someVar(newValue:SomeClass):void {
_someVar = newValue;
}
but it's a bit of a hassle when I have several private variables.

An interface is just a model for your class, it does not allows you to skip the definition of a function (or a getter/setter). You must declare the getter and the setter in any class implementing your interface. What you are looking for is an extension (inheritage).
If you define a class Mother.as with a getter and a setter, the class Child.as which extends Mother.as doesn't need to redefine the getter and setter.
Furthermore, if you don't plan on writing anything else inside the getter and setter, you should use a public variable. These are less resource consuming than useless get/set.

Related

Why does this property/function name collision compile in AS3?

In ActionScript 3.0, this compiles:
public function set func(value:Function):void
{
}
public function func():void
{
}
This doesn't:
public function set someVar44(value:int):void
{
}
var someVar44:int;
Why does the first one compile? I suppose it's possible that Adobe just specifically and arbitrarily decided to block this for variables and to allow it for functions, but allowing it for either functions or variables doesn't seem to make any sense. I'm suspicious there's more to the story here. What am I not seeing?
This is really interesting, and took a fair amount of digging to get down to (although the answer seems painfully obvious).
As you know variables/properties cannot be declared in the same scope with identical names. Therefore the set function someVar44() and the variable someVar44 are in direct conflict (besides issues with trying to initialize the variable twice). Conversely if you had tried:
public function get func(value:Function):void
{
}
you would have ran into a similar issue with a duplicate function definition error. So why does the set function seem to allow you to get past these errors? As setters and getters are known for accessing and mutating properties of a class, it would seem they are also treated as class properties as opposed to a typical method, but this is not entirely the case. In fact, only the setter appears as a property of the public interface, the getter on the other hand is a method that can read like a property.
The setter:
public function set func(value:Function):void
Is read exactly like a property of the object, and without any other properties in direct conflict with it (i.e. - there is no current property like var func.) you do not receive a compiler error.
From adobe:
set Defines a setter, which is a method that appears in the public interface as a property.
get Defines a getter, which is a method that can be read like a property.
I believe that is why you are not getting a compiler error with set method. Although if you attempt to access that set method, the priority is immediately assumed to the function func(). That is, if you attempt this.func = function():void { } you will get the error:
Error #1037: Cannot assign to a method func
I can understand logically why the first compiles (in the older compiler). When considering an instance of the object, getting the obj.func property should return the member function you've defined, while setting the obj.func property should call the setter you've defined. This doesn't seem to be an ambiguity to me, though as we've seen, the runtime disagrees with me.
In the second case, you've defined a var (which defaults to the internal scope since you didn't say public, but that's another story) which, being externally visible, implicitly defines a getter and setter. So if someone sets the obj.someVar44 property of your object, are they calling the setter or setting your variable value? It's clearly an ambiguity and a duplicate definition.

Passing Model to View constructor for Stage Instances

I'm looking for an elegant solution to setting the Model argument for a View that is a stage instance.
My View constructor looks like this:
public function View($model:Model, $controller:IController=null){ ... }
My Subclass constructor:
public function ViewSubClass($model:Model, $controller:IController=null){ ... }
The idea is that the subclass will be a UI element that I'd rather just have on stage and not have to position it manually. I've thrown around the possibility of having placeholder elements that get removed and using their positions to attach the real UI elements but it seems a little hackish to me.
I suppose I could always set the default value of the $model argument in View to null, but not all views will be stage instances so I don't exactly want to do that either.
Any thoughts are appreciated.
Flash has no way to populate constructor parameters. This is why I always suggest avoiding constructor parameters for View. If this is your own Class, I'd suggest to go ahead and fix the parent Class so it doesn't need this.
If it is not, you can use
public function VewSubClass() {
super(null, null);
}
I would be concerned here that you actually need the Model and controller, so I would make setters available on the View subclass and then populate those variables once the instance arrives on stage.
public function set model(value:Model):void {
_model = value;
//do whatever the super constructor did based on receiving the model
}
public function set controller(value:IController):void {
_controller = value;
//etc.
}
Note that it probably shouldn't be necessary for the View to know about the controller, and I wouldn't suggest that the entire model be given to the View either--just the few properties it needs. So if the Class is yours, that puts you into a better position to correct these (IMO) architectural problems.
Based on the signature you've provided, I wouldn't be at all surprised to find that the model and controller internal storage are private, rather than protected, which means you're pretty much faced with a rewrite anyway (assuming the model and controller are needed on the View).
I solved the issue by changing the ViewSubClass constructor to:
public function ViewSubClass ($model:Model=null, $controller:IController=null){ ... }
Not sure why I was thinking that the constructor had to match the superclass' constructor perfectly. I'm still open to more elegant solutions if there are any. Thanks for looking.
-Veo

Limit argument type to package, possible?

In AS3, the below method accepts a parameter of any type:
public function myFunc(data:*) :void
Is it possible to limit the type to a specific package? Something like this maybe:
public function myFunc(data:(my.package:*)) //Accepts any type from my.package
It is possible, but will only have type control at runtime.
import flash.utils.getQualifiedClassName;
public function myFunc(data:*):void {
if (data is Object) {
var fqcn:String=getQualifiedClassName(data);
if (fqcn.slice(0,10)!='my.package') return; // otherwise work
// work here
} // simple types process if needed
}
This sounds like a design issue. One way to make this work during compile is if the parameter type is a custom class:
public function myFunc(data:MyCustomClass):void
Assuming that all the classes within my.package are varied, you could create a custom base class that extends Object and have all of your classes within my.package extend from this base class. Of course, If, however, the inheritance of your my.package classes is less broad you wouldn't need to reach so far. For example, you should only extend from DisplayObject if all the classes within my.package are of that type.
There may also be a way to accomplish what you want using namespaces, but I'm unsure.

trouble accessing non-static functions from static functions in AS3

I have a class containing, among other things, a drop down menu. With the aim of saving space, and since the contents of the menu will never change, I've made a static DataProvider for the whole class that populates each instances menu. I was hoping to populate the list with actual functions like so:
tmpArr.push({label:"Details...", funct:openDetailsMenu, args:""});
and then assign tmpArr to the DataProvider. Because the DataProvider is static the function that contains that code also needs to be static, but the functions in the array are non-static. At first it didn't seem like a problem, because when the user clicks on a menu item the drop down menu can call a non-static "executeFunction(funct, args)" on its parent. However, when I try to compile, the static function setting up the DataProvider it can't find the non-static functions being passed. If the compiler would just trust me the code would work fine!
The simple solution is to just pass strings and use a switch statement to call functions based on that, but that's big, ugly, inelegant, and difficult to maintain, especially if something inherits from this class.
The simpler solution is to just make the DataProvider non-static, but I'm wondering if anyone else has a good way of dealing with this? Making the static function able to see its non-static brethren?
Thanks.
OK, the basic reason for making things static is if you want to make it independant of an instance, for example the Math functions in as3 (you call Math.min() as opposed to var math = new Math(); math.min()...) this is useful for reference, repetetive calculation, simple actions (add 10 to x value) etc.
the problem with combining static and non static functionality is that when calling a static function, there is a possibility that the class has no instance at that point, or (in this case) that there is any reference to the function that would make sense in compilation (if a seperate class called the function, how would it reference openDetailsMenu?).
what you need to do is either go through getting function by name (object"functionname" works for example), make annonymous functions in your array or alternatively add a callback method to your static function something similar to this:
public static function doAction(object:Menu, event:String){
if(event == "details") object.openDetailsMenu() ;
}
all in all you are just adding layers of complexity that isnt really going to help. if you just add a class function and get them all to do the same action it is not taking more space or effort than if you are calling to a static function. you need to think about how and why the function is going to be used in (or out of) the class.
you could just store a static reference to the instance, in this case _instance. ( Kind of like a ghetto singleton ) just be careful not to call the static method before the class has been instantiated.
/// in your constructor define a static reference handle to the instance
public function ClassName(){
_instance = this;
}
public static function doSomethingStatic(){
var varValue = ClassName._instance.someInstanceVariable;
}

public and private modifiers for variables in access form module

I have a form module in my access project. In the top of the module, I declare a variable like so:
option explicit
private id_foo as long
I want to explicitely state that I need the variable in my form module by using the private access modifier on it.
Now, further down in the same form module, I have a function that needs to know and/or modify the value of id_foo:
function bar() as long
call do_something(me.id_foo)
end function
Yet, this doesn't work. But when I change the private modifier to a public modifer like
public id_foo as long
it does work.
This behaviour strikes me as odd or unintuitive, and, in fact, I can't see the meaning of public and private if I have to declare the variable as public anyway in order to use it in the same form module.
So, am I overlooking something obvious or is this how it is supposed to be?
Thanks / Rene
Try it without the "me" in front of id_foo:
function bar() as long
call do_something(id_foo)
end function
If you use the me keyword, you can see only public members, properties (also Form and VBA).
A form along with its module actually represents a class object. You can also create (instanciate) multiple Instances of that class object.
So, Any variable you declare as public becomes a public property of that class object. Note that any function in the forms code module declared as public becomes a public method of that class object. All of these properties and methods then show up in the intel-sense when you type in the "me." keyword.
If you declare the variable as private, then that variable (or function) will not be exposed as a public property (variable) or a public method (function) of the form.
So the simple solution your cases is to drop the use of the me keyword in your code, and it you code will run just fine at.
So declaring as public or private does have an effect here. In fact, “private” is the default.
So, public will expose the variable and/or functions as properties and methods of that form which is a class object (note that you can have Multiple instances of the same form loaded at the same time).
If you decleare things as private (the default, so you don’t have to do anything for the Variable or function) then you can still use the value in ANY code routine in that forms code module, but it will not be public exposed as a property/method and thus you can't use me.
Thus, your code will work fine if you remove the use of the me., and just go:
function bar() as long
call do_something(id_foo)
end function