The runtime constant in forward declaration

Viewed 55

How do we do runtime constant in forward definition/declaration by any way around (tried hard not work on using let)

let n :int

proc m : int =
 let i=1
 var u=n+i

n=m()

error for this or for other varied ones:

Error: 'let' symbol requires an initialization

Error: redefinition of 'n'; previous

... etc Please solve it out, thanks before

1 Answers

It's hard to say what you are trying to do with this code, a let variable is treated as a constant, so it can't be modified, but presumably your intent is to use the variable as a sort of counter to increase itself and presumably reassign n multiple times by calling m()? If you remove the n from the proc you can write the code like:

proc m : int =
 let i=1
 var u=0+i

let n :int = m()

So if you actually want n to be mutable, there's no problem using a var:

var n :int

proc m : int =
 let i=1
 var u=n+i
 u

n=m()
echo n

Note that I had to add u as a last line to the m proc, because otherwise you were returning nothing and the assignment to n would always be zero, which is the default value for the implicit result variable inside a proc that returns something. You can verify this by repeating the n=m() assignment before the last echo.

Related