We can create variables using
:=
which both declares new variables and assigns values to them. The variable name goes on the left and the value goes on the right, e.g.
x := 42
Variables in Ra have some differences from other programming languages. One of the main ones is that we can create relation variables (relvars), e.g.
planet := {planet_name:str, orbit_period:float, gravity:float}{
["Mercury", 88, 0.38],
["Venus", 224.7, 0.91],
["Earth", 365.25, 1.00],
["Mars", 687, 0.38],
["Jupiter", 4331, 2.36],
["Saturn", 10747, 0.92],
["Uranus", 30589, 0.89],
["Neptune", 59800, 1.12],
}
planet
┌─────────────┬──────────────┬─────────┐ │ planet_name │ orbit_period │ gravity │ ├─────────────┼──────────────┼─────────┤ │ Earth │ 365.25 │ 1.00 │ │ Jupiter │ 4331.00 │ 2.36 │ │ Mars │ 687.00 │ 0.38 │ │ Mercury │ 88.00 │ 0.38 │ │ Neptune │ 59800.00 │ 1.12 │ │ Saturn │ 10747.00 │ 0.92 │ │ Uranus │ 30589.00 │ 0.89 │ │ Venus │ 224.70 │ 0.91 │ └─────────────┴──────────────┴─────────┘
And we can then use these variables in relation expressions (ignore the new syntax for now, it will be explained soon), e.g.
planet[orbit_period < 500]{planet_name}
┌─────────────┐ │ planet_name │ ├─────────────┤ │ Earth │ │ Mercury │ │ Venus │ └─────────────┘
Here are some things to note about variables in Ra:
- there is no nil, NULL or undefined: variables are given a value whenever they are created
- there are no pointers
- variable names beginning with an uppercase letter are global (so in a sub-routine they cannot be masked by a local variable)
- global relational variables (relvars) are persistent: their values survive Ra restarts
- global relvars are in the same folder and are known as a schema
- global relvars in a schema are subject to relational constraints stored in the schema
Constraint
relvar
Storage details
Ra persists global relvar values to disk files, currently as <table>
s in HTML files in the schema folder (the one Ra was started in).
Although storing data in HTML files has some space overheads, there are some benefits to using HTML:
- the format solves many storage issues (e.g. cross-platform encoding, nested tables)
- a browser can be used to view the raw data which handles many internationalisation and accessibility issues (e.g. unicode, right-to-left scripts)
- diff tools can be used to track changes
- version control tools can be used to record change history and revert to old snapshots
- version control tools can be used to distribute and backup the data
- a browser can show tables on any device, handling the scrolling etc., and can show nested relations
- a browser can handle a variety of fonts and styles, which can be customised with css
- it's future-proof: it will likely provide support for new data types
- it's future-proof: your data will be accessible 100 years from now