Changeset 1720

Show
Ignore:
Timestamp:
11/01/08 19:18:27 (2 months ago)
Author:
Chuck.Esterbrook
Message:

Added a shortcut syntax for declaring a property and its backing variable in one line.
ticket:46
reported-by:necromanco

Location:
cobra/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Developer/IntermediateReleaseNotes.text

    r1717 r1720  
    99 
    1010* Added new install-from-workspace command in the Source directory. This makes installing out of a Subversion workspace very easy and reliable. Documentation is in the doc string at the top of InstallFromWorkspace.cobra. 
     11 
     12* Added a shortcut syntax for declaring a property and its backing variable in one line: 
     13.code 
     14        get x from var as int 
     15        # is a short cut for these two lines: 
     16        var _x as int 
     17        get x from var 
     18 
     19        # the general form of the shortcut above is: 
     20        get <name> from var as <type> 
     21         
     22        # the fully general form is: 
     23        (get|pro|set) <name> from (var|<varName>) as <type> 
    1124 
    1225* Modified commandline options so -r becomes synonym for -run rather   
  • cobra/trunk/Source/CobraParser.cobra

    r1716 r1720  
    15331533                else 
    15341534                        varName = .expect('ID').text 
    1535                 possibleVarDef = .curBox.declForName(varName) 
    1536                 if possibleVarDef is nil 
    1537                         .throwError('There is no variable named "[varName]" to match the property "[name]".') 
    1538                 if not possibleVarDef inherits BoxVar 
    1539                         .throwError('A property can only cover for variables. [varName] is a [possibleVarDef]') 
    1540                 varDef = possibleVarDef to BoxVar 
     1535                declVarType = if(.optional('AS'), .typeId, nil) 
     1536                if declVarType 
     1537                        varDef = BoxVar(token, .curBox, varName, declVarType, List<of String>(_isNamesStack), nil, nil, 'Automatic backing var for property "[name]".') 
     1538                        .curBox.addDecl(varDef) 
     1539                else 
     1540                        possibleVarDef = .curBox.declForName(varName) 
     1541                        if possibleVarDef is nil 
     1542                                .throwError('There is no variable named "[varName]" to match the property "[name]".') 
     1543                        if possibleVarDef inherits BoxVar 
     1544                                varDef = possibleVarDef 
     1545                        else 
     1546                                .throwError('A property can only cover for variables. [varName] is a [possibleVarDef]')  # TODO: .englishName? 
    15411547                if .curBox inherits Interface 
    15421548                        .throwError('Cannot use the "from" form of a property inside an interface declaration.') 
  • cobra/trunk/Tests/120-classes/504-properties-from.cobra

    r1484 r1720  
    2323                """ 
    2424 
    25         def main is shared 
    26                 p as Person = Person() 
    27                 p.ageProp = 5 
    28                 assert p.age==5 
    29                 p.age1 = 6 
    30                 assert p.age1==6 
    31                 p.age2 = 7 
    32                 assert p.age3==7 
    33                 p.age = 8 
    34                 assert p.age==8 
     25        shared 
     26 
     27                def main 
     28                        .testPerson 
     29                        .testPoint 
     30         
     31                def testPerson 
     32                        p as Person = Person() 
     33                        p.ageProp = 5 
     34                        assert p.age==5 
     35                        p.age1 = 6 
     36                        assert p.age1==6 
     37                        p.age2 = 7 
     38                        assert p.age3==7 
     39                        p.age = 8 
     40                        assert p.age==8 
     41         
     42                def testPoint 
     43                        p as Point = Point() 
     44                        assert p.x == 0 and p.y == 0 
     45                        p.moveBy(2, 3) 
     46                        assert p.x == 2 and p.y == 3 
    3547 
    3648 
     
    6173        get foo from var 
    6274                is override 
     75 
     76 
     77class Point 
     78 
     79        # TODO: cover `pro` and `set` 
     80        # TODO: verify _x is protected and __y is private 
     81 
     82        def init 
     83                _x = 0 
     84                __y = 0 
     85 
     86        get x from var as int   # typical property with matching var backing 
     87         
     88        get y from __y as int   # test explicit naming of the var. note that __y will be private 
     89 
     90        def moveBy(dx as int, dy as int) 
     91                _x += dx 
     92                __y += dy