#Types being mutable by default

1 messages · Page 1 of 1 (latest)

plucky raven
#

This is more of a question, not quite asking for help.

When specifying types, I always find myself either prefixing most types with const, or, more frequently, forgetting to and eating a chain of compile errors saying "expected T, received const T" and having to go and prefix everything that's not supposed to be mutable with const either way.

Is there any reason for types not being immutable by default? Wouldn't it be better if one had to specify a type to be mutable if necessary instead of the inverse?

**turns out question was specifically about pointers

placid flame
#

it might have something to do with the fact that Zig's types follow a pattern where less-qualified can implicitly cast to more qualified

#

so T can implicitly cast to const T

#

but if immutability was the default,
T can not cast to mut T

#

that's just my speculation, though. that might not be the full reason, but that's just something i've heard

supple musk
#

Do you have a minimal example? I'm unsure what you mean by types being mutable. Types (other than pointers) do not have a mutable/immutable property. Are you talking specifically about pointers? I'm unware of a compiler error that looks like the one quoted other than for constness mismatch on pointers.

plucky raven
#

thanks