getters and setters - meta programming ? CPP ?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

getters and setters - meta programming ? CPP ?

MarcWeber
The composition pattern seems to work.

Now I'd like to add a class resizing childs but also adding some margin
(top,left,right,bottom)


// add some border
class Margin extends PP {

  private var pps:List<PP>;

  // property left {{{1
  private var pleft: Float;
  public var left(getLeft, setLeft) : Float;
  private function getLeft(): Float
  {
    return pleft;
  }
  private function setLeft(value : Float): Float
  {
    if (pleft == value) return pleft;
    pleft = value;
    setPosChilds();
    return value;
  }
  // }}}
 

I think this kind of getter and setter is used very much. Am I right?

- setting a value does nothing if a value doesn't change.
- it returns the value
- it calls some action (setPosChilds)
- there is a private var containing the actual value of the property

Is there a way to abstract this pattern?

Something like

  meta-property private var "pleft" public name "left" type "Float" onChange call setPosChilds;

Using a proprocessor this can be done easily but HaXe does not support
one, correct?

HaXe has many advantages compared to languages such as Lisp or Ruby. But
in this particular case it feels like coding C# or Java.

Those properties are not a big problem: I made Vim generate this code
for me. But using code generators in general do suck.

There will be a time when you have to do some refactoring. And then
having many generated lines is bad.

So should I start using Lisp or such to generate HaXe code?

Thoughts ?

Marc Weber

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: getters and setters - meta programming ? CPP ?

gershon
Hi Marc,
Not really an answer to any of your questions, but have a few remarks to make...

If i understand correctly, you are probably targeting flash, and have a class which composites a DisplayObject\Sprite.
Now, i saw the way you are trying to implement layouts and though i might suggest something,
Basically, the idea involves components and signals.
The compositing class should register and instance of LayoutComponent\LayoutPlugin or whatever you prefer naming, by registering i mean that it listens to events.
So, your setter could look like:

private function setLeft(value : Float): Float
 {
   if (left == value) return left;
   left = value;
   dispatchEvent(new MoveEvent(MoveEvent.MOVE));
   return value;
 }

here's a MoveEvent for your reference, http://code.google.com/p/haxegui/source/browse/trunk/haxegui/events/MoveEvent.hx

I would also suggest that components follow an interface, even if empty...
class myClass {
  sprite : Sprite;
  componenets:Array<IComponent>; //looks better than an Array<Dynamic>
}

if i'm still not making sense, this should http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/

And once again, sorry for going off-topic...
gershon.

On Fri, Mar 19, 2010 at 2:23 AM, Marc Weber <[hidden email]> wrote:
The composition pattern seems to work.

Now I'd like to add a class resizing childs but also adding some margin
(top,left,right,bottom)


// add some border
class Margin extends PP {

 private var pps:List<PP>;

 // property left {{{1
 private var pleft: Float;
 public var left(getLeft, setLeft) : Float;
 private function getLeft(): Float
 {
   return pleft;
 }
 private function setLeft(value : Float): Float
 {
   if (pleft == value) return pleft;
   pleft = value;
   setPosChilds();
   return value;
 }
 // }}}


I think this kind of getter and setter is used very much. Am I right?

- setting a value does nothing if a value doesn't change.
- it returns the value
- it calls some action (setPosChilds)
- there is a private var containing the actual value of the property

Is there a way to abstract this pattern?

Something like

 meta-property private var "pleft" public name "left" type "Float" onChange call setPosChilds;

Using a proprocessor this can be done easily but HaXe does not support
one, correct?

HaXe has many advantages compared to languages such as Lisp or Ruby. But
in this particular case it feels like coding C# or Java.

Those properties are not a big problem: I made Vim generate this code
for me. But using code generators in general do suck.

There will be a time when you have to do some refactoring. And then
having many generated lines is bad.

So should I start using Lisp or such to generate HaXe code?

Thoughts ?

Marc Weber

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: getters and setters - meta programming ? CPP ?

Cauê W.
it would be really nice to be able to proxy all getters/setters calls to another variable.
e.g.

class Test
{
var point:Point;
public var x(point.x, point.x):Float;
public var y(point.y, point.y):Float;
}

2010/3/20 gershon <[hidden email]>
Hi Marc,
Not really an answer to any of your questions, but have a few remarks to make...

If i understand correctly, you are probably targeting flash, and have a class which composites a DisplayObject\Sprite.
Now, i saw the way you are trying to implement layouts and though i might suggest something,
Basically, the idea involves components and signals.
The compositing class should register and instance of LayoutComponent\LayoutPlugin or whatever you prefer naming, by registering i mean that it listens to events.
So, your setter could look like:


private function setLeft(value : Float): Float
 {
   if (left == value) return left;
   left = value;
   dispatchEvent(new MoveEvent(MoveEvent.MOVE));
   return value;
 }

here's a MoveEvent for your reference, http://code.google.com/p/haxegui/source/browse/trunk/haxegui/events/MoveEvent.hx

I would also suggest that components follow an interface, even if empty...
class myClass {
  sprite : Sprite;
  componenets:Array<IComponent>; //looks better than an Array<Dynamic>
}

if i'm still not making sense, this should http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/

And once again, sorry for going off-topic...
gershon.


On Fri, Mar 19, 2010 at 2:23 AM, Marc Weber <[hidden email]> wrote:
The composition pattern seems to work.

Now I'd like to add a class resizing childs but also adding some margin
(top,left,right,bottom)


// add some border
class Margin extends PP {

