Language features

Lambda expressions

Lambda expressions within steelscript are actually treated as regular functions. They allow to quickly define short functions using a simple syntax. They can be easily passed to functions and look natural in the regular syntax. Lambda expressions are defined in the following way:

func ( arguments ) -> expression_body

Example

Lambda expressions
var numbers = { 1 , 2 , 3 , 4 , 5 , 6};

numbers
.select ( func ( x ) -> x > 2 && x % 2 == 0 )
.foreach ( func ( x ) -> print ( x ) );

Extension functions

Extension functions can be used to extend functionality of custom and build-in objects. They are defined like normal functions, but instead of the keyword "function", "extension" is used. These functions take a special "parameter" to define the name of the object they extend. Extension functions support both get and set bodies. Names for the build-in objects are: string, int, double, object, array and dictionary.

extension<object_name >function_name( arguments )
{

[ get ]
[ body ]

[ set ]
[ body ]

}

Example

Extension functions
function MyObject ( )
{
var foo = 5;
}

var myobj = new MyObject ( );
var myint = 5;

extension < MyObject > Multiply ( target , arg1 )
{
return target.foo * arg1;
}

extension < int > Multiply ( target , arg1 )
{
return target * arg1;
}

cout << myobj.Multiply ( 5 );
cout << myint.Multiply ( 5 );

Macro's

Macro's are compiler instructions, they are executed during compilation time. It's arguments are not evaluated, but directly inserted into the code template. There are some standard macro's already defined by steelscript, such as FOR and TRYCATCH. It's also possible to define custom macro's. The arguments that are passed to the macro can be referred to in the macro body by prepending $ to the argument name ($argument).

Example

Macro's
macro sort_and_print_numbers ( array_name )
{
$array_name.sort ( func ( x , y ) -> y.compareto ( x ) );

$array_name.foreach ( func ( x ) -> print ( x ) );
}

var numbers = { 10 , 3 , 2 , 6 , 7};

macro < sort_and_print_numbers > ( numbers );