#################################### # First example: #################################### var intstring : list = {}; intstring.append( 123 ); intstring.append( "abc" ); #intstring.append( {} ); # typing error io.writeln( intstring, intstring[0], intstring[1] ); #################################### # Second example: #################################### interface HasSizeMethod { routine Size()=>int; } class AA { routine Size()=>int{ return 10 } } class BB { routine Size()=>int{ return 20 } } routine Test( object: AA | BB | HasSizeMethod ) { # casting to an interface will invoke automatic binding: var object2 = (HasSizeMethod) object; io.writeln( object2.Size() ) } io.writeln( std.about( Test ) ); Test( AA() ) Test( BB() ) routine Test2( data: int|float|string ) { switch( data ) type { case int : io.writeln( "handling int", std.about( data ) ); case float : io.writeln( "handling float", std.about( data ) ); case string : io.writeln( "handling string", std.about( data ) ); } } Test2( 1 ); Test2( 1.0 ); Test2( "abc" ); #################################### # Third example: #################################### class FakeImage { var image = [1,2,3,4;5,6,7,8;11,12,13,14;15,16,17,18]; # instead of writing routine methods with all the combinations # such as tuple, tuple, ... # one can use disjoint union to simplify this. routine[]( i: int, js: none|tuple )=>array { # one can simply return image[i,js], but the following is for demonstration purpose: var j1 = 0; var j2 = image.dim(1) - 1; if( js == none ) return image[i,:]; if( js[0] != none ) j1 = js[0]; if( js[1] != none ) j2 = js[1]; return image[i,j1:j2]; } } var image = FakeImage(); io.writeln( image[1,1:] ); io.writeln( image[2,:1] ); #################################### # Fourth example: #################################### routine Sum( alist : list<@T> ) => @T { # reflect.trace(); return alist.sum(); } var s = Sum( { 1, 2, 3 } ); #s += "a"; # typing error io.writeln( s ); var s2 = Sum( { "a", "b", "c" } ); io.writeln( s2 );