Declaring backing vars for properties on the same line
Posted: 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:
Here's an example program:
Here's an example that tests the explicit naming of the var:
I do wonder if the automatic backing var shouldn't be a double underscore private var by default...
# 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...