Version 1 (modified by hopscc, 15 years ago) |
---|
Literals
Cobra provides a convenient way to specify literals for initializing standard collections and
explicitly setting the types (size and sign) for numeric literals.
List Literals
List contents comma separated, collection [] delimited.
Empty list is []
e.g
names = [ 'mike', 'gary', 'pat', 'bruce', 'paul'] # List<of String> heads = [3,1,1,1,1] # List<of int> if name in [ 'fred', 'george', 'bill' ] myList=[] myList.add('1th')
Map/Dictionary? Literals
Key and value colon separated, each entry comma separated, collection {} delimited.
empty map is {:}.
e.g.
nameId = { 'mike':10110, 'gary':21003, 'paul':32289 } # Dictionary<of String, int> assert nameId['mike'] == 10110 order = { 0:'mike', 1:'bruce', 2:'gary', 3:'pat' } # Dictionary<of int, String> mmap = {:} mmap['top'] = 99
Set Literals
Values comma separated, collection {} delimited.
Empty set is {}.
e.g.
names= {'gary', 'mike', 'bruce', 'paul'} assert names.intersection({'gary', 'paul'} == {'gary', 'paul'} assert not names.isSuperSetOf({'paula'}) collisions={} collisions.add(toyota)
Numeric Literals
Non explicitly typed numeric Literals are typed to Decimal by default.
This can be overidden by the compiler -number commandline switch or @number compiler directive.
Explicitly typed numeric literals can be specified by suffixing the numeric literal with a sign and size specification ( with an optional leading '_').
Signs are
u - unsigned. i - signed
Sizes are
8 16 32 64
as in
i = 123 # default (Decimal) ii = 123i # integer (default size) 32 bits # same as ii = 123 to int j = 123_i16 # signed 16 bit k = 32u8 # unsigned 8 bit == Byte l = 879289992978_i64 # signed 64 bit