Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Coercions

We touched briefly on coercion of data types earlier in the chapter. Coercion is necessary whenever you attempt to perform an operation that is not designed for the type of data you’re feeding to the operator.

Some kinds of data can be coerced to almost anything. For example, a list containing a single item can be coerced to the item itself, no matter what its type. The reverse is also true: data of any type can be coerced to a list containing the original value as its only item. Each of the number types, real and integer, can be coerced to the other. When a real is coerced to an integer, the result is rounded off. (In older versions of AppleScript, a real could be coerced to an integer only if it had no fractional part.) An integer or real can be coerced to its string equivalent (not the word, but the digits and any decimal point enclosed in straight quotation marks) and a string of digits, with or without a decimal point, can be coerced to a real or integer. Dates can be coerced to strings, and vice versa. The string format depends on your settings in the Date & Time pane of System Preferences. Strings can be coerced to a list of words, and a list of words can be coerced to a string. Constants can be coerced to strings (this wasn’t always true). There are other rules too numerous to list here.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint