"new" functions
new and list-new
The function for creating a new value is usually called new or list-new,
and there is a special syntax for calling these functions.
The syntax 1, 2, 3 may call either:
- A
newfunction taking those arguments, making it equivalent tonew1,2,3 - A
list-newfunction taking with an array containg1,2,3
new is for functions that expect a fixed number of arguments.
list-new is for functions that take a variable number of arguments.
1, 2, 3 is a call expression.
Keen will build the initial overload set using all functions named new or list-new,
then go through the same logic described in Overloading.
foo 1, 2 and foo (1, 2) are two different expressions.
The former just calls foo. The latter calls new first, then foo on the result.
More "new" syntax
Single-argument new
To call new with a single argument, end with a trailing comma.
Empty new
To call new with no arguments, write ().
Implicit empty new
In many contexts fn-new with no arguments will be called implicitly.
Defining a "new" function
new functions are just ordinary functions and are declared in the usual way.
Defining a "list-new" function
list-new functions are also just ordinary functions.
Keen generates an array from all the arguments before calling list-new.
Multi-line new
There is an equivalent syntax for calling new or list-new:
This behaves the same as 1, 2, 3, just with a multi-line syntax to make it convenient for bigger calls.
The extra indentation is not needed when it is the return value of a function:
Multi-line new with arrays
Using -- instead of -
concatenates all the elements of an array into the array passed to list-new.
This can be used to conditionally include elements in an array.
Named-new
The syntax x: 1, y: 2 calls either:
- A
newfunction with parameters namedxandy. - A
named-newfunction taking an array of names and an array of values.
Keen will build the initial overload set with both kinds of function, then filter using the logic described in Overloading.
This has a multi-line form which is usually preferred.
To demonstrate with a custom new:
To demonstrate with a custom named-new: