|Pliant talk forum
Discussion: Local variables
|Message posted by maybe Marcus on 2002/05/05 22:29:04
|I am now documenting what local and global variables are.
I could find in Patrice's tutorial a good definition for global variables,
but could not find one for local variables.
Could someone post a good definition?
|Message posted by maybe pom on 2002/05/06 05:29:35
|Local variables are defined inside functions, methods, and meta
using the var directive, as follows:
var type variableName [:= expression]
where 'type' is the type of the variable and 'variableName' is the name of the variable.
It is also possible to initialize the local variable
upon declaration, e.g., the instruction below initializes an integer local variable with value 10.
var Int x := 10
It is also possible to declare a local variable within a function/method/meta call:
for (var Int i) 1 10
mystring parse (var Int value)
Be aware local variable definition with initialization (var Int i := 3) may
not be used directly "inline" as a local variable:
do_something (var Int i:=3) # does not work: (var Int i:=3) -> Void
To "inline" the definition and initialization of a local variable, you have to use
do_something (var Int i:=3;i) # works, result of sequence (var Int i:=3;i) is i
|Message posted by maybe Hubert Tonneau on 2002/05/06 08:25:16
|I would say it differently, also it's not contradictory with pom statements:
In Pliant, you cannot use a variable without defining it's type:
i := 5
would implicitely declare 'i' as in integer in some languages, but in Pliant
So, you have to declare the variable first:
var Int i
i := 5
Now the trick is that 'var Int i' is also exactly the same as 'i',
so you can write things like:
var Int i := 5
You should see 'var Int i' as a more verbose way to say 'i' which also provides
a type for it so that Pliant will not reject the statement if 'i' is not
already declared as a local variable.
|Message posted by maybe Marcus on 2002/05/06 12:27:20
|I have a question: we know that local variables can only occurr inside a
function, method, or meta. Consequently, the following .pli program does _not_
var Str x:= "caca"
However, the following '.page' program _does_ compile and run:
var Str x:= "caca"
Why? Since the local variable 'x' does not occurr, at least explicity, inside
a function, method or meta.
|Message posted by maybe Hubert Tonneau on 2002/05/06 13:09:34
|Because a .page is a Pliant function.
|Message posted by maybe Marcus on 2002/05/06 13:37:19
|I have finished documenting 'variables'. Could you please check it out to see
if I am missing sth?
|Message posted by maybe pom on 2002/05/06 15:23:04
"You cannot define two variables with the same name within the same scope."
But it is possible to declare the same variable (with the same type & name)
more than once (the same variable is then used):
var Int i:=3
var Int i:=5 # same name, but ok: same type -> same variable -> same as "i:=5"
for (var Int i) 1 10 # same name & type -> same as "for i 1 10"
var Float i # error! same name, different type
|Message posted by maybe Marcus on 2002/05/06 18:14:31