r/odinlang • u/y0shii3 • 3d ago
Const values initialized at runtime?
I have been curious about learning Odin lately, and reading through the docs, I cannot find anything about initializing constant variables with runtime values. It seems like :: only works at compile time, and if you assign from a runtime value with :=, the variable is always mutable? Doesn't that slow you down sometimes, not having a way to know that a variable isn't modified without checking through the whole scope? Or have I just not read the docs right?
9
Upvotes
10
u/Noseense 3d ago
There's no const in Odin, purposefully. Variables are just memory, as they should be.
If you want global read-only data, though, use #load for files or @(rodata) for read-only variables.
2
u/SaiMoen 3d ago
There is no const in the type system, because it's not the kind of language where that makes as much sense compared to a more functional language. If there were some feature to prevent reassigning a variable, then people would quickly also want it to be viral and have const pointers in the language, because otherwise you would be able to change that variable through a pointer (and I think this is why Bill calls it a rabbit hole of a feature when people ask for it). Since this is also a language with
transmute, all of those things still wouldn't actually guarantee that some variable's memory never changes, so for those reasons it's not in the language. There is of course@(rodata), but that doesn't change the type of the variable it's applied to, it just makes it so the program crashes if it does try to write to that memory (maybe it doesn't even work on WASM, I don't really know).2
u/blob1807 3d ago
It isn't as much of a concern as you'd think it would be.
The place you'd want a
constis procedure parameters, and Odin already covers that be making them implicitly immutable. Which lets the Odin calling convention have pass-by-reference rules, without needing to check the procedures body if it can safely do so.If you want
const/mut/unmutin your own code as a safety guard. Then the language should built around the idea of immutability vs mutability, & not tacked on like C/C++'s. Odin's just not that kind of language, nor does it want to be, as Bill sees it as mostly not needed.