Language reference · section 15

15. Ranges, values, and operators

15.1 Ranges

Feature: Ranges

  Scenario: An inclusive range includes its end
    Given the code "[1..5]"
    Then the result is 1, 2, 3, 4, 5

  Scenario: An exclusive range excludes its end
    Given the code "[1...5]"
    Then the result is 1, 2, 3, 4

  Scenario: A range can have a step
    Given the code "[0..10 by 2]"
    Then the result is 0, 2, 4, 6, 8, 10

15.2 Values

Feature: Values

  Scenario: Indexing reads and writes one item
    Given the code "xs[i] = value"
    Then item i of xs is value

  Scenario: Spread expands an array in a literal or a call
    Given the code "[...xs, 1]" and "f(...xs)"
    Then the array holds every item of xs and then 1, and f receives the items of xs as arguments

  Scenario: Object spread copies fields, and later fields win
    Given the code "{...defaults, size: 3}"
    Then size is 3 and every other field comes from defaults

  Scenario: Object shorthand uses a name as key and value
    Given the code "{name}" with name is "Ada"
    Then the object is {name: "Ada"}

  Scenario: Object keys can be names or strings
    Given the code "{a: 1, \"b c\": 2}"
    Then the object has keys a and "b c"

  Scenario: Optional member access gives undefined for null
    Given the code "user?.address" with user is null
    Then the result is undefined

Array literals, call arguments, and object literals do not allow a trailing comma.

15.3 Operators

Kind Spelling
Arithmetic + - * / % **
Comparison < <= > >=, equal is, not equal isnt
Membership in
Logic and, or, not
Nullish ??
Optional member ?.
Pipe |>
Conditional if cond then a else b
Await await
Feature: Operators

  Scenario: Equality uses words
    Given the code "x is 4" or "x isnt 4"
    Then the code checks equal or not equal

  Scenario: The symbol == is an error
    Given the code "x == 4"
    Then the compiler reports an error "pin doesn't use `==` or `===`. Write `is` to test equality"

  Scenario: The symbol != is an error
    Given the code "x != 4"
    Then the compiler reports an error "pin has no `!=`; write `isnt` to mean not equal"

  Scenario: The symbol && is an error
    Given the code "a && b"
    Then the compiler reports an error "pin has no `&&`; write `and` to join two conditions"

  Scenario: The symbol || is an error
    Given the code "a || b"
    Then the compiler reports an error "pin doesn't use `||`. Write `or` to join two conditions"

  Scenario: The symbol ??= is not part of the language
    Given the code "x ??= 2"
    Then the compiler reports an error

  Scenario: A conditional expression gives a value
    Given the code "label = if age < 18 then \"minor\" else \"adult\""
    Then label holds the value of the branch that is true

  Scenario: The nullish operator gives a default for null or undefined
    Given the code "name ?? \"guest\"" with name is null
    Then the result is "guest"

  Scenario: in checks membership
    Given the code "x in xs"
    Then the code checks whether xs contains x, or whether x is a key when xs is an object

  Scenario: A chained comparison checks each step
    Given the code "0 < x <= 100"
    Then the code checks 0 < x and x <= 100 and evaluates x once