The general syntax for the function data type is:
function [function_name] (arg1 type, arg2 type,...) [ -> return_type ] {body}
Notice that:
- Both function_name and -> return_type are optional.
- Except that function_name is not optional when the function definition is recursive.
- When the return_type is not specified, StreamBase determines the return type from the output of the function.
- Parentheses enclose the list of arguments.
- Braces enclose the body of the function definition.
For simple data types, declaring the type is very straighforward -- it's just the name of the type.
function to_fahrenheit(cels double) -> double { (9/5 * cels) + 32 }
But what about the complex data types -- lists, tuples, and functions?
Here's a function whose argument type is a tuple whose schema has two fields, an integer, a, and a string, b:
function foo (arg (a int, b string)) { "bar" }
Here's a function that takes an argument which is a list of integers:
function foo (arg list (int)) { 3 }
This function has an argument that is a list of tuples; the schema of the tuples is a single int field, a.
function foo (arg list ((a int))) { 3 }
This one's argument is a tuple of lists plus a string:
function foo (arg (a list(int), b string)) { 3 }
And this function's argument is itself an unnamed function whose argument is an int and that returns a double
function foo (arg (a int)->double) { 3 }
Finally, consider the following unlikely argument list for a function definition:
function foobar (arg (a int)->(b double)->(c string)->int ) { 3 }
Function foobar takes a single argument that is an unnamed function,
which itself takes an int argument and returns a function
that takes a double argument and returns a function
that takes a string argument and returns an int.