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

45 lines
1.2 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.
2015-07-10 09:18:22 -07:00
### Fn.**new**(function)
2015-01-18 15:36:36 -08:00
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
:::wren
2015-07-10 09:18:22 -07:00
var fn = Fn.new {
2015-09-15 07:46:09 -07:00
System.print("The body")
2015-01-18 15:36:36 -08:00
}
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.
:::wren
2015-09-15 07:46:09 -07:00
System.print(Fn.new {}.arity) // 0.
System.print(Fn.new {|a, b, c| a }.arity) // 3.
2015-01-23 20:33:05 -08:00
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.
:::wren
2015-07-10 09:18:22 -07:00
var fn = Fn.new { |arg|
2015-09-15 07:46:09 -07:00
System.print(arg)
2015-04-04 15:29:10 -07:00
}
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.