the strictVariables global property

I guess I don’t understand the purpose of this device. I read the blurb and it seems to be making a point of BIG vs. big (case-sensitivity of variable names).

I turned it on because it sounds like the kind of thing that can help me avoid shooting myself in the foot. (Like Perl’s ‘use strict’)

I have a Machine.script which is an object.

In another script I say:
put new Machine with (name:machineName) into m

the strictVariables complains about this:
StrictVariablesViolation: Variable ‘Machine’ used without being set

If I change my code to (quote Machine):
put new “Machine” with (name:machineName) into m

it works.

Why? And why does it matter? What mistake is this global property supposed keep me from making?

If it is case-sensitivity, then what case am I supposed to use with ‘new Machine’. Since, Machine is an instance of Machine.script, is it ‘new MACHINE’, ‘new Machine.script’, or what?


First, let’s clear one thing up right away: the strictVariables global property has nothing to do with capitalization. I guess you got that idea from its mention in Appendix A where capitalization of unquoted literals is discussed. A better (and more complete) description of the strictVariables property is found in Chapter 4.

To explain the value of the strictVariables property in a little more detail, imagine that you wrote this script:

set customerName to "Jason Jablonsky"
put "Hello " & the first word of cutsomerName into greeting
put greeting

Now, you expected this bit of code to display “Hello Jason”, so you may be confused when instead it displays “Hello cutsomerName”!

The problem (which you probably spotted already if you were reading carefully) is that the variable ‘customerName’ is misspelled on the second line, so the script accesses the ‘cutsomerName’ variable instead. Since that variable has never been defined, it is treated as an unquoted literal with its value being the same as its name.

In a real script, the problem may not show up until much later in the script, and may be hard to track down or diagnose. By setting the strictVariables to true, you can prevent the use of unquoted literals. The result in the example above is that you will get an error on the second line when you run the script, since ‘cutsomerName’ has not been assigned a value.

The downside to setting the strictVariables to true is that you can’t use unquoted literals at all. While most languages don’t allow unquoted literals, in SenseTalk they are used fairly frequently (often without you even realizing that’s what you’re doing). That’s what happened in your case.

The SenseTalk ‘new’ operator needs to be followed by an object identifier (usually the name of an object) that will be used in creating the new object. The name is allowed to come from a variable, so in your case the language interpreter treated Machine as a variable, which in turn was treated as an unquoted literal since you hadn’t put anything into Machine.

The result of all this is that setting the strictVariables to true caused an error on that line. You’ve already discovered that putting quotes around “Machine” solves the problem, and this is probably the correct solution in your case (either that, or turn the strictVariables off). You’ll have to decide whether the advantages to you of using strictVariables outweighs its disadvantages. Hopefully now you’ll at least have a better idea of what the tradeoffs are. :slight_smile:

P.S. Oh yes, you mentioned ‘use strict’ in Perl so I looked that up, and yes, it appears to be very similar in intent to SenseTalk’s strictVariables. The tradeoffs are a bit different, though, since SenseTalk lets you use unquoted literals which oftern turn out to be rather handy.
P.P.S. You may also want to look into the strictProperties global property, which does a similar service (or disservice!) for accessing object properties.