Note: information on this page refers to Ceylon 1.1, not to the current release.
Callable references
A callable reference is an expression that references something (a function, method, or class) that can be invoked by specifying a positional argument list.
Usage
value classReference = String;
value methodReference = 1.plus;
value functionReference = sum<Integer>;
Description
Callable references introduce a level of indirection between the
definition of a function, method, or class, and the invocation of
the function, method, or class, allowing the definition of generic
functions, called higher order functions, that operate upon
callable references and other Callable
values.
Type
The type of a callable reference is the callable type of the thing being referenced, so referring to the example above,
- the type of
classReference
isString({Character*})
, - the type of
methodReference
isInteger(Integer)
. - the type of
functionReference
isInteger({Integer+})
,
Unreferenceable things
It's not possible to obtain a callable reference to an abstract
class, because calling it would be the same as instantiating the
abstract
class. So, for example, the following code is illegal:
value objectReference = Object;
A callable reference to a generic declaration must specify type
arguments, as shown by functionReference
in the example above.
This code is illegal:
value functionReference = sum;
Invocation
A callable reference may be invoked, for example:
classReference({'h', 'e', 'l', 'l', 'o'})
This is an indirect invocation, so you cannot use a named argument list.
Equality
Callable
does not refine Object.equals
. This means that
if you obtain two different callable references to the
same thing they will not compare as equal:
value ref = String;
// while this assertion will pass...
assert(ref == ref);
// ...these assertions will fail
assert(String == String);
assert(ref == String);
While this may seem surprising for callable references
like String
it would also be surprising if
some Callable
instances had a semantic for equals
, but
others (such as those produced by higher order functions)
did not.
See also
- Callable references in the Ceylon language specification