Page 1 of 1

Declaring backing vars for properties on the same line

PostPosted: Sat Nov 01, 2008 4:32 pm
by Charles
In development, there is now a shortcut syntax for declaring a property and its backing var in one line:
# example:
get x from var as int

# is a short cut for these two lines:
var _x as int
get x from var

# the general form of the shortcut above is:
get <name> from var as <type>

# the fully general form is:
(get|pro|set) <name> from (var|<varName>) as <type>

Here's an example program:
class Point

def init
pass

get x from var as int

get y from var as int

def moveBy(dx as int, dy as int)
_x += dx
_y += dy


class UsePoint

def main is shared
p = Point()
assert p.x == 0 and p.y == 0
p.moveBy(2, 3)
assert p.x == 2 and p.y == 3

Here's an example that tests the explicit naming of the var:
class Point

def init
_x = 0
__y = 0

get x from var as int # typical property with matching var backing

get y from __y as int # test explicit naming of the var. note that __y will be private

def moveBy(dx as int, dy as int)
_x += dx
__y += dy


class UsePoint

def main is shared
p = Point()
assert p.x == 0 and p.y == 0
p.moveBy(2, 3)
assert p.x == 2 and p.y == 3


I do wonder if the automatic backing var shouldn't be a double underscore private var by default...

Re: Declaring backing vars for properties on the same line

PostPosted: Thu Dec 11, 2008 12:10 pm
by jonathandavid
Chuck wrote:In development, there is now a shortcut syntax for declaring a property and its backing var in one line:

# example:
get x from var as int

# is a short cut for these two lines:
var _x as int
get x from var




First let me say that I think this is a great idea. Second, I wonder if the syntax could be modified a little bit in order to allow:

get x from var = 1


Which would be equivalent to the following two liner:

var _x as int = 1
get x from _x


Or also equivalent to making the initialization in a constructor:

get x from var as int

def init
_x = 1


Chuck wrote:I do wonder if the automatic backing var shouldn't be a double underscore private var by default...


Absolutely. IMHO, both for methods and member variables, the single underscore should mean "private", and the double underscore should be reserved for protected members. I say so because I use private much more that protected, and I don't believe that abusing protected is a good thing. Especially for member variables, which I think that never should be protected (they should be public for "struct-like" classes that don't have any state, or private otherwise).

Regards,
Manuel

Re: Declaring backing vars for properties on the same line

PostPosted: Wed Dec 17, 2008 11:01 am
by jonathandavid
Chuck: did you get to read my post above?

I'm especially interested in the possibility to write "get x from var = 1".

Regards,
Manuel

Re: Declaring backing vars for properties on the same line

PostPosted: Thu Dec 18, 2008 9:35 am
by Charles
_foo and __foo will remain protected and private. We could explore the possibility of an attribute you put on a type declaration to alter that.

Yes, I'll accept a ticket for "get/set/pro <name> from var = <expr>"

Re: Declaring backing vars for properties on the same line

PostPosted: Sat Dec 20, 2008 3:41 am
by hopscc
Ticket#102 opened for this

Re: Declaring backing vars for properties on the same line

PostPosted: Sat Dec 20, 2008 1:39 pm
by Charles
I added a comment with the actual syntax to ticket:102. Looks good.