| 1 | = Literals = |
| 2 | |
| 3 | Cobra provides a convenient way to specify literals for initializing standard collections and [[BR]] |
| 4 | explicitly setting the types (size and sign) for numeric literals. |
| 5 | |
| 6 | == List Literals == |
| 7 | List contents comma separated, collection [] delimited. |
| 8 | |
| 9 | Empty list is [] |
| 10 | |
| 11 | e.g |
| 12 | {{{ |
| 13 | names = [ 'mike', 'gary', 'pat', 'bruce', 'paul'] # List<of String> |
| 14 | heads = [3,1,1,1,1] # List<of int> |
| 15 | if name in [ 'fred', 'george', 'bill' ] |
| 16 | |
| 17 | myList=[] |
| 18 | myList.add('1th') |
| 19 | }}} |
| 20 | |
| 21 | == Map/Dictionary Literals == |
| 22 | Key and value colon separated, each entry comma separated, collection {} delimited. |
| 23 | |
| 24 | empty map is {:}. |
| 25 | |
| 26 | e.g. |
| 27 | {{{ |
| 28 | nameId = { 'mike':10110, 'gary':21003, 'paul':32289 } # Dictionary<of String, int> |
| 29 | assert nameId['mike'] == 10110 |
| 30 | |
| 31 | order = { 0:'mike', 1:'bruce', 2:'gary', 3:'pat' } # Dictionary<of int, String> |
| 32 | |
| 33 | mmap = {:} |
| 34 | mmap['top'] = 99 |
| 35 | }}} |
| 36 | |
| 37 | == Set Literals == |
| 38 | Values comma separated, collection {} delimited. |
| 39 | |
| 40 | Empty set is {}. |
| 41 | |
| 42 | e.g. |
| 43 | {{{ |
| 44 | names= {'gary', 'mike', 'bruce', 'paul'} |
| 45 | assert names.intersection({'gary', 'paul'} == {'gary', 'paul'} |
| 46 | assert not names.isSuperSetOf({'paula'}) |
| 47 | |
| 48 | collisions={} |
| 49 | collisions.add(toyota) |
| 50 | |
| 51 | }}} |
| 52 | |
| 53 | |
| 54 | == Numeric Literals == |
| 55 | |
| 56 | Non explicitly typed numeric Literals are typed to Decimal by default.[[BR]] |
| 57 | This can be overidden by the compiler '''-number''' commandline switch or '''@number''' compiler directive. |
| 58 | |
| 59 | Explicitly typed numeric literals can be specified by suffixing the numeric literal with a sign and size specification ( with an optional leading '_'). |
| 60 | |
| 61 | |
| 62 | Signs are |
| 63 | u - unsigned. |
| 64 | i - signed |
| 65 | |
| 66 | Sizes are |
| 67 | 8 |
| 68 | 16 |
| 69 | 32 |
| 70 | 64 |
| 71 | |
| 72 | as in |
| 73 | |
| 74 | {{{ |
| 75 | i = 123 # default (Decimal) |
| 76 | |
| 77 | ii = 123i # integer (default size) 32 bits |
| 78 | # same as |
| 79 | ii = 123 to int |
| 80 | |
| 81 | j = 123_i16 # signed 16 bit |
| 82 | k = 32u8 # unsigned 8 bit == Byte |
| 83 | |
| 84 | l = 879289992978_i64 # signed 64 bit |
| 85 | }}} |