Elder reserves and specifies various types, literals, primitives, and constants as the use is generally so common that defying the conventions would lead to confusion.
Since we're starting with the most common concepts, notice that these are abstractions and don't represent the actual implementation. To keep it simple, more concepts will be introduced incrementally.
Not every type can be represented in every environment. If an implementation doesn't natively support the types there's a few ways to configure how the environment responds:
All that Elder syntax guarantees is that if it's defined, it must mean what is specified in Elder syntax standard. Not that it must be defined. For example, I32
(a 32 bit integer) doesn't have a native representation in JavaScript VM but it can be emulated using a bit of extra code the compiler can generate, default to a JavaScript builtin Number
, or throw a comptime error on use.
Although there are many builtin types, let's start with the most common:
Number
represents any numberIntegral
represents any integerDecimal
represents any floating pointRatio
represents any fixed pointList
represents a ordered series of itemsSet
represents a unique series of itemsMap
represents a series of key-value pairsString
represents a series of charactersOptional
represents a value which may not be presentBool
only can be values True
or False
We follow the conventions of many languages for the most basic literals. Each literal uses the default implementation for the environment it's running in.
The most common are:
[
is a List
literal Array
]
or at end of linelist = [ 1, 2, 3 ]
list = [ 1, 2, 3
list = [
1
2
3
{
is a Map
literal HashMap
=
}
or at end of linemap = { a = 1, b = 2, c = 3 }
map = { a = 1, b = 2, c = 3
map = {
a = 1
b = 2
c = 3
"
is a inline String
\
works as expected"
or at end of linestr = "my long name"
str = "my long name
str =
"my"
"long"
"name"
\n
str = "
my
long
name
str = "mylongname"
str = "my long name"
\n
(newline) str = "my\nlong\nname"
"""
is a block String
\
works as expectedstr = """
my
multil
name
str = "my\nlong\nname"
str =
"""
my
multi
name
"""
my
other
name
By default there are values which are interpreted as known types to avoid unnecessary visual noise like:
1.0
4
3/4
0xF00D
0b1010
0o744
True
for logical true, False
for logical falseSyntactically a type must start with a alphabetic, upper-case character and each new word of it's name should be upper-case like MyTypeName
.
Types get special treatement as they're so widely used and useful.
Lets start simple with an example:
x:(type = Integral) = 4
Notice that x
's type is literally represented by the key type
under the metadata relator :
. The key :type
, among others, has a reserved meaning in Elder syntax to mean the logical type of the name it's describing.
Since defining types is so common, we support sugar to minimize the syntactical noise:
x:Integral = 4
This only works for identifiers which can be interpreted as a types (meaning starting with upper-case alphabetic character) and is under the metadata relator :
.
It's still possible to add more metadata:
x:(Integral, min = 0, max = 10) = 4
or the equivalent expanded form:
x:(type = Integral, min = 0, max = 10) = 4
It is a syntax error to set both the type
and use the shorthand together like:
x:(Integral, type = Decimal) = 4
Using the syntax so far we have enough to emulate various serialization languages.
JSON is ubiquitous so let's see the difference.
Consider a somewhat simple JSON example:
[
{
"department": "History",
"staff": [
{
"name": "Jane Janison",
"is-tenured": true
},
{
"name": "Billy Bane",
},
{
"name": "Katie Kattson",
"is-assistant": true
},
]
},
{
"department": "Mathematics",
"staff": [],
},
{
"department": "Biology",
"staff": [
{
"name": "Dirk Darwin",
"specialization": "Evolutionary",
"teacher-assistants": [
]
},
{
"name": "Ellen Erickson",
"teacher-assistants": [
{
"name": "John Johnson"
}
]
},
],
},
We can recreate with Elder:
[
- department = History
staff = [
- name = Jane Janison
is-tenured = True
- name = Billy Bane
- name = Katie Kattson
is-assistant = True
- department = Mathematics
staff = []
- department = Biology
staff = [
- name = Dirk Darwin
specialization = Evolutionary
teacher-assistants = []
- name = Ellen Erickson
teacher-assistants = [
name = John Johnson
It's not perfect as there's still some repetition in the shape and structure of the data. Later on it can be simplified further.