docs: recolor doc site with warm earth tones and update prose for clarity
This commit is contained in:
@@ -8,9 +8,7 @@ Functions are objects like everything else in Wren, instances of the `Fn` class.
|
||||
|
||||
Most of the time you create a function just to pass it to some method. For example, if you want to filter a [list](lists.html) by some criteria, you'll call its `where` method, passing in a function that defines the predicate you're filtering on.
|
||||
|
||||
Since that's the most common usage pattern, Wren's syntax optimizes for that. Taking a page from Ruby, a function is created by passing a *block argument* to a method.
|
||||
|
||||
At it's simplest, it looks like this:
|
||||
Since that's the most common usage pattern, Wren's syntax optimizes for that. Taking a page from Ruby, a function is created by passing a *block argument* to a method. At it's simplest, it looks like this:
|
||||
|
||||
:::dart
|
||||
blondie.callMe {
|
||||
@@ -19,7 +17,7 @@ At it's simplest, it looks like this:
|
||||
|
||||
Here we're invoking the `callMe` method on `blondie`. We're passing one argument, a function whose body is everything between that pair of curly braces.
|
||||
|
||||
Methods that receive a block argument take as a normal parameter. `callMe` could be defined like so:
|
||||
Methods that take a block argument receive it as a normal parameter. `callMe` could be defined like so:
|
||||
|
||||
:::dart
|
||||
class Blondie {
|
||||
@@ -47,7 +45,7 @@ Block arguments are purely sugar for creating a function and passing it in one l
|
||||
IO.print("Hi!")
|
||||
}
|
||||
|
||||
As you can see it takes a block argument too! All the constructor does it return that, so there's no special syntax here.
|
||||
As you can see it takes a block argument too! All the constructor does it return that, so this exists purely as a convenience method for you.
|
||||
|
||||
## Calling functions
|
||||
|
||||
@@ -73,9 +71,38 @@ Functions expose a `call` method that executes the body of the function. Of cour
|
||||
|
||||
## Function parameters
|
||||
|
||||
**TODO**
|
||||
Of course, functions aren't very useful if you can't pass values to them. The functions that we've seen so far take no parameters. To change that, you can provide a parameter list surrounded by `|` immediately after the opening brace of the body, like so:
|
||||
|
||||
**TODO: Implicit returns from short bodies.**
|
||||
:::dart
|
||||
blondie.callMe {|first, last|
|
||||
IO.print("Hi, " + first + " " + last + "!")
|
||||
}
|
||||
|
||||
Here we're passing a function to `greet` that takes two parameters, `first` and `last`. They are passed to the function when it's called:
|
||||
|
||||
:::dart
|
||||
class Blondie {
|
||||
callMe(fn) {
|
||||
fn.call("Debbie", "Harry")
|
||||
}
|
||||
}
|
||||
|
||||
It's an error to call a function with fewer or more arguments than its parameter list expects.
|
||||
|
||||
## Returning values
|
||||
|
||||
Function bodies return values just like methods. If the body is a single expression—more precisely if there is no newline after the `{` or parameter list—then the function implicitly returns the value of the expression.
|
||||
|
||||
Otherwise, the body returns `null` by default. You can explicitly return a value using a `return` statement. In other words, these two functions do the same thing:
|
||||
|
||||
:::dart
|
||||
new Fn { "return value" }
|
||||
|
||||
new Fn {
|
||||
return "return value"
|
||||
}
|
||||
|
||||
**TODO: Non-local returns?**
|
||||
|
||||
## Closures
|
||||
|
||||
|
||||
Reference in New Issue
Block a user