import os
type
EnvException = object of Exception
proc getVar(name: string): string {.inline, raises: [EnvException].} =
let result = getEnv(name).string
if len(result) == 0:
raise newException(EnvException, "%" & name & "% is not defined!")
try:
let
yygnu = getVar("YYGNU")
except EnvException:
quit(getCurrentExceptionMsg())
let
cp = yygnu & r"\cp.exe"
Please note the "let nature" of yygnu.
import os
type
EnvException = object of Exception
proc getVar(name: string): string {.inline, raises: [EnvException].} =
result = getEnv(name).string
if len(result) == 0:
raise newException(EnvException, "%" & name & "% is not defined!")
let
yygnu = try: getVar("YYGNU") except EnvException: (quit(getCurrentExceptionMsg()); nil)
let
cp = yygnu & r"\cp.exe"
And your defined let result won't be returned from proc, just use the predefined one.
@LeuGim: Thanks for pointing me to the "let result" line (and a practical solution).
As for "for now", are there any changes in sight?
As for my example: I hoped to be able to do sth. like this (looks better ;-):
try:
let
v1 = getVar(...)
v2 = getVar(...)
v3 = getVar(...)
except EnvException:
quit(...)
let
d1 = f(v1)
d2 = f(v...)
About "for now" - there were suggestions on the forum about settable only once variables, so that you can declare it undefined in the outer block and then assign to it once in some inner block; I don't know is such a feature supposed to appear in future versions or not.
The problem with your let-variables is that they are defined inside try and try opens new scope (like block; it is intended and quite expected behavior), so v1, ... exist only inside try block. Using var you needed to declare it outside the try and assign to it then inside the try. As let-variables are not assignable, you need to use expression forms of try/if, use helper procs or smth else.
What you can do in this particular case is something like the following:
template check(e: expr): expr =
try: e
except: quit(getCurrentExceptionMsg()); nil
let
v1 = check getVar("v1")
v2 = check getVar("v2")
v3 = check getVar("v3")
let d1 = f(v1)