Wiki

Changes between Initial Version and Version 1 of Literals

Show
Ignore:
Timestamp:
11/13/09 13:46:56 (15 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Literals

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