Files
wren/doc/site/core/fn.markdown
T

45 lines
1.1 KiB
Markdown
Raw Normal View History

2015-01-18 15:36:36 -08:00
^title Fn Class
^category core
A first class function—an object that wraps an executable chunk of code.
[Here](../functions.html) is a friendly introduction.
### new **Fn**(function)
2015-02-22 12:34:32 -08:00
Creates a new function from... `function`. Of course, `function` is already a
function, so this really just returns the argument. It exists mainly to let you
create a "bare" function when you don't want to immediately pass it as a [block
argument](../functions.html#block-arguments) to some other method.
2015-01-18 15:36:36 -08:00
:::dart
var fn = new Fn {
IO.print("The body")
}
2015-02-22 12:34:32 -08:00
It is a runtime error if `function` is not a function.
2015-01-18 15:36:36 -08:00
2015-03-27 07:43:36 -07:00
## Methods
2015-01-23 20:33:05 -08:00
### **arity**
The number of arguments the function requires.
:::dart
IO.print(new Fn {}.arity) // 0.
IO.print(new Fn {|a, b, c| a }.arity) // 3.
2015-01-18 15:36:36 -08:00
### **call**(args...)
2015-04-04 15:29:10 -07:00
Invokes the function with the given arguments.
:::dart
var fn = new Fn { |arg|
IO.print(arg)
}
2015-04-22 07:45:20 -07:00
fn.call("Hello world") // Prints "Hello world".
It is a runtime error if the number of arguments given is less than the arity
of the function. If more arguments are given than the function's arity they are
ignored.