jakt icon indicating copy to clipboard operation
jakt copied to clipboard

Able to take mutable slice of immutable array

Open thislooksfun opened this issue 2 years ago • 1 comments

The following code is possible:

function main() {
    let foo = [1, 2, 3, 4]
    mut slice = foo[1..2]
    slice[0] = 5
    println("Foo is {} and the slice is {}", foo, slice)
}

This compiles and prints Foo is [1, 5, 3, 4] and the slice is [5]

I don't know whether this is a design choice that I can't find documentation for, but it really feels like a bug to me.

thislooksfun avatar Sep 05 '22 02:09 thislooksfun

It is the same with classes.

class Test {
    public a: i64
}

function main() {
    let a = Test(a: 1)
    mut b = a
    b.a++

    println("{}", a)
}

With the binding being immutable but not the object itself.

robryanx avatar Sep 08 '22 07:09 robryanx