Luce
Luce BaseSourceLuciaOS

3. Literals

3.1 Boolean and absence#

true, false, and none. none takes its optional type from context and is never a universal null.

3.2 Numbers#

let count = 42
let mask = 1_000_000
let hex = 0xFF
let ratio = 0.5
let large = 6.022e23

An integer literal is an int and a literal with a point or an exponent is a float. There are no suffixes and no other widths. Underscores separate digits; based prefixes 0x, 0o and 0b are lowercase. A literal outside its type's range is a compile error. - before a literal is negation, and -9223372036854775808 is accepted. A float prints as the shortest decimal that reads back as it, with .0 after digits that have no point: print(0.1 + 0.2) shows 0.30000000000000004, print(5.0) shows 5.0, and 6.022e23 shows as written.

3.3 Text and bytes#

let name = "Ada"
let path = r"C:\temp"
let greeting = f"hello {name}, you are {age + 1}"
let block = """
    two lines
    of text
    """
let data = b"\x00\x01"

A str literal is UTF-8 with the escapes \\ \" \n \r \t \0 \u{HEX}. A raw literal r"..." has no escapes. A formatted literal f"..." interpolates any expression whose type has a display (§10.5), each field {expression} or {expression:spec} with a format specification (below); a { is written {{. A field's expression holds no brace of its own: a set or map literal is bound to a name first. A field is code, not text: a string inside it is written with plain quotes, f"{name if name != "" else "none"}". A formatted literal is one line: there is no triple-quoted form. A triple-quoted literal strips the common indentation of its lines. A bytes literal b"..." admits \xNN and is the only place a byte is spelled.

There is no character literal: a text of one scalar is a str of length one.

A format specification is Python's format mini-language, [[fill]align][sign][z][#][0][width][grouping][.precision][type]:

print(f"{1234.5:,.2f} {42:>6} {255:#x} {"total":*^11} {0.25:.0%} {-7:+05}")

prints 1,234.50 42 0xff ***total*** 25% -0007.

PartMeaning
fillany one scalar, written before an alignment; a space when not written
align< left, > right, ^ centred, = a number's padding put after its sign; a number aligns right and anything else left when not written
sign+ before every number, - before a negative one only (as when not written), a space before a positive one
za float that rounds to negative zero shows as zero
#the alternate form: 0b, 0o, 0x, 0X before an int in that base; a float's point kept
0zeros fill a number after its sign, as 0= does; for anything else, a fill of 0
widththe least width, in scalars
grouping, or _ between groups of three digits; _ groups the digits of b, o, x and X by four
.precisiondigits after the point for f, e and %, significant digits for g and for no type, the scalars kept of a display
typeof an int: d decimal (when not written), b, o, x, X, and c, the scalar the number names; of a float: f and F fixed, e and E with an exponent, g and G general, % a percentage in fixed; of anything else, s, its display

The specification is the text after the field's own :, the first : outside the field's brackets and strings, up to its }; it holds no field of its own and no escape. One that does not read as that grammar, or whose parts do not fit the value, is a compile error: a type letter of another type, a precision of an int, a sign, z, #, = or grouping of anything but a number. An int takes no float type, so {n:.2f} is written {float(n):.2f} (§6.2). Digits are rounded half to even on the exact binary value, as Python's are: f"{2.675:.2f}" is 2.67. A float with neither type nor precision shows its display where Python shows its repr (1e16, not 1e+16); a precision without a type is Python's general format with a digit always after the point. Anything but an int and a float is laid out from its display, bool included; widths and precisions count scalars. A c of a number that names no Unicode scalar traps.

3.4 Collections#

let primes = [2, 3, 5, 7]
let ages = {"Ada": 36, "Grace": 45}
let seen = {1, 2, 3}
let empty_map: map[str, int] = {}
let empty_list: list[str] = []

[...] is a list, {k: v, ...} a map, {v, ...} a set; {} is an empty map. The element type comes from context or from the elements, which must agree. A literal creates a new collection each time it is evaluated.

3.5 Tuples#

(1, "one") is a tuple; (1,) is a tuple of one; () is unit.