Hi Dave & Mark,
If those variable “type” fields are going to be binding in some contexts, perhaps we should make them binding in all contexts?
I’d like to point out again that the “type” field matters only when parsing the default value of the variable. The declared type of should_be_a_float_variable
in Dave’s example could be any of “integer”, “float”, “boolean”, or “string”, and the output of the experiment would be identical (seriously, try it), because the value of the variable is changed before it’s ever used.
As I recall, the reason why the “type” field is needed is that it was impossible to give a variable a default value of string type without it, e.g.
<variable tag="name" default_value="Chris" type="string" ...
Thinking about it now, it seems like this shouldn’t be necessary, since the expression parser is fully capable of recognizing string literals (see attached example). But for some reason I opted to rely on the “type” parameter, and if I thought about it long enough I’d probably remember why.
Anyway, regarding the specific issues at hand, I think there’s a simpler solution. The two examples of unexpected/confusing behavior cited in this thread (i.e. (bool)bool_var + 2
raising an exception, and 1/5
evaluating to 0) are both the result of design decisions in the STX expression evaluator. In my opinion, neither behavior is very useful. I don’t see any danger in treating boolean true and false as integer 1 and 0 (and off the top of my head, I can’t think of any programming language that doesn’t do that), so it seems pointless to disallow it. And I assume that most MWorks users would expect 1/5
to evaluate to 0.2; in the unlikely case that someone really wants truncating division, they can get it by casting the result of floating-point division to an integer.
So, why not just change the expression evaluator to eliminate those behaviors? That is, allow boolean true and false in arithmetic expressions, and change the division operation to always return a floating-point result (as is the case with division in Python 3). That would resolve these issues without requiring any changes to MWorks’ XML parser or the Variable and Datum classes.
What do you think? Is there some disadvantage to this approach that I’m not seeing?
Chris