Assignment Question
- Posted by Mario Steele <eumario at trilake.net> Feb 25, 2005
- 487 views
Allright, I know this may be a stupid question, but it actually has a purpose, cause this is really bugging me how the '$' operator messes up everything. Why do I say that? Well, follow these examples Example 1:
integer a a = 1
>From reading this, we know that 'a' is declared as a variable, and that variable is an integer. Plain and simple, right? Next step, we move to an assignment operation, where we are assigning '1' to 'a'. Now, there's more to it, then what I'm about to explain, but just hang in there, cause it'll get more intresting here in a sec. Internally, it's determined that we're assigning the value 1 to the variable namespace of 'a'. Between the decloration of the variable namespace, the assignment operator, and the actual value, there's a type-check done on '1' to ensure that it'll fit in 'a', EG: we make sure that '1' is an integer, and if it is, assign it to the variable, otherwise, we tell the user that they are trying to put in the wrong type of data, into the variable. Now, is this, or is this not correct? We move on to Example 2:
integer a a = 5 + 1
Now, this one is a bit more difficult, cause now, we're doing mathmatics in the assignment statement. But, the thing is here, how does the interpreter know that the value that 'a' should get, is '5 + 1', and not '6'? Well, like before, this isn't exact, but first, the RHS get's evaluated, since it's an expression, not a litteral. Meaning, that since it found the operator '+', it now knows that '5' and '1' needs to be added together, before finally assigning it to the variable namespace 'a'. Meaning now, that instead of simply just assigning, we actually need to evaluate what the programmer means when they do this. Next Example:
function GetValue() return 5 end function integer a a = GetValue()
Again, we have the basic decloration of the 'a' namespace as a variable, and we have an assignment. But, we come back to the same problem, how do we know that the user doesn't want to put 'GetValue()' into a? Cause the interpreter knows, that it's earlier declared as a Function, and again, it catches the fact, that it's a expression, that needs evaluated, before finally assigning the data to a. If 'GetValue()' was a procedure, it would get evaluated, and break out, cause no data is returned from 'GetValue()' as a procedure. And again, the interpreter has to evaluate the final return value, to ensure that the value can be put into the namespace, 'a', EG: it must be a integer. Final Example:
function GetValue(integer b) return b + 1 end function integer a a = GetValue(5)
This example, adds more complexity, but it remains the same, except for the fact, now GetValue has a Private Member Space, in which 'b' is assigned to, The interpreter knows, to assign 'b' the value of '5', cause of the order of the arguments. Meaning basically, that 'integer b' and '(5)' becomes 'integer b b = 5' And the interpreter knows that it can't return the litteral 'b + 1' from the call, cause it knows that 'b + 1' is an expression which was evaluated first, before returning it to the caller, which in this case is 'a = GetValue(5)'. Throughout all of these examples, the interpreter didn't need to evaluate 'a' or 'b', cause it allready knew they was variable namespaces, and it knew that they was an integer, only cause there was a need to assign data to it, and when it came down to it, the RHS had to be evaluated first, before the left hand side was. So, Rob, you stated before, that it's not documented the exact behavior of the interpreter, when you add something to a global sequence, from another routine, we just experimented, and it worked that way. Well, it worked that way, because it's an expression, and the interpreter knew that. It knew it needed to evaluate the expression first, before Type Checking the data, and finally assigning the data to the variable. The only difference now, is the fact that instead of us pulling the rug out from under you, you've pulled the rug out from under us. I guess my question is now, since this "feature" was implemented, are we to expect that the way the rest of the assignments are done, are going to change as well? Mario Steele P.S. Before anyone get's the wrong idea, I am not trying to beat down on the interpreter, or the language construct, or Rob for that matter, I am just pointing out a few things that are "Givens" in the Euphoria Language, that don't need to be documented, cause they are the Basic programming principals.