docs: update Wren documentation examples to use string interpolation and clarify fiber and method call semantics
- Replace string concatenation with interpolation in class method examples - Remove backticks from Fiber class references in concurrency docs - Add guidelines for choosing between getters and empty-parentheses methods - Clarify block syntax for single-expression functions and chaining
This commit is contained in:
@@ -107,17 +107,17 @@ Blocks of this form when used for method and function bodies automatically
|
||||
return `null` after the block has completed. If you want to return a different
|
||||
value, you need an explicit `return` statement.
|
||||
|
||||
However, it's pretty common to have a method or function that just evaluates
|
||||
and returns the result of a single expression. For that, Wren has a more
|
||||
compact notation:
|
||||
However, it's pretty common to have a method or function that just evaluates and
|
||||
returns the result of a single expression. Some other languages use `=>` to
|
||||
define these. Wren uses:
|
||||
|
||||
:::wren
|
||||
{ "single expression" }
|
||||
|
||||
If there is no newline after the `{` (or after the parameter list in a
|
||||
[function](functions.html)), then the block may only contain a single
|
||||
expression, and it automatically returns the result of it. It's exactly the
|
||||
same as doing:
|
||||
expression, and it automatically returns the result of it. It's exactly the same
|
||||
as doing:
|
||||
|
||||
:::wren
|
||||
{
|
||||
@@ -136,6 +136,14 @@ put a newline in there:
|
||||
}
|
||||
}
|
||||
|
||||
Using an initial newline after the `{` does feel a little weird or magical, but
|
||||
newlines are already significant in Wren, so it's not totally crazy. The nice
|
||||
thing about this syntax as opposed to something like `=>` is that the *end* of
|
||||
the block has an explicit delimiter. That helps when chaining:
|
||||
|
||||
:::wren
|
||||
numbers.map {|n| n * 2 }.where {|n| n < 100 }
|
||||
|
||||
## Precedence and Associativity
|
||||
|
||||
We'll talk about Wren's different expression forms and what they mean in the
|
||||
|
||||
Reference in New Issue
Block a user