vtereshkov / umka-lang

Umka: a statically typed embeddable scripting language
BSD 2-Clause "Simplified" License
1.05k stars 53 forks source link

Structured constants are not constants #413

Closed vtereshkov closed 1 month ago

vtereshkov commented 3 months ago
type S = struct {
    x, y, z: int
}

const (
    a = [2]int{2, 7}
    b = []real{3.14, 6.28}
    c = S{7, 8, -3}
)

fn main() {
    a[1] = 6
    b[0] = -0.666
    append(b, 17.2)
    c.x *= 2

    printf("%v  %v  %v\n", a, b, c)   // [2 6]  [-0.666 6.28 17.2]  {x: 14 y: 8 z: -3}
}
vtereshkov commented 1 month ago

Local structured constants are especially weird:

fn f(depth: int = 0) {

    const a = [2]int{5, 6}
    if a[1] > 10 {
        printf("OOPS!\n")  // OOPS!
    } 

    if depth < 5 {
        a[1]++
        f(depth + 1)
    }
}

fn main() {
    f()
}
vtereshkov commented 1 month ago

Without the concept of a pointer to constant (like const T * in C/C++), structured constants cannot be made truly constant and useful. Go disallows such constants, and so do we.