Wiki
Version 4 (modified by todd.a, 14 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 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

Back to LanguageTopics