Haxe Property - getters and setters versus @:isVar
Asked Answered
A

1

6

My question is regarding properties in haxe. If I understand it correctly, if I make property like this var propertyInt(get, set):Int; according to haxe documentation it's not really a variable. But I can make it a variable by modifying it this way @:isVar var propertyInt(get, set):Int;. Otherwise I need to make two methods:

function get_propertyInt()
    return propertyInt;

function set_properyInt(i)
    return propertyInt = i;

So my question is: does it matter if I'm using exclusively @:isVar var propertyInt(get, set):Int; ? For example I have more than 5 properties in my class and as you can imagine making this methods for every property could be huge pain in the rear. So what is the best approach?

Acentric answered 13/1, 2015 at 20:37 Comment(0)
C
11

Here's what the documentation has to say about physical properties (also known as backing fields):

A field is considered to be physical if it is either

  • variable
  • a property with the read-access or write-access identifier being default or null
  • a property with :isVar metadata

So you can set up a property that fully consists of calculated values. Think a read-only property giving you the area of a rectangle as a function of width and height, or think of a property that is backed by some other property and just returns/sets width and height in a different unit. Or maybe you just want to name your backing fields differently, say m_width and m_height.

The :isVar is helpful in situations where the property access rules etc. laid out above would let the compiler think that there is no backing field needed. In that case, the code would fail (from the docs again):

  // This field cannot be accessed because it
  // is not a real variable
  public var x(get, set):Int;

  function get_x() {
    return x;
  }

  function set_x(x) {
    return this.x = x;
  }

By adding :isVar you basically tell the compiler that you absolutely want the backing field. The other option for your particular case would be to use default,default, in which case the compiler knows there is an automatic backing field required and access should be restricted according to the access level of the property (public or private):

public var propertyInt(default, default):Int;

In that case you might also use a variable directly, because the net effect is in essence the same:

public var propertyInt : Int;
Clearcut answered 13/1, 2015 at 21:6 Comment(4)
Thanks much for very fulfilling answer. I will put it in good use. Im still litle confused, i jump over from c# and im slowly starting understand all haxe features. Thanks much ! :)Acentric
I also recommend to look up the meaning of private in Haxe - it's different compared to C# :-)Clearcut
I noticed that private has functionality of protected. Why is that ? Its annoying a little. Is there some reasonable explanation for this behavior ? Also i hate lack of classic for loop functionality ...Acentric
It starts to become OT, but anyway ... Haxe for() examples for C# programmersClearcut

© 2022 - 2024 — McMap. All rights reserved.