Show/Hide Toolbars

XSharp

Codeblocks are an important part of the X# language.

 

Traditionally the codeblock looked like

 

codeblock                  :  { | codeblockParamList? | expression }
                         ;
codeblockParamList  : identifier (, identifier)*
                   ;

For example

  {|a,b| a*b}

X# has extended the Codeblock rule. We now not only accept a single expression, but also a statement list and an expressionlist:

 

codeblock                  : { | codeblockParamList? |
                         ( expression
                         | eos statementblock
                         | codeblockExpressionList )
                         }
                         ;

codeblockExprList        : (expression? ,)+ expression                        // The last expression is the return value of the block
                  ;
 

Examples of this are

{|a,b| a:= Sqrt(a), a*b}    
  {|a,b|
     ? a
     ? b
  }

The second example can be seen as an anonymous method with 2 parameters