forked from Mirror/wren
@ -19,13 +19,14 @@ Like other scripting languages, Wren has a single numeric type:
|
||||
double-precision floating point. Number literals look like you expect coming
|
||||
from other languages:
|
||||
|
||||
:::wren
|
||||
0
|
||||
1234
|
||||
-5678
|
||||
3.14159
|
||||
1.0
|
||||
-12.34
|
||||
<pre class="snippet">
|
||||
0
|
||||
1234
|
||||
-5678
|
||||
3.14159
|
||||
1.0
|
||||
-12.34
|
||||
</pre>
|
||||
|
||||
Numbers are instances of the [Num][] class.
|
||||
|
||||
@ -40,39 +41,44 @@ though.)
|
||||
|
||||
String literals are surrounded in double quotes:
|
||||
|
||||
:::wren
|
||||
"hi there"
|
||||
<pre class="snippet">
|
||||
"hi there"
|
||||
</pre>
|
||||
|
||||
A handful of escape characters are supported:
|
||||
|
||||
:::wren
|
||||
"\0" // The NUL byte: 0.
|
||||
"\"" // A double quote character.
|
||||
"\\" // A backslash.
|
||||
"\%" // A percent sign.
|
||||
"\a" // Alarm beep. (Who uses this?)
|
||||
"\b" // Backspace.
|
||||
"\f" // Formfeed.
|
||||
"\n" // Newline.
|
||||
"\r" // Carriage return.
|
||||
"\t" // Tab.
|
||||
"\v" // Vertical tab.
|
||||
<pre class="snippet">
|
||||
"\0" // The NUL byte: 0.
|
||||
"\"" // A double quote character.
|
||||
"\\" // A backslash.
|
||||
"\%" // A percent sign.
|
||||
"\a" // Alarm beep. (Who uses this?)
|
||||
"\b" // Backspace.
|
||||
"\f" // Formfeed.
|
||||
"\n" // Newline.
|
||||
"\r" // Carriage return.
|
||||
"\t" // Tab.
|
||||
"\v" // Vertical tab.
|
||||
</pre>
|
||||
|
||||
A `\u` followed by four hex digits can be used to specify a Unicode code point:
|
||||
|
||||
:::wren
|
||||
System.print("\u0041\u0b83\u00DE") //> AஃÞ
|
||||
<pre class="snippet">
|
||||
System.print("\u0041\u0b83\u00DE") //> AஃÞ
|
||||
</pre>
|
||||
|
||||
A capital `\U` followed by *eight* hex digits allows Unicode code points outside
|
||||
of the basic multilingual plane, like all-important emoji:
|
||||
|
||||
:::wren
|
||||
System.print("\U0001F64A\U0001F680") //> 🙊🚀
|
||||
<pre class="snippet">
|
||||
System.print("\U0001F64A\U0001F680") //> 🙊🚀
|
||||
</pre>
|
||||
|
||||
A `\x` followed by two hex digits specifies a single unencoded byte:
|
||||
|
||||
:::wren
|
||||
System.print("\x48\x69\x2e") //> Hi.
|
||||
<pre class="snippet">
|
||||
System.print("\x48\x69\x2e") //> Hi.
|
||||
</pre>
|
||||
|
||||
Strings are instances of class [String][].
|
||||
|
||||
@ -85,13 +91,15 @@ followed by a parenthesized expression, the expression is evaluated. The
|
||||
resulting object's `toString` method is called and the result is inserted in the
|
||||
string:
|
||||
|
||||
:::wren
|
||||
System.print("Math %(3 + 4 * 5) is fun!") //> Math 23 is fun!
|
||||
<pre class="snippet">
|
||||
System.print("Math %(3 + 4 * 5) is fun!") //> Math 23 is fun!
|
||||
</pre>
|
||||
|
||||
Arbitrarily complex expressions are allowed inside the parentheses:
|
||||
|
||||
:::wren
|
||||
System.print("wow %((1..3).map {|n| n * n}.join())") //> wow 149
|
||||
<pre class="snippet">
|
||||
System.print("wow %((1..3).map {|n| n * n}.join())") //> wow 149
|
||||
</pre>
|
||||
|
||||
An interpolated expression can even contain a string literal which in turn has
|
||||
its own nested interpolations, but doing that gets unreadable pretty quickly.
|
||||
@ -104,14 +112,16 @@ implements the `..` and `...` [operators][] to create them:
|
||||
|
||||
[operators]: method-calls.html#operators
|
||||
|
||||
:::wren
|
||||
3..8
|
||||
<pre class="snippet">
|
||||
3..8
|
||||
</pre>
|
||||
|
||||
This creates a range from three to eight, including eight itself. If you want a
|
||||
half-inclusive range, use `...`:
|
||||
|
||||
:::wren
|
||||
4...6
|
||||
<pre class="snippet">
|
||||
4...6
|
||||
</pre>
|
||||
|
||||
This creates a range from four to six *not* including six itself. Ranges are
|
||||
commonly used for [iterating](control-flow.html#for-statements) over a
|
||||
@ -119,10 +129,11 @@ sequences of numbers, but are useful in other places too. You can pass them to
|
||||
a [list](lists.html)'s subscript operator to return a subset of the list, for
|
||||
example:
|
||||
|
||||
:::wren
|
||||
var list = ["a", "b", "c", "d", "e"]
|
||||
var slice = list[1..3]
|
||||
System.print(slice) //> [b, c, d]
|
||||
<pre class="snippet">
|
||||
var list = ["a", "b", "c", "d", "e"]
|
||||
var slice = list[1..3]
|
||||
System.print(slice) //> [b, c, d]
|
||||
</pre>
|
||||
|
||||
Their class is [Range][].
|
||||
|
||||
|
||||
Reference in New Issue
Block a user