Data types

From Reia

Jump to: navigation, search

Contents

Numeric types

Two numeric types are available: Integers and Floats. Both consist of a sequence of digits. Floats are distinguished by the presence of a '.' character somewhere inside the digit sequence.

Integers and Floats are arbitrary precision and have no set range limitations.

Examples:

3      # integer
3.1415 # float

Booleans

The two boolean types are represented by the "true" and "false" keywords. These values are returned from any boolean comparison.

Examples:

true
false

Nil

The "nil" keyword is a placeholder for the absence of a value. In a boolean context it evaluates to false.

Example:

nil

Atoms

Atoms are like a freeform enumeration. Each atom has a unique value globally across the entire system. Atoms are declared by prepending the : character to an alphanumeric sequence, and may optionally contain underscores. The name of an atom must start with a letter. More complex atoms may be specified by quoting the sequence following the : character. Quoted atoms are not restricted and may start with any character you desire.

Examples:

:foo        # an atom
:foo_bar    # an atom with an underscore
:foobar_42  # an atom with an underscore and digits
:"Hi there" # a quoted atom
:" :-) "    # quoted atoms are unrestricted
:'this too' # single quotes are also acceptable

Strings

Strings are delimited by either single or double quotes. Quotes of one type are nestable inside the other.

>> ""          # empty string
>> 'foo'       # a single quoted string
>> "foo 'bar'" # a string nesting another

Strings can also be ''interpolated'' with fragments of Reia syntax enclosed in #{...} escaping:

 <pre>
>> (foo, bar, baz) = (1, 2, 3)
=> (1,2,3)
>> "foo and bar make: #{foo + bar} which equals: #{baz}"
=> "foo and bar make: 3 which equals: 3"

Lists

Lists are ordered sequences of values, enclosed with square braces: []. Lists are internally implemented as immutable singly linked lists, meaning access to a particular member is O(n). Entries in lists are delineated with commas.

Examples:

[]     # empty list
[42]   # a list with one value
[0,42] # a list with two values

Tuples

Tuples are fixed-length, immutable value sets, enclosed with parens: (). One way to think of tuples is as immutable arrays. Access to members of a tuple is constant time, as opposed to lists where accessing a member by index is O(n). Entries in tuples are delimited with commas.

However, parens can also be used to specify order-of-operation. Thus, (x) evaluates to x, rather than specifying a single-element tuple containing x. The special-case syntax (x,) specifies a single element tuple. () specifies an empty tuple.

Examples:

()     # empty tuple
(42,)  # tuple with one value
(0,42) # tuple with two values

Hashes

Hashes are simple key/value stores, enclosed with curly braces {}. The => delineates the key from the value. Entries in dicts are delineated with commas.

Examples:

{}                      # empty dict
{'foo' => 42}           # dict with one value
{:foo => 0, :bar => 42} # dict with two values

Binaries

Binaries are a data structure for storing and operating on bitfields, enclosed using a combination of angle and square brackets: <[...]>. Binaries are immutable and store data in a space efficient manner.

Binaries are specified as lists of integers, in the range 0-255.

Examples:

<[]>     # empty binary
<[0]>    # binary with one value
<[0,42]> # binary with two values

Modules

Modules are Reia's primary means of encapsulating code and begin with a capital letter.

Examples:

 Object
 Tuple
 Regex

Lambdas

Lambda expressions are declared by the "fun" keyword, which is followed by a parenthetical containing arguments, and curly braces or do/end keywords containing an expression. Variables bound in the parenthetical are available in the scope of the given expression, as are any variables in the surrounding lexical scope (i.e. outer variables are closed over by the lambda) The lambda returns result of last expression evaluated in its scope.

Lambda expressions are called by adding a parenthetical expression to variable bound to a Lambda.

Examples:

>> plus_two = fun(n) { n + 2 }
>> plus_two(2)
4

When using curly braces, the lambda expression is bound to a single line. For multi-line lambdas, the do syntax must be used instead:

>> plus_two_times_two = fun(n) do
..   n += 2
..   n * 2
.. end
..
#<Lambda -expr/5-fun-2- (module: erl_eval arity: 1>
>> plus_two_times_two(2)
8

Regular expressions

Regular expressions are Perl compatible and use the following syntax: %r/.../

The standard Perl modifiers 'i', 'm', 's', and 'x' may follow the second forward slash terminating the expression. (Note: not implemented yet)

Examples:

 %r//        # empty regexp
 %r/foo/     # a regexp that matches foo
 %r/[a-z]+/i # a regexp that matches strings of lowercase or uppercase letters