Methods are recipes that can take arguments to "fill in the blanks".


A method is anonymously defined with a thin arrow ->, like so:

<arguments> -> <recipe>

Arguments can be omitted if none are needed:

-> <recipe>  # arity zero

To register a method to the scope, assign it to an identifier:

<identifier> = <arguments> -> <recipe>

The number of arguments in a method signature is fixed, and all arguments are mandatory. However, if you pass in excess arguments to the method, they will be ignored.

Arguments are treated as immutable entries within the method's execution scope. If you need optional or mutable arguments, you can pass in a scope as an argument, which can pack multiple "arguments". Alternatively, you can use a custom type with default values.


FatScript uses auto-return, meaning the last standing value is returned:

answer: Method = (theGreatQuestion) -> {
  # TODO: explain Life, the Universe and Everything

answer("6 x 7 = ?")  # outputs: 42

Auto-call trick

If a method is accessed inside a scope with dot syntax and it takes no arguments (arity zero), then it will be called "automagically":

foo = {
  bar = -> 'hey'
}  # returns 'hey'    # also returns 'hey' due to auto-call

Note: these methods can still be referred to without triggering the auto-call feature by using the get syntax:

foo('bar')  # yield a reference to the method

Implicit Argument

A convenience offered by FatScript is the ability to reference a value passed to the method without explicitly specifying a name for it. In this case, the implicit argument is represented by the underscore _.

Here's an example that illustrates the use of implicit arguments:

double = -> _ * 2
double(3)  # output: 6

You can use implicit arguments whenever you need to perform a simple operation on a single argument without assigning a specific name to it.

See also

results matching ""

    No results matching ""