Version 7 (modified by Nefarel, 12 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
Dynamically sized heterogenous content data structures.
List contents are comma separated, collection is [] 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
Also known as associative arrays or Dictionaries (Dicts).
Key and value is colon separated, each entry comma separated, collection is {} 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
Unordered hashed collection of unique items optimized for membership testing and set operations.
Values are comma separated, collection is {} 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)
Array Literals
Statically sized homogenous content data structure.
Values are comma separated, collection is [] delimited with an '@' prefix.
Empty array is @[]
e.g
names = @[ 'mike', 'gary', 'pat', 'bruce', 'paul'] # String[] or Array<of String> heads = @[3,1,1,1,1] # int[] or Array <of int> heads[4] = 3
Lists are probably easier to use and more flexible but for some purposes arrays have better performance.
Numeric Literals
Non explicitly typed floating point 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 type and size specification ( with an optional leading '_').
Types are
- u - unsigned int
- i - signed int
- d - decimal
- f - float
Sizes are
- for int 8, 16, 32, 64
- for float 32, 64
as in
d = 123 # default (Decimal) d = 123.4d 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 f = 1327.3_f # float (default size 64) f1 = 97.3f32 # or f1 = 97.3_f32
String and Char Literals
See Strings