Hacker News new | past | comments | ask | show | jobs | submit login

I guess this works like the Javascript `const` keyword.



Java too. Rust is the only language I’ve used where “immutable” means everything through the whole reference tree is also immutable. Well, only language that allows mutation at all.


And C/C++. Probably other value based languages like Go and Zig.

JavaScript probably behaves like that because `const` was tacked on (and it's dynamically typed so there's no place to mark parameters as const). There is Object.freeze() which sort of makes something immutable, but it's not really the same.

Java doesn't even have const so I guess they just didn't care about const correctness or didn't have time to add it.


In Go, consts can only be used for primitives like 42, "foo" etc. - not structs, because the language doesn't have a concept of immutability (and so wouldn't be able to enforce const-ness) except for primitives.

More info here: https://go.dev/blog/constants


Java has final, which I frequently find has junior dev advocates who believe it avoids changes that it doesn’t.


Afaik for C & C++, that's not true – you can turn const references into normal references and are even allowed to mutate the object they point to unless said object is declared as const.


Only through an escape hatch (a big red flag one at that). You aren't supposed to.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: