5.2.6. Statements & Operators

Most of Spicy’s language constructs are pretty standard stuff. We summarize them briefly in the following. We include Spicy’s statements here, as well as some generic, non-trivial operators that aren’t type-specific (e.g., new, begin). For operators specific to a type, see the type’s documentation. assert

assert EXPR;

assert EXPR : MSG;

Ensures at runtime that EXPR evaluates to a True value. If it doesn’t, an exception gets thrown that will typically abort execution. EXPR must either be of boolean type to begin with, or support coercion into it. If MSG is specified, it must be a string and will be carried along with the exception. begin

begin(<container>) <iterator>

Returns an iterator to the beginning of the container’s content. break


Inside a for or while loop, break aborts the loop’s body, with execution then continuing right after the loop construct. confirm


If the parser is currently in trial mode, confirm that the unit is successfully synchronized to the input; the unit is then put into regular parsing mode again. If the unit is not in trial mode confirm has no effect.

See reject to reject the synchronization instead.

confirm can only be invoked from hooks. end

end(<container>) <iterator>

Returns an iterator to the end of the container’s content. for

for ( ID in ITERABLE )

Loops over all the elements of an iterable value. ID is an identifier that will become local variable inside BLOCK, with the current loop element assigned on each round. ITERABLE is a value of any type that provides iterators.


module Test;

for ( i in [1, 2, 3] )
    print i;

for ( i in b"abc" ) {
    print i;

local v = vector("a", "b", "c");

for ( i in v )
    print i;
# spicyc -j for.spicy
c if

if ( EXPR )

if ( EXPR )

A classic if-statement branching based on a boolean expression EXPR. import

import MODULE;

Makes the content of another module available, see Modules for more. new

new T T&

Returns a reference to an instance of a type newly allocated on the heap. If T is a type, a default instance of that type will be allocated. If T is an expression, an instance of the expression’s type will be allocated and initialized with the value of the expression. pack

pack tuple bytes

Packs a value into a binary representation. See Packing / Unpacking Values for details. print

print EXPR;

print EXPR_1, ..., EXPR_N;

Prints one or more expressions to standard output. This is supported for expressions of any type, with each type knowing how to render its values into a readable representation. If multiple expressions are specified, commas will separate them in the output.


A particular use-case combines print with string interpolation (i.e., string::Modulo):

module Test;

print "Hello, %s!" % "World";
print "%s=%d" % ("x", 1);
# spicyc -j print.spicy
Hello, World!
x=1 reject


If the parse is currently in trial mode, reject the synchronization; this immediately fails parsing of the unit and raises the parse error which caused the unit to be put into trial mode. If the unit is not in trial mode this triggers a generic parse error.

See confirm to confirm the synchronization instead.

reject can only be invoked from hooks. return


return EXPR;

Inside a function or hook, return yields control back to the caller. If it’s a function with a non-void return value, the return must provide a corresponding EXPR. stop


Inside a foreach container hook (see here), aborts the parsing loop without adding the current (final) value to the container. switch

switch ( [local IDENT =] CTRL_EXPR ) {
    case EXPR [, ..., EXPR]:


    case EXPR [, ..., EXPR]:


Branches across a set of alternatives based on the value of an control expression. CTRL_EXPR is compared against all the case expressions through the type’s equality operator, coercing CTRL_EXPR accordingly first where necessary. If local IDENT is specified, the blocks have access to a corresponding local variable that holds the value of the control expression. If no default is given, the runtime will throw an UnhandledSwitchCase exception if there’s no matching case.


Don’t confuse the switch statement with the unit type’s switch parsing construct. They look similar, but do different things. throw

throw EXPR;

Triggers a parse error exception with the message indicated by EXPR. EXPR needs to be a String. throw aborts parsing. try/catch


This isn’t available in Spicy yet (#89).


catch [(TYPE IDENT)]


catch [(TYPE IDENT)]

Catches any exception thrown in the try block that match one of the types in any of catch headers, which must be Exception types. A catch without a type matches any exception. If no catch matches an exception thrown in the try block, it’ll be propagated further up the stack. A bare throw statement can be used inside a catch block to rethrow the current exception. unpack

unpack type <unpacked value>

Unpacks a value from a binary representation. See Packing / Unpacking Values for details. while

while ( COND )

while ( local IDENT = EXPR; COND )

while introduces a loop that executes BLOCK for as long as the boolean COND evaluates to true. The second form initializes a new local variable IDENT with EXPR, and makes it available inside both COND and BLOCK.