Team LiB   Previous Section   Next Section
class

Allowed coercions

list with one item, as in {integer}
string

Syntax

set theClass to class of theString

Description

The class value type is used to describe the data type of a variable or object, such as: boolean, class, integer, real, record, or string. It is used most often to check the value type of a variable or return value:

set theString to "I am a string"

set theClass to class of theString -- returns string

Getting the class property of a string returns an object of type class, which is just the word string (or whatever the class is) without quotation marks. If you want to twist your tongue into further knots, follow the prior example with the statement:

get class of theClass

It returns, you guessed it, the term class with no quotation marks.

Examples

To make sure they are of the proper data type, check the class property of any parameters that are passed to functions:

on MultiplyByTwo(aNumber)

   if (class of aNumber is not in {integer,real}) then

      return 0

   else

      return aNumber * 2

   end if

end MultiplyByTwo

MultiplyByTwo(45)

MultiplyByTwo("woops")

The first call to the MultiplyByTwo function will result in "90." The second call will produce "0" because the parameter is a string, instead of the required integer or real value type.

The class of aNumber part of the previous example returns an object of type class (it will return integer from the first call to MultiplyByTwo and string from the second call). The segment:

(class of aNumber is not in {integer,real})

of the function call is a boolean expression. It will return false if the class of the parameter is either an integer or a real.

    Team LiB   Previous Section   Next Section