Wiki

Changeset 2335

Show
Ignore:
Timestamp:
03/20/10 04:42:28 (23 months ago)
Author:
Chuck.Esterbrook
Message:

Improvements to the How-To's.

Location:
cobra/trunk/HowTo
Files:
11 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/HowTo/101-WriteBasicSyntax.cobra

    r2127 r2335  
    5252 
    5353        # If-else statement 
     54        # See also: "Make An If Else Ladder" How To 
    5455        if a > b 
    5556            print 'a is greater' 
     
    5758            print 'a is not greater' 
    5859 
    59         # Can put target on same if target is just one statement 
     60        # Can put target on same line if the target is just one statement 
    6061        if a > b, print 'a is greater' 
    6162        else, print 'b is greater or equal' 
     
    6465        while a > b 
    6566            a -= 1  # augmented assignment 
     67         
     68        # While loop in one line 
    6669        while a > b, a -= 1 
     70 
     71        # Parallel assignment 
     72        a, b = 1, 2 
     73         
     74        # Lists -- see the "Use Lists" How To for more info 
     75        t = ['a', 'b', 'c'] 
     76        for letter in t, print letter 
     77 
     78        # Dictionaries 
     79        d = {'a': 1, 'b': 2} 
     80        assert d['a'] == 1 and d['b'] == 2 
     81        for key, value in d 
     82            print '[key] = [value]'  # string interpolation 
     83 
     84        # Sets -- like lists, but no repetition 
     85        letters = {'a', 'b', 'c'} 
     86        assert 'a' in letters 
     87        for letter in letters, print letter 
    6788 
    6889        # Line continuation is implicit with parenthesized arguments 
  • cobra/trunk/HowTo/111-UseProperties.cobra

    r2127 r2335  
    4444 
    4545# Many properties just cover for an underlying variable. When that 
    46 # is the case, you can use a shortcut syntaxes. Later, you can 
     46# is the case, you can use a shortcut syntax. Later, you can 
    4747# change the property to the full form in order to add logic. 
    4848 
     
    6161 
    6262 
     63# You can even pack an initial value into the declaration 
     64# which also be used to infer the data type. 
     65 
     66class Person3 
     67 
     68    pro name from var = ''  # inferred as String 
     69     
     70    pro age from var = 0    # inferred as int 
     71 
     72 
    6373# In this shortcut syntax, the vars were already declared so 
    6474# the property simply "... from var": 
    6575 
    66 class Person3 
     76class Person4 
    6777 
    6878    var _name as String 
     
    8898# you can specify that name instead of "var": 
    8999 
    90 class Person4 
     100class Person5 
    91101 
    92102    var _name as String 
     
    101111# Properties can be read-only: 
    102112 
    103 class Person5 
     113class Person6 
    104114 
    105115    cue init(name as String) 
  • cobra/trunk/HowTo/125-MakeAnIfElseLadder.cobra

    r2127 r2335  
    88    * Use the `branch` statement (see MakeABranchStatement.cobra) 
    99 
    10 When using an if-else ladder, consider raising a FallThroughException() at the 
     10When using an if-else ladder, consider throwing a FallThroughException() at the 
    1111bottom if you expect that it should never happen. 
    1212 
  • cobra/trunk/HowTo/130-MakeABranchStatement.cobra

    r2127 r2335  
    8484        assert word == 'number' 
    8585 
    86         # Don't forget that you can branch on enumerations, strings and characters. 
     86        # You can also branch on enumerations, strings and characters. 
  • cobra/trunk/HowTo/150-DeclareInits.cobra

    r2127 r2335  
    88 
    99    cue init 
    10         # code 
     10        # statements 
    1111 
    1212    cue init(ARG as TYPE) 
    13         # code 
     13        # statements 
    1414 
    1515    cue init(ARG1 as TYPE1, ARG2 as TYPE2) 
    16         # code 
     16        # statements 
     17 
     18Where the first statement is call to another initializer in the same class or 
     19the base class: 
    1720 
    1821Some key points: 
     
    2023    * You can have 0 or more arguments. 
    2124 
    22     * If you declare no initalizers at all for a given class, Cobra will 
    23       provide one, public, parameterless initializer. 
     25    * If you declare no initializers at all, Cobra will automatically provide 
     26      them to match each non-private initializer in the base class. 
     27 
     28    * If you declare even one initializer, Cobra will not automatically add any 
     29      more. This gives you control over how a class must be initialized. 
    2430 
    2531    * Initializers can be overloaded by the number and type of their arguments. 
     
    2733    * Initializers are public by default. 
    2834 
    29     * Initializers can say "base.init(ARGS)" to invoke a base init. 
     35    * Initializers can say "base.init" or "base.init(ARGS)" to invoke 
     36      a base initializer. 
    3037 
    31     * Initializers can say ".init" or ".init(ARGS)" to invoke a fellow init. 
     38    * Initializers can say ".init" or ".init(ARGS)" to invoke 
     39      a fellow initialier. 
    3240 
    3341    * Initializers can have their own unit tests just like methods. 
     
    94102    get age from var as int 
    95103 
     104 
     105class Program 
     106 
    96107    def main 
    97108        pass 
  • cobra/trunk/HowTo/180-UseLists.cobra

    r2216 r2335  
    230230        assert names.indexOf('foo', 1, 2) == -1  # start at 1, search 2 items 
    231231         
    232         # left off on .insertRange 
     232        # TODO: left off on .insertRange 
    233233         
    234234        # additional methods. search web for "msdn generic list" 
     
    269269        require .count > 0 
    270270        return this[.count // 2] 
     271 
    271272 
    272273# a subclass with specific type 
     
    289290        return leftMost 
    290291 
     292 
    291293# a generic subclass with a type constraint is even better, 
    292294# because then you can specify a type parameter when using it 
  • cobra/trunk/HowTo/186-UseArrays.cobra

    r2046 r2335  
    5353            body  
    5454                sum = 0  
    55                 for num in nums  
    56                     sum += num 
     55                for num in nums, sum += num 
    5756                return sum  
    5857         
     
    6160            # list or any object that implements IList. 
    6261            sum = 0 
    63             for num in nums 
    64                 sum += num 
     62            for num in nums, sum += num 
    6563            return sum  
    6664 
  • cobra/trunk/HowTo/210-UseNilAndNilableTypes.cobra

    r2127 r2335  
    9292        name = if(value and value <> 0, 'value', nil)  # type is "String?" 
    9393        if name, print name 
     94 
     95        # you can coalesce with ? as a binary operator 
     96        displayName = name ? '(no name)' 
     97        print displayName 
  • cobra/trunk/HowTo/282-PassReferencesToMethods.cobra

    r2127 r2335  
    4747        for cust in customers, print cust 
    4848 
    49         # Yes, there will be lambdas in a future version so 
    50         # the comparison can be inlined in the sort() call. 
     49        # you can localize the comparison code with a closure 
     50        customers.sort(do(a as Customer, b as Customer)) 
     51            return a.name.toLower.compareTo(b.name.toLower) 
     52         
     53        # or a lambda expression 
     54        customers.sort(do(a as Customer, b as Customer)=a.name.toLower.compareTo(b.name.toLower)) 
    5155 
    5256    def orderByName(a as Customer, b as Customer) as int is shared 
  • cobra/trunk/HowTo/310-IterateThroughRecursiveDataWithYield.cobra

    r2127 r2335  
    3737    def dump 
    3838        print this stop 
    39         if _left 
    40             print ' left:', _left stop 
    41         if _right 
    42             print ' right:', _right stop 
     39        if _left, print ' left:', _left stop 
     40        if _right, print ' right:', _right stop 
    4341        print 
    4442        if _left, _left.dump 
  • cobra/trunk/HowTo/350-MakeACollectionClass.cobra

    r2127 r2335  
    22MakeACollectionClass.cobra 
    33 
    4 This HowTo shows the syntax and techniques for declaring an IList. 
     4This HowTo shows the syntax and techniques for declaring a class that 
     5implements IList<of T>. 
    56""" 
    67