int function twice(int x)
{
return 2*x;
}
string function twice(string s)
{
return s+s;
}
(+ is concatenation for strings. See the section on strings for details.) Then,
twice(2);
->4
twice("hello");
->hellohello
In this manual, functions will be introduced in the form:
int function twice(int x)
that is, by the first lines of their definitions.
Fabl also supports parametric polymorphism, meaning that the types of the inputs and output of a function take on an infinite set of possible values, usually parameterized by the base types of constructed types such as sequences. Here is an example:
For each type <tp>, SeqOf(<tp>) is the type of sequences whose elements are of type <tp> (see the section on sequences for details). The function:SeqOf(<tp>) function plus(SeqOf(<tp>) x,SeqOf(<tp>) y)
concatenates the sequences x and y. Its output type is the same as the types of its inputs. For example, invar a = plus([2,3],[4,5]);
the type of a is SeqOf(int), while in
var b = plus(["a","b"],["c","d"]);the type of b is SeqOf(string).
As in the above example, symbols that are intended to vary over types rather than denote a particular type are enclosed in angle brackets (<>).
Fabl programmers can introduce their own parametrically polymorphic functions, but this facility is not yet documented. A future document - the Fabl Meta Manual - will cover metaprogramming in Fabl (that is, programming whose purpose is to manipulate programs); parametric polymorphism is a part of the metaprogramming facility. Fabl itself is almost entirely implemented using its own metaprogramming constructs.