Function
In melon functions are first-class citizens, this means that functions are a type of value and are not treated any differently from any other value. They can thus be saved inside variables, passed as arguments to other functions or stored inside an Object
or Array
.
Defining functions
There are various ways to define a function in melon.
Anonymous functions
We have seen that functions are just another type of value in melon, this means that we can assign a function to a variable as we would with any other type of value:
Arguments in melon are expressed with pipes (|
) when defining a function:
A function can be called with more or less arguments than the one it expects.
If the arguments provided are more than the one expected the excess arguments will simply be discarded.
If less arguments are provided the arguments that were not specified will have the value null
.
note
If a function does not return a value using the return
keyword the function will still return the value null
You can pass functions as other function's arguments:
You can store functions in an Object
or an Array
:
Storing Function
values inside an Object
can be used to do OOP, see also Object
and Methods
Labeled functions
Anonymous functions can sometime be hard to debug, especially when using io.print
to print them:
To make it easier to identify functions when debugging you can label them with the following syntax:
Named functions
The most concise way to define a function is by using the named function syntax:
This is just syntactic sugar for:
Closures
Closures are functions which capture values outside of the function itself:
It's important to note that closures do not copy the value being referenced, they will simply point to the value in the stack. This means that the value used by the closure is the actual value in memory when the closure is called and not when it's defined:
If the values in the stack that a closure is referencing go out of scope, the value is copied out of the stack and preserved, so that it will be available and shared among the closures referencing it.
Methods
In OOP a method is a function that can be called on an object to alter its internal state. Melon is not an object oriented language but it provides some syntactic sugar to make functions used as methods more expressive.
Methods could simply be declared as:
Traditionally in object oriented languages the object on which the method has been invoked can be accessed through an implicit argument this
.
You can do this in melon by replacing the fat arrow =>
with ->
:
Look at the method access operator to understand how you can use method definitions in conjunction with Object
values to do OOP.
Variadic arguments
Functions can consume all the arguments that are provided to them without having to specify the number of arguments they expect in advance:
Standard arugments definition can also be mixed with the variadic syntax:
Further function manipulation
There are other ways to manipulate Function
values, they can be found in the function
core module. This module includes ways to do introspection/reflection on Function
values.