Literal
Basic Literals
Int Literal
0, -0, 1, -1, 2, -2, 3, -3, ...
Ratio Literal
0.00, -0.0, 0.1, 400.104, ...
Note that the Ratio type is different from the Float type; the API is the same, but there are differences in the accuracy and efficiency of the calculation results.
If a Ratio literal has an integer or decimal part of 0, you can omit the 0.
assert 1.0 == 1.
assert 0.5 == .5
Note: This function
assertwas used to show that1.0and1.are equal. Subsequent documents may useassertto indicate that the results are equal.
Str Literal
Any Unicode-representable string can be used.
Unlike Python, quotation marks cannot be enclosed in '. If you want to use " in a string, use \".
"", "a", "abc", "111", "1# 3f2-3*8$", "こんにちは", "السَّلَامُ عَلَيْكُمْ", ...
\{..} allows you to embed expressions in strings. This is called string interpolation.
If you want to output \{..} itself, use \\{..}.
assert "1 + 1 is 2" == "\{1} + \{1} is \{1+1}"
Documentation comments are also treated as string literals, so string interpolation can be used. This is expanded at compile time. You will be warned if you embed an expression that cannot be determined at compile time.
PI = 3.14159265358979323
'''
S(r) = 4 × \{PI} × r^2
'''
sphere_surface r = 4 * PI * r ** 2
Exponential Literal
This is a literal representing exponential notation often used in academic calculations. It is an instance of type Ratio.
The notation is the same as in Python.
1e-34, 0.4e-10, 2.455+e5, 245e5, 25E5, ...
assert 1e-10 == 0.0000000001
Compound Literals
Each of these literals has its own documentation describing them separately, so please refer to that documentation for details.
Array Literal
[], [1], [1, 2, 3], ["1", "2",], ...
Tuple Literal
(), (1, 2, 3), (1, "hello", True), ...
Dict Literal
{:}, {"one": 1}, {"one": 1, "two": 2}, {"1": 1, "2": 2}, {1: "1", 2: True, "three": [1]}, ...
Record Literal
{=}, {one = 1}, {one = 1; two = 2}, {.name = "John"; .age = 12}, {.name = Str; .age = Nat}, ...
Set Literal
{}, {1}, {1, 2, 3}, {"1", "2", "1"}, ...
As a difference from Array literals, duplicate elements are removed in Set.
assert {1, 2, 1} == {1, 2}
What looks like a literal but isn't
Boolean Object
True and False are simply constant objects of type Bool.
True, False
None Object
None is a singleton object of type NoneType.
None
Range Object
Unlike Python's range, it can treat not only Int but also any object of type that allows comparisons (subtype of Ord, e.g. Str, Ratio, etc.).
assert 0..10 in 5
assert 0..<10 notin 10
assert 0..9 == 0..<10
assert (0..5).to_set() == {1, 2, 3, 4, 5}
assert "a" in "a".."z"
Float Object
assert 0.0f64 == 0
assert 0.0f32 == 0.0f64
This is a Ratio object multiplied by f64, a Float 64 unit object. You can also use f32 to indicate Float 32.
They are basically performant than Ratio, but may introduce errors.
assert 0.1 + 0.2 == 0.3
assert 0.1f64 + 0.2f64 != 0.3f64 # Oops!
Complex Object
1+2Im, 0.4-1.2Im, 0Im, Im
A Complex object is simply an arithmetic combination of an imaginary unit object, Im.
*-less multiplication
In Erg, you can omit the * to indicate multiplication as long as there is no confusion in interpretation. However, the combined strength of the operators is set stronger than *.
# same as `assert (1*m) / (1*s) == 1*(m/s)`
assert 1m / 1s == 1 (m/s)