Change from ‘const’ to ‘def’
Posted: October 25th, 2012 | Author: Mars | Filed under: Design, Progress, Syntax | 2 Comments »Radian offers two simple symbol types: var lets you define a symbol to which you can later assign a new value, while const is a definition which cannot later be changed. I had expected to make heavy use of const in Radian code since it echoes a pattern I use frequently in C or C++, but in practice I’ve found myself shying away from it. The reason is entirely superficial: it doesn’t feel right, because the values I would be assigning just aren’t constants. Instead, most of the consts I would define are intermediate values – things that will change on every invocation of the function or every pass through the loop, but which can remain unchanged once I’ve defined them. As such it just feels weird to call them constants, and so I tend to define them as var even if I have no intention of ever redefining them.
I still think that const has a good place; in fact I think that using it heavily is good style. I’ve decided therefore to rename it. Stealing a keyword from Python, “constants” are now “definitions”, using the keyword def. I’d avoided def since Python uses it for function definitions, specifically, while Radian functions use function, but sometimes one’s nice clean abstract ideas don’t pan out in practice.
It’s about time to freeze the syntax for a while. Aside from the half-finished regex literals, which are actually present in 0.6, I don’t see any further syntax changes on the horizon. All the upcoming work is in libraries and the toolchain.
I was struggling with this same question recently, and have started toying with a slightly different idea. I already use var for variables, and const for constants. But I was thinking of adding “ivar” for invariable — it’s not constant in that it doesn’t get replaced straight-away at compilation time, but it is not variable in that you can assign to it exactly once within the block in which it’s declared.
Not sold yet; seems like a pretty fine distinction. But an idea to noodle nonetheless.
Interesting. “ivar” makes me think more of “instance variable” than “i[n]var[iant]“, but it compares well to a pairing like “int” and “uint”.