Wiki

Changes between Version 12 and Version 13 of AllOfCobraInOnePage

Show
Ignore:
Timestamp:
01/10/14 06:23:11 (7 years ago)
Author:
kobi7
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AllOfCobraInOnePage

    v12 v13  
    4444 
    4545examples: 
    46 ... 
     46{{{ 
     47#!cobra 
     48interface IAudioPacket 
     49        def toWavPacket as WavPacket 
     50 
     51 
     52class WavPacket implements IAudioPacket 
     53        cue init(bytes as uint8[]) 
     54                base.init 
     55                # ... create the wav packet format, using the bytes 
     56         
     57        def toWavPacket as WavPacket 
     58                return this 
     59 
     60class Mp3Packet implements IAudioPacket 
     61        var bytes as uint8[] 
     62        def toWavPacket as WavPacket 
     63                wavBytes = Mp3Utils.decode(.bytes) 
     64                wavpack = WavPacket(wavBytes) 
     65                return wavpack 
     66 
     67}}} 
    4768 
    4869 
    4970valid keywords inside a method (def): 
    5071you can have "trace, assert" wherever you want inside a method 
    51 very useful for debugging are  
    52 trace (also accepts multiple variables) 
     72very useful for debugging are 'trace' and 'assert' 
    5373}}} 
    5474{{{ 
     
    6484assert accepts a boolean, with one expression per line. 
    6585example: 
    66  
     86}}} 
    6787 
    6888 
    6989contracts example: 
     90without contracts you might get surprising values, not knowing why 
     91{{{ 
     92#!cobra 
    7093def gap(a as uint, b as uint) as uint 
     94    test 
     95        assert .gap(7,9) == 2 
    7196    require 
    72         0 <= a <= 1000, 0 <= b <= 1000 
    73         # a >= b # enable this line (or change to ints) to fix the definition 
    74     ensure 
    75         result <= 1000 
     97        0 <= a <= 1000 # in this way it's possible to constrain the valid range of arguments for this function. 
     98        0 <= b <= 1000 
     99        # a >= b # enable this line to find the error 
     100 
     101    ensure # we add ensure because we got unexpected results, and it failed. 
     102        result <= 1000 # the return value is assigned to the 'result' variable, so you can test it in the 'ensure' clause 
    76103    body 
    77104        res = a - b # is something wrong with this code? (when b > a, uint handles negatives by going to uint's maxvalue) 
    78         trace res 
     105                    # a fix would be to check if a > b, a-b, else b-a 
     106        trace res # prints out the value and variable name, along with location in code. 
    79107 
    80         return res 
    81      
    82     # without contracts you might get surprising values, not knowing why 
     108        return res    
     109 
    83110}}} 
    84111 
     
    86113mixin example: 
    87114{{{ 
    88  
     115#!cobra 
    89116mixin VolumeSupport 
    90117    pro volume from var as int 
     
    97124}}} 
    98125 {{{ 
     126#!cobra 
    99127class Radio adds VolumeSupport   
    100128    cue init 
     
    104132}}}  
    105133{{{ 
     134#!cobra 
    106135class TV adds VolumeSupport      
    107136    cue init 
     
    110139}}}  
    111140{{{ 
     141#!cobra 
    112142class Program 
    113143    def main