Closures

Sather - Language Specification
Prev |  | Next

# Chapter 9. Closures

Routine and iter closures are similar to the 'function pointer' and 'closure' constructs of other languages. They bind a reference to a method together with zero or more argument values (possibly including 'self').

# 9.1. Closure creation expressions

Example 9-1. Examples:

    bind(2.plus(_))
    bind(filter!(bind(is_eq(a))))

    method_closure_create_expression ==>
            routine_closure_create_expression | iter_closure_create_expression

    routine_closure_create_expression ==>
            bind (  [ type_specifier ::  |  routine_closure_argument . ] identifier [ ( routine_closure_argument { ,  routine_closure_argument } ) ] )

    routine_closure_argument ==>
            routine_mode ( expression | _ )

    iter_closure_create_expression ==>
            bind (  [ type_specifier ::  |  iter_closure_argument . ] iter_name [ ( iter_closure_argument { ,  iter_closure_argument } ) ] )

    iter_closure_argument ==>
            iter_mode ( expression | _ )

The outer part of the expression is 'bind(...)'. This surrounds a routine or iterator call in which any of the arguments or self may have been replaced by the underscore character '_'. Such unspecified arguments are unbound. Unbound arguments are specified when the closure is eventually called. 'out' and 'inout' arguments must be left unbound. In case of ambiguity, the signature of the method specified in the 'bind(...)' expression is inferred from context. The same overloading resolution rules as for the method call expressions (See Sather supports routine and iterator overloading. In addition to the name, the number, types, and modes of arguments in a call and whether a return value is used all contribute to the selection of the method. The modal_list portion of a call must supply an expression corresponding to each declared argument of the method. There must exist a method with the specified name such that:) apply to the closure creation expressions.

The expressions in this construct are evaluated from left to right and the resulting values are stored as part of the closure. Closure creation expressions return closure types. As previously described on See Type specifiers, the type specifiers for these types have the form:

    method_closure_type_specifier ==>
            routine_closure_type_specifier | iter_closure_type_specifier

    routine_closure_type_specifier ==>
            ROUT [ { routine_mode type_specifier { ,  routine_mode type_specifier } } ] [ : type_specifier ]

    iter_closure_type_specifier ==>
            ITER [ { iter_mode type_specifier { ,  iter_mode type_specifier } } ] [ : type_specifier ]

These specifiers begin with the keywords 'ROUT' or 'ITER' and are followed by the modes and types of the underscore arguments, if any, enclosed in braces (e.g. 'ROUT{A, out B, inout C}', 'ITER{once A, out B, C}'). These are followed by a colon and the return type, if there is one (e.g. 'ROUT{INT}:INT', 'ITER{once INT}:FLT').

Prev | Home | Next
break! expressions | Up | Closure calls