flofriday / Moose

🐐 A new fun programming language
MIT License
6 stars 1 forks source link

For each loop mutability #16

Closed flofriday closed 2 years ago

flofriday commented 2 years ago

The following code is allowed by the typechecker:

for i in range(10) {
    print(i)
}

Obviously it mutates i but the typechecker still allows this. We however should requirea mutable variable on the left side of in.

Jozott00 commented 2 years ago

Oh, I thought I made i immutable, so only the loop can internally change its value...

flofriday commented 2 years ago

It is true that i is immutable inside the loop, but still it feels like i is declared here and then mutated on each iteration. This is not how it is implemented but it I thing we should force it to be mutable.

I mean languages that are purely functional don't have for each loops for exactly this reason, because i would get mutated.

Jozott00 commented 2 years ago

It was mutable before... With the last commit I made it immutable. At least this is how Swift handles this

We could do it in a different way of course