views:

453

answers:

4

Hi,

Is there a way to synthesize getters/setters in AS3? It's very common that you have a class with lots of variables, especially in math calculations (Model in MVC pattern), that you'd like to expose. Is there something like synthesize property in Objective-C, that allows to generate getters/setters?

Thanks,

Nava

+1  A: 

If you just make it a public property you don't have to worry about getters and setters:

public var str:String;

However, if you need anything along the lines of event dispatching to notify yourself of property changes, you'll need to code it yourself. Hopefully whatever IDE you're using may help by generating this kind of code for you, or you may need to set up some kind of code snippet the DIE can inject for you.

Avoid copy and paste if you can, you're bound to miss changing the name of something that will cause you issues down the road.

Rhysyngsun
I'm using eclipse. I'd like these properties to be private like:private var _str:String;and generate a public str getter and a setter. Do you know how do i do it in eclipse?Thanks, Nava
Nava Carmon
A: 

EDIT: If you want to use the IDE (eclipse) . You can go rightClick on class->Source->Generate Getters and Setters

It is not the same use as the synthesize, but if you want just to expose them and don't have to use the getMethod() notation, it can help you declaring the methods with the keywords get/set like this.

public function get example() : int
{
             return example;
}
public function set example( value : String ) : void
{
             example = value;
}

When you use it the time you access you can use the variables as if they were public and the methods will be called instead of direct access

Diego Dias
Thank you, I know how to work with getters/setters, my question was whether there is some attribute, that just says: generate them for me.It's a lot of dirty work and code overloading to write manually getters/setters for some 20 variables. You come to 40 functions with very high possibility to copy/paste errors. I thought there is a more elegant way for doing this
Nava Carmon
If you want to use the IDE (eclipse) . You can go rightClick on class->Source->Generate Getters and Setters
Diego Dias
GREAT, thanks, that's exactly what I was looking for!
Nava Carmon
+1  A: 

You can try regex. Take a backup before attempting this.

For example, if your private variables are named _str and you want it's public getter/setter to be named str, you can use the following patterns. Hit Ctrl-F in flex builder (or eclipse), tick the regex check box, and add the following patterns to the search and replace input fields respectively. Now hit 'Find' to find the property declaration and hit 'Replace' to generate setter and getter.

^((\t)+)private\s+var\s+_(\w+):(\w+)\s*;\s*(\n)

$0$5$1public function set $3(value:$4):void$5$1{$5$1$2_$3 = value;$5$1}$5$5$1public function get $3():$4$5$1{$5$1$2return _$3;$5$1}$5$5

This pattern was tested on

        private var _str:String;//indented by two tabs

And it successfully generated:

 private var _str:String;

 public function set str(value:String):void
 {
  _str = value;
 }
 public function get str():String
 {
  return _str;
 }
Amarghosh
upvoted your answer, since it's really useful and neat.
Nava Carmon
+2  A: 

For those that are using the Flex Builder 3 IDE, there are plug-ins for creating and placing code snippets, e.g. here: http://www.insideria.com/2008/04/flex-builder-enhancements-snippets-and-todo.html (there are some other small but useful tools compiled on that site)

These can be used for a more or less comfortable getter/setter generation.

However I only create explicit getters and setters if I need to place some extra code there. There are some who say it breaks encapsulation if you don't use getters and setters. However, with a language where you can add getters and setters later without changing the interface, I don't think that this is true anymore. To the user, it is completely transparent if he is using a plain variable assign or a function set varName(arg:object):void.

And also bear in mind, that if you make a variable [Bindable] (i.e. without an explicit event name), the compiler creates getters and setters for your variable wihtout you even noticing it (except there are already getters and setters of course). This is BTW a quick&dirty way to implement Interfaces that declare getter and setter functions

Jörg Reichardt
That's nice. Too pity it's impossible to choose a couple of answers as an answer, but I can always upvote :)
Nava Carmon
That is always appreciated :)
Jörg Reichardt
I agree with this - if all you are doing is creating a getter/setter wrapper without any special behaviour then there is no difference to just having a public property directly to the variable.
Sly_cardinal