Forums

Declaring backing vars for properties on the same line

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Declaring backing vars for properties on the same line

Postby Charles » Sat Nov 01, 2008 4:32 pm

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...
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Declaring backing vars for properties on the same line

Postby jonathandavid » Thu Dec 11, 2008 12:10 pm

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
jonathandavid
 
Posts: 159

Re: Declaring backing vars for properties on the same line

Postby jonathandavid » Wed Dec 17, 2008 11:01 am

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
jonathandavid
 
Posts: 159

Re: Declaring backing vars for properties on the same line

Postby Charles » Thu Dec 18, 2008 9:35 am

_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>"
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Declaring backing vars for properties on the same line

Postby hopscc » Sat Dec 20, 2008 3:41 am

Ticket#102 opened for this
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Declaring backing vars for properties on the same line

Postby Charles » Sat Dec 20, 2008 1:39 pm

I added a comment with the actual syntax to ticket:102. Looks good.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 31 guests