 private var pps:List<PP>;

 // property left {{{1
 private var pleft: Float;
 public var left(getLeft, setLeft) : Float;
 private function getLeft(): Float
 {
   return pleft;
 }
 private function setLeft(value : Float): Float
 {
   if (pleft == value) return pleft;
   pleft = value;
   setPosChilds();
   return value;
 }
 // }}}


I think this kind of getter and setter is used very much. Am I right?

- setting a value does nothing if a value doesn't change.
- it returns the value
- it calls some action (setPosChilds)
- there is a private var containing the actual value of the property

Is there a way to abstract this pattern?

Something like

 meta-property private var "pleft" public name "left" type "Float" onChange call setPosChilds;

Using a proprocessor this can be done easily but HaXe does not support
one, correct?

HaXe has many advantages compared to languages such as Lisp or Ruby. But
in this particular case it feels like coding C# or Java.

Those properties are not a big problem: I made Vim generate this code
for me. But using code generators in general do suck.

There will be a time when you have to do some refactoring. And then
having many generated lines is bad.

So should I start using Lisp or such to generate HaXe code?

Thoughts ?

Marc Weber

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: getters and setters - meta programming ? CPP ?

Gamehaxe
In reply to this post by MarcWeber
Hi,

Does the "default" property getter help?

Hugh


class Test
{
var x(default,setX) : Int;

public function new() { {

function setX(inVal:Int) : Int
{
   x = inVal;
   trace("Set!");
   return x;
}

public static function main()
{
    var t = new Test();
    t.x = 10;
    trace(t.x);
}

}



> The composition pattern seems to work.
>
> Now I'd like to add a class resizing childs but also adding some margin
> (top,left,right,bottom)
>
>
> // add some border
> class Margin extends PP {
>
>   private var pps:List<PP>;
>
>   // property left {{{1
>   private var pleft: Float;
>   public var left(getLeft, setLeft) : Float;
>   private function getLeft(): Float
>   {
>     return pleft;
>   }
>   private function setLeft(value : Float): Float
>   {
>     if (pleft == value) return pleft;
>     pleft = value;
>     setPosChilds();
>     return value;
>   }
>   // }}}
>
> I think this kind of getter and setter is used very much. Am I right?
>
> - setting a value does nothing if a value doesn't change.
> - it returns the value
> - it calls some action (setPosChilds)
> - there is a private var containing the actual value of the property
>
> Is there a way to abstract this pattern?
>
> Something like
>
>   meta-property private var "pleft" public name "left" type "Float"  
> onChange call setPosChilds;
>
> Using a proprocessor this can be done easily but HaXe does not support
> one, correct?
>
> HaXe has many advantages compared to languages such as Lisp or Ruby. But
> in this particular case it feels like coding C# or Java.
>
> Those properties are not a big problem: I made Vim generate this code
> for me. But using code generators in general do suck.
>
> There will be a time when you have to do some refactoring. And then
> having many generated lines is bad.
>
> So should I start using Lisp or such to generate HaXe code?
>
> Thoughts ?
>
> Marc Weber
>


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: getters and setters - meta programming ? CPP ?

MarcWeber
In reply to this post by gershon
> here's a MoveEvent for your reference,
> http://code.google.com/p/haxegui/source/browse/trunk/haxegui/events/MoveEvent.hx
What about allowing haxelib to upload repo urls and a description only?
If there is no release yet can still find information about
future libraries then.

You're likely to miss code like this. Missing code often ends in
reinventing the wheel splitting efforts.

> I would also suggest that components follow an interface, even if empty...
> class myClass {
>   sprite : Sprite;
>   componenets:Array<IComponent>; //looks better than an Array<Dynamic>
> }
>
> if i'm still not making sense, this should
> http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/

Do you know any open source real world code using this pattern?
I only know the traditional hierarchical style very well.

I started writing my own parent and position aware object hierarchy.

So I wrote a class which has a parent and a position.
Then I wrote childs which have textfields and or progressbars.
I can replace implementation depending on backend.

I don't need a complete gui. I only need some small things right now.
I'll post my code later. If think its useful I'll create a haxelib then.

Somehow I didn't like the way arctic was designed. Basing everything on
one big enum adding implementation details such as how to load an image
into large files didn't look appealing to me. The enum has one custom
escape member which you can use to add custom blocks. Does this mean I
always have to wrap custom blocks (such as new controls) using
CustomBlock every time ?

Marc Weber

--
haXe - an open source web programming language
http://haxe.org