1
0
forked from Mirror/wren
Files
wren/doc/site/values.markdown

90 lines
2.5 KiB
Markdown
Raw Normal View History

2013-12-04 07:46:41 -08:00
^title Values
2014-04-14 21:23:46 -07:00
^category types
2013-12-04 07:46:41 -08:00
Values are the built-in object types that all other objects are composed of.
They can be created through *literals*, expressions that evaluate to a value.
2015-01-03 23:27:02 -08:00
All values are *immutable*—once created, they do not change. The number
`3` is always the number `3`. The string `"frozen"` can never have its
character array modified in place.
2013-12-04 07:46:41 -08:00
## Booleans
A boolean value represents truth or falsehood. There are two boolean literals,
`true` and `false`. Their class is [Bool](core/bool.html).
2013-12-04 07:46:41 -08:00
## Numbers
2015-01-03 23:27:02 -08:00
Like other scripting languages, Wren has a single numeric type:
double-precision floating point. Number literals look like you expect coming
from other languages:
2013-12-04 07:46:41 -08:00
2014-01-20 21:44:51 -08:00
:::dart
2013-12-04 07:46:41 -08:00
0
1234
-5678
3.14159
1.0
-12.34
Numbers are instances of the [Num](core/num.html) class.
2013-12-04 07:46:41 -08:00
## Strings
Strings are chunks of text stored as UTF-8. Their class is
[String](core/string.html). String literals are surrounded in double quotes:
2013-12-04 07:46:41 -08:00
2014-01-20 21:44:51 -08:00
:::dart
2013-12-04 07:46:41 -08:00
"hi there"
2015-01-03 23:27:02 -08:00
A handful of escape characters are supported:
2013-12-04 07:46:41 -08:00
2014-01-20 21:44:51 -08:00
:::dart
2013-12-04 07:46:41 -08:00
"\"" // A double quote character.
"\\" // A backslash.
2015-01-03 23:27:02 -08:00
"\a" // Alarm beep. (Who uses this?)
"\b" // Backspace.
"\f" // Formfeed.
"\n" // Newline.
"\r" // Carriage return.
"\t" // Tab.
"\v" // Vertical tab.
2013-12-04 07:46:41 -08:00
2015-01-03 23:27:02 -08:00
A `\u` followed by four hex digits can be used to specify a Unicode code point.
2013-12-04 07:46:41 -08:00
2014-04-11 10:45:20 -07:00
## Ranges
2015-01-03 23:27:02 -08:00
A range is a little object that represents a consecutive range of integers.
They don't have their own dedicated literal syntax. Instead, the number class
implements the `..` and `...` [operators](expressions.html#operators) to create
them:
2014-04-11 10:45:20 -07:00
:::dart
3..8
2015-01-03 23:27:02 -08:00
This creates a range from three to eight, including eight itself. If you want a
half-inclusive range, use `...`:
2014-04-11 10:45:20 -07:00
:::dart
2015-01-01 22:48:43 -08:00
4...6
2014-04-11 10:45:20 -07:00
2015-01-03 23:27:02 -08:00
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
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:
2014-04-11 10:45:20 -07:00
2014-04-14 21:23:46 -07:00
:::dart
2014-04-11 10:45:20 -07:00
var list = ["a", "b", "c", "d", "e"]
var slice = list[1..3]
IO.print(slice) // ["b", "c", "d"]
Their class is [Range](core/range.html)
2013-12-04 07:46:41 -08:00
## Null
2015-01-03 23:27:02 -08:00
Wren has a special value `null`, which is the only instance of the class
[Null](core/null.html). (Note the difference in case.) It functions a bit like
`void` in some languages: it indicates the absence of a value. If you call a
method that doesn't return anything and get its returned value, you get `null`
back.