Values and types

Every primitive type PickleScript has, and the conversions that actually work.

vlatest

PickleScript is statically typed with type inference. Most values need no annotation, and the ones that do are usually there to break an ambiguity.

Statements are newline separated

There is no semicolon. A newline ends a statement:

fn main() {
    let a = 1
    let b = 2
    println(a + b)
}

Primitive types

Type Examples Notes
int 0, 42, -7 64-bit signed
float 1.5, 0.0, -2.25 64-bit IEEE 754
bool true, false
string "hi", """text""" UTF-8
byte 0–255 via bytes(s)

Integers and floats are separate types. int does not widen to float, so this is a compile error:

let b: float = 7

Annotate the literal, or convert explicitly:

fn main() {
    let b: float = 7.0
    println(b)      // 7

    let c = 7 as float
    println(c)      // 7
}

The diagnostic says int does not satisfy float; use as for an explicit conversion.

Numeric literals

A number is written in decimal, hex, binary or octal, and _ may be used as a digit separator anywhere in it. Scientific notation is available on floats:

fn main() {
    println(1_000_000)   // 1000000
    println(0xFF)        // 255
    println(0b1010)      // 10
    println(0o17)        // 15
    println(1.5e3)       // 1500
}

Suffixes are accepted and ignored

You may write i8, i16, i32, i64, u8, u16, u32, u64, f32 or f64 after a literal and the program will run:

fn main() {
    println(1i64)        // 1
    println(255u8)       // 255
    println(1.5f32)      // 1.5

    let n = 7i32
    println(n * 2)       // 14
}

The suffix does not select a type. PickleScript has one int and one float, so 1i8 and 1i64 are the same value, and a f32 suffix on an integer-looking literal does not turn it into a float. Treat suffixes as documentation for a human reader, or leave them off entirely.

The one real restriction is between int and float themselves. A literal does not automatically satisfy the other type's name, so bind with an explicit conversion or rely on the promotion that happens in an arithmetic expression:

fn main() {
    let small: int = 7
    let exact: float = 2.5
    println(small + exact)
}

The restriction applies to binding one type's literal to the other type's name, and to arguments of a generic function like min or max.

as? produces an optional

as fails the check when the cast is not allowed. as? is the same cast with the result wrapped in an optional, so x as? T has type T?:

fn describe(i: int) -> int {
    let f = i as? float

    match (f) {
        case some(x) -> { return 1 }
        case none -> { return 0 }
    }
}

fn main() {
    println(describe(7))
}

Read this one carefully, because it is not a try cast:

  • For a plain value the result is always some. There is no failure mode, so the none arm is unreachable in practice.
  • Applied to an already-optional value it nests: int? with as? float gives you an optional holding an optional.
  • The checker still refuses a cast it does not permit, even with as?. some_string as? int is E0363 cannot cast 'string' to 'int'. as? is not a way to probe whether a conversion is legal.
  • is is the operator for an actual test: x is T is a bool.

The clearest way to end up with an optional is to annotate the binding and use as, which keeps the type visible in the source:

fn main() {
    let i = 7
    let a: float? = i as float

    match (a) {
        case some(x) -> { println(x) }
        case none -> { println(0) }
    }
}

Char literals

A char is written in single quotes and holds exactly one character. A backslash escape is accepted, and a multi-character literal is a lexical error rather than a truncation:

fn main() {
    let a = 'x'
    let b = '\n'
    let c = 'Z'

    println(a)
    println(c)
    println(c as int)     // 90, the character's numeric value
}

char is distinct from a one-character string. s[0] on a string gives you a char, not a new string.

Chars are byte-valued underneath, so keep them inside ASCII. A non-ASCII literal such as 'π' parses, but printing it emits ? and c as int returns 63, the UTF-8 byte, not the Unicode scalar value.

Lists

A list literal is homogeneous; the element type comes from the first element.

fn main() {
    let nums = [1, 2, 3]
    let words = ["a", "b"]
    let mixed = [1 as float, 2.5]
    println(len(nums))     // 3
    println(len(words))    // 2
    println(len(mixed))    // 2
}

An empty literal [] has no element type to infer. Passing one where a List<int> is expected is a type error:

first([])

That reports type mismatch in argument: expected List<int>, found List<?>. Build the list from real values instead:

Maps

A map literal is {} with key: value entries, and its type comes from the first entry.

fn main() {
    let table: Map<string, int> = {"one": 1, "two": 2}
    println(table["one"])   // 1
    println(len(table))     // 2
}

The type annotation is optional when the literal is not empty, because both parameter types can be inferred:

fn main() {
    let ages = {"ada": 36, "alan": 41}
    println(ages["ada"])   // 36
}

Map methods are covered in Collections.

Tuples

A tuple is written (a, b) and read by destructuring:

fn main() {
    let pair = (1, "one")
    let (a, b) = pair
    println("{a} {b}")
}

Indexing a tuple with pair[0] is not part of the type system. Destructuring is the way, and a for loop will destructure the elements for you:

fn main() {
    let pairs = [("x", 1), ("y", 2)]
    for ((key, value) in pairs) {
        println("{key}={value}")
    }
}

Optionals

A ? after a type makes it optional: either a value or none.

fn main() {
    let a: int? = 1
    let b: int? = none
    let n = a ?? -1
    println(n)      // 1
    println(b ?? -1)   // -1
}

See Error Handling for unwrapping, propagation, and what happens when a none is not handled.

There are no type aliases

There is no type declaration. type UserId = int is a parse error, and a const is a value, not a type:

const MAX_RETRIES = 3

A const declaration is a value, not a type, and a named value at module level is a function:

fn maxRetries() -> int {
    return 3
}

fn main() {
    println(maxRetries())
}

Printing a collection

println does not print a list's contents. It prints the list's length:

fn main() {
    let nums = [1, 2, 3]
    println(nums)     // List(len=3)
}

To see the elements, iterate or index:

fn main() {
    let nums = [1, 2, 3]
    for (n in nums) {
        print("{n} ")
    }
    println("")
}

There is no null

There is no nil, null, or undefined binding. The absence of a value is none, and exit is the only way to stop a program on purpose.

Next steps