I use an Enum like this:
LocationOption* = enum
locHere, locThere
and have then the following program construct:
case location
of locHere:
let msg = "I'm here!"
of loThere:
let msg = "You're there!"
echo &"And Shakespeare said: {msg}"
This doesn't compile, because msg is not in scope on the top level. However if I declare the variable msg on the top level, I have to declare it as mutable var and re-assign it in the case statement.
Is there a way to avoid this use of `var` and still declare `msg` it as a immutable reference (with `let`)?
Side note: This might be a trivial question, but I have forgotten a lot of Nim and want to find my feet back into it after more than a year of defection into Python...
You use an expression:
let msg =
case location
of locHere:
"I'm here!"
of loThere:
"You're there!"
Very cool! So elegant!
Now where you said it, it feels so obvious...
There are a few constructs just like that where analogy making from the constraints of other programming languages fail you. A similar example is try-except expressions. I.e., this works:
import tables
var t: Table[int, int]
let x = try: t[0] except: 1
The language is a tool.
No, tools disappear once the house has been built. A language really is building material. Connecting wood with stone is harder than putting stone on top of stone.