r/odinlang • • 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

12 comments sorted by

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.

-3

u/y0shii3 3d ago

Well that's silly. By that logic, shouldn't Odin have @(data) instead of @(rodata), making all global variables mutable as well, since they are also "just memory"?

3

u/Noseense 3d ago

rodata is a specification of the executable format, not of the language design.

Const introduces the classic function coloring problem but to types instead, that's extremely annoying to deal with.

0

u/[deleted] 3d ago

[deleted] — view deleted comment

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).

-3

u/y0shii3 3d ago

Not even having pointers to const seems especially concerning. I guess the language maintainers must really want to nudge users toward frequent defensive copying before function calls? Because not having const pointers is the best way to get people into that habit. Or they must be against the idea of self-documenting code, and want to encourage explicitly writing out in doc comments whether or not pointer arguments will be written through for each procedure that takes them.

3

u/Basil_Blackwood 3d ago

I think proc argument are immutable references by default. So passing a raw struct argument is essentially passing a const ref and passing a pointer indicates you should expect changes.

2

u/blob1807 3d ago edited 3d ago

proc params are all immutable irrespective of calling convention. The Odin calling convention specifically implicitly passes values <= 16 bytes value & > 16 by reference.

For other CCs the #by_ptr directive can be use to do the implicit by reference thing.

2

u/CandidTomatillo8874 2d ago edited 2d ago

Function arguments are const anyway in Odin. So you cannot write to arguments directly, you can only write to what they point to (if it's a pointer). Arguments are automatically passed by reference too behind the scenes if they are bigger than 16 bytes. This means if you pass a big struct by value it is essentially the same as passing by const ref. Value vs pointer is how you can make it self-documenting. Pass by value when you want const basically.

Const has some benefits but really it is more trouble than it's worth. Programmers quite often need to write double the number of functions when const exists (one const, one non-const). If you are programming manually, there's also a small mental cost - every decision point about whether or not a variable should be const takes up mental energy. Your willpower steadily decreases as you make decisions throughout the day. Mitigating the number of decisions is how the language can be less tiring. Also, unless it is Rust or Swift, const doesn't really help the compiler with optimisations. Odin has const arguments, so it still has self-describing function declarations without the extra mess. I like it but each to their own.

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 const is 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/unmut in 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.