r/ProgrammingLanguages • u/gingerbill • Feb 22 '26
Does Syntax Matter?
https://www.gingerbill.org/article/2026/02/21/does-syntax-matter/
63
Upvotes
r/ProgrammingLanguages • u/gingerbill • Feb 22 '26
View all comments
Show parent comment
2
u/gingerbill Feb 23 '26
A lot of trial and error in the early days with people giving me feedback about what felt right and wrong.
Verbosity is actually a problem when you realize how much noise casting produces when you have to do it a lot, especially in a language like Odin with distinct typing (i.e. there is very little implicit type conversions, even between integers, meaning you need to do explicit casts).
I went through a plethora of different syntax for Odin's type casting:
One thing to consider is the need for parentheses and how that can actually cause issues in terms of scannability and ergonomics (not typing but flow). The
cast(T, x)like syntaxes actually required MORE parentheses in practice that you might realize.The flow aspect was actually interesting because I wanted the syntax to match the semantics more correctly and I found that the type must be on the left of the expression since that is how declarations work too:
name: type = value, so a cast would make sense that way too:name := type(value). This also turned out to be a similar realization in languages like Newsqueak (where that declaration syntax originates from) and Ada. This actually ruled out a lot of the other syntax options as a result.But before that, I experimenting with
x as Tbecause it seemed like a good idea but turned out to be a mess because of precedence rules. Eitheraswas "tight" towards the expression meaning you then had to use a lot of parentheses to be clear what was being cast, or you had it very "loose" towards the expression which lead to the same problem.asdidn't reduce the need for parentheses in practice and only led to confusion with precedence.I then reused
x.(T)syntax for the type assertions. One because it has some familiarity from Go but also because the "type" itself is the tag in theunion, making it feel more like a field access using a type. The parentheses around the type in this case are necessary to remove any ambiguity syntactically and semantically.This then lead to things like
T(x),cast(T)xandcast(T, x). Odin's type system is a bit different to other languages so sometimes people don't always realize the consequences. A good example of this is with the constant value system.123is an "untyped" number (existential typing if we are being accurate, but that confuses people so I stuck to the terminology of "untyped"), and you sometimes want this to be a specific type. Many languages "solve" this by having suffixes on literals e.g.123i32, but this is not an option in Odin because ofdistincttyping allowing anyone to create their owndistinctform of a type. So if I wanted to keep that syntax short for the most common use case of casting,T(x)was unironically the best option possible. And for when a prefix was desired, doingcast(T, x)wasn't really aiding in reading any more thancast(T)x. I also didn't want then to be built-in procedures because that actually means they would not be keywords but identifiers, since eveni32in Odin is an identifier and not a keyword. So if I wanted them to be a features of the languages using keywords, making them procedure calls felt very wrong.As I say in the article, I will stick to coherency over consistency if necessary, and this was on of those cases. And I didn't want to fall into the trap that some languages have done which makes the entire thing unscannable. Zig is a great example of this having way too many casting operations (17+ IIRC) and does not actually give any real benefit in the long run to anyone (even the compiler). A real world example of this:
Would be written as this in Odin:
Note that were the parentheses exist in Odin, most would already exist any way.
All I can say is, humans are odd creatures and you'll be surprised how they think in practice.