Section 14.1. Implicit Coercion


14.1. Implicit Coercion

Implicit coercion is performed automatically when you supply a value where a value of another datatype is expected. This happens only in connection with AppleScript's operators. These operators have definite rules about what datatypes they expect, and what implicit coercions they will perform if other datatypes are provided. Details appear in Chapter 15.

Otherwise, AppleScript has no implicit coercion .

No implicit coercion takes place when assigning a value to a variable, because variables have no declared datatype; the variable simply adopts the new value.

No implicit coercion takes place when a parameter is passed to a user handler, because such handlers are not protected by any mechanism such as prototypes or datatype declarations in their definition from receiving parameters with undesirable datatypes. If your handler has reason to be choosy about what sorts of values it's willing to accept, then it needs to test those values and respond accordingly. For example, it might coerce explicitly, or it might throw an error, like this:

 on sendMeAString(s)     if {class of s} is not in {string, Unicode text} then         error "Can't make some data into the expected type."     end if     -- remaining code goes here, secure in the knowledge that s is a string... end sendMeAString 

Coercion might take place when passing a parameter to a command, but then the coercion is probably not being performed by AppleScript, and whether it is implicit becomes a moot point. For example:

 tell application "Finder"     set sidebar width of window 1 to "123" end tell 

You shouldn't have done that, but it works anyway. The sidebar width property is distinctly said, in the Finder's dictionary, to be an integer. You provided a string, but the Finder, instead of complaining, coerced it to an integer. This has nothing whatsoever to do with AppleScript itself! AppleScript did not look in the Finder's dictionary, see that the Finder expects an integer, and implicitly coerce the string. It sent the string to the Finder, and let you take your chances. You gambled, and luckily, this time, you won.

You should not, however, count on such behavior. For one thing, you have absolutely no way of knowing when it will work; a scriptable application's dictionary doesn't tell you (see "Coercions" in Chapter 20). The Finder's dictionary here says "integer," not "I'd like an integer, but if you're fool enough to send me a string, I suppose I'll coerce it for you." For another thing, it doesn't always work. For example, suppose you say this:

 tell application "Finder"     set name of folder 1 to 6     -- error: Finder got an error: Can't make some data into the expected type end tell 

Again, as you can see, AppleScript does not help out; it just sends the Finder what you said to send it. It is then up to the Finder to decide whether it's happy with what was sent. In this case, the Finder is not happy, and lets you know with an error.




AppleScript. The Definitive Guide
AppleScript: The Definitive Guide, 2nd Edition
ISBN: 0596102119
EAN: 2147483647
Year: 2006
Pages: 267
Authors: Matt Neuburg

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net