Note: information on this page refers to Ceylon 1.1, not to the current release.
Parameters
A function or class initializer has a parameter list declaring the parameters of the function or class. A parameter list may contain zero or more parameters.
Usage
You have the choice between 'inline' style:
class Example(Integer int, String name) {
}
void example(Integer int, void print(String name)) {
}
And 'reference' style:
class Example(int, name) {
Integer int;
String name;
}
void example(int, print) {
Integer int;
void print(String name);
}
The second style is preferred when the parameters are annotated.
Description
Parameter lists
Syntactically, a parameter list is a comma separated list enclosed in parentheses following the type parameter list. Each parameter is either:
- zero or more annotations, a type expression, a name, and, optionally, a one or more parameter lists, or
- a reference to a value or function declaration in the class or function body that follows.
A parameter with its own parameter list is called a callable parameter, and its type is a function type. A function with no parameter list is called a value parameter.
Defaulted parameters
A default argument for a parameter may be specified.
For a value parameter, the default argument expression follows the parameter name, separated with an equals sign:
void example(Integer int,
String name = int==0 then "zero" else "") {
}
For a callable parameter, the default argument expression follows the
parameter's parameter list, separated with a fat arrow (=>
):
void example(String name(Integer int)
=> int == 0 then "zero" else "") {
}
A defaulted parameter allows the function or class to be invoked without an argument for that parameter.
Parameters with default values may be called optional parameters (not to be confused with an optional type T?
) or
defaulted parameters. Parameters without default values may be called
required parameters. The default argument expression may be called the
default argument.
To avoid ambiguity, defaulted parameters are only permitted after all the required parameters in the parameter list.
Variadic methods (and 'varargs')
Note: In Ceylon, functions which accept an Iterable
parameter are
generally preferred over variadic functions, since Iterable
s may be
evaluated lazily by the called function or class, whereas the arguments
to a variadic parameter must be evaluated and packaged into a sequence
before the function or class is called.
A variadic function has a variadic parameter as the last parameter in the parameter list.
There are two different kinds of variadic parameter:
-
Possibly-empty variadic parameters allow the function or class to be
invoked with the caller specifying zero
or more arguments for that parameter. Syntactically, a possibly-empty
variadic parameter is declared with a type expression followed by an
asterisk,
*
, and then the parameter name. From within the function or class the parameter has the typeT[]
. A possibly-empty variadic parameter's default argument is implictly the empty sequence[]
. -
Nonempty variadic parameters allow the function or class to be
invoked with the caller specifying one
or more arguments for that parameter. Syntactically, non-empty
variadic parameters are declared with a type expression followed by a
plus sign,
+
, and then the parameter name. From within the function or class the parameter has the type[T+]
. A nonempty variadic parameter doesn't have a default argument, and so none of the preceding parameters may be defaulted.
For example
void variadic(String string, Integer* ints) {
/* method body: statements
parameter ints has type Integer[] here */
}
void nonemptyVariadic(String+ strings) {
/* method body: statements
parameter strings has type [String+] here */
}
Invocation
When a reference to the function or class is the primary of an
invocation, the caller supplies an
either a positional argument list
or a named argument list
whose type, expressed
in terms of Tuple
,
must be a subtype of the type of the parameter list, also expressed
as a tuple type.
Multiple parameter lists
A function can have multiple parameter lists. A function with multiple parameters lists is equivalent to a function with one parameter list that returns a function.
Here's an example:
String name(String firstName)(String secondName) {
return firstName + " " + secondName;
}
This is equivalent to the following higher-order function that returns an anonymous function:
String(String) name(String firstName)
=> (String secondName) {
return firstName + " " + secondName;
};
Note: A class may not have multiple parameter lists.
See also
- Type parameters
class
declarationfunction
declaration- Parameters in the Ceylon language spec