Wiki
Version 9 (modified by kobi7, 11 years ago)

--

All of cobra in one page

The purpose of this page is to see usage by looking at sample code, and have a link to the thorough documentation.

SORRY, WORK IN PROGRESS

quick jump:

Keywords in Alphabetical Order

abstract adds all and any as assert base be body bool branch break callable 
catch char class const continue cue decimal def do dynamic each else end ensure
enum event every except expect extend extern fake false finally float for from 
get has if ignore implements implies import in inherits inlined inout int 
interface internal invariant is listen lock mixin must namespace new nil nonvirtual
not number objc of off old on or out override par partial pass passthrough post
print private pro protected public raise ref require return same set shared sig stop
struct success test this throw to to? trace true try uint use using var vari 
virtual where while yield 

comments:

# a one line comment

/#
a multi-line comment
multi?
yes.
 #/
basic structure:
very top level keywords are the optional "namespace" and "use"
top level keywords are:
interface, class, struct, mixin
    inside interface are:
    def, pro, get, set
    inside class are:
    def, var, pro, get, set, const, test
        inside def there can be:
            test, require, ensure, body - or the beginning of code.

examples:
...


valid keywords inside a method (def):
you can have "trace, assert" wherever you want inside a method
very useful for debugging are 
trace and accepts multiple variables
assert accepts a boolean, with one expression per line.
example:



contracts example:
def gap(a as uint, b as uint) as uint
    require
        0 <= a <= 1000, 0 <= b <= 1000
        # a >= b # enable this line (or change to ints) to fix the definition
    ensure
        result <= 1000
    body
        res = a - b # is something wrong with this code? (when b > a, uint handles negatives by going to uint's maxvalue)
        trace res

        return res
    
    # without contracts you might get surprising values, not knowing why

mixin example:

mixin VolumeSupport
    pro volume from var as int
    def decVolume
        .volume = Math.max(0, .volume - 1)

    def incVolume
	.volume = Math.min(100, .volume + 1)

class Radio adds VolumeSupport	
    cue init
        base.init
	.volume = 0

class TV adds VolumeSupport	
    cue init
	base.init
	.volume = 0
class Program
    def main
	r = Radio()
	assert r.volume == 0
	r.incVolume
	assert r.volume == 1