[ Team LiB ] |
8.1 Returned ValueWhen a handler is executed, it may return a value. By this we mean that it generates a value which becomes the value of the call that executed the handler. We may speak of the returned value as the result of the handler.
For example: on getRam( ) set bytes to system attribute "ram " return bytes div (2 ^ 20) end getRam The handler getRam( ) returns the amount of RAM installed on the user's machine, in megabytes. On my machine, it returns the number 384. This means that a call to getRam( ) can presently be used wherever I would use the number 384; in effect, it is the number 384. For example: on getRam( ) set bytes to system attribute "ram " return bytes div (2 ^ 20) end getRam display dialog "You have " & getRam( ) & "MB of RAM. Wow!" The call to getRam( ) in the last line behaves exactly as the number 384 would behave in this context: it is implicitly coerced to a string and concatenated with the other two strings (as explained under Section 15.5), and the full resulting string is displayed to the user. The value returned by a handler is determined in one of two ways:
If a handler returns no value, there is no error; but in that case it is a runtime error to attempt to use the call as if it had a value. The status of such a call is similar to that of a variable that has never been assigned a value (see Section 7.2). So, for example, there's nothing wrong with this: on noValue( ) return end noValue set x to noValue( ) After that, even if x was previously defined, it is now undefined. Thus an attempt to fetch its value will generate a runtime error: on noValue( ) return end noValue set x to 1 set x to noValue( ) set x to x + 1 -- error The result of a handler is volatile. It is substituted for the call, but the call itself is not storage (it isn't a variable), so the result is then lost. If you wish to use the result of a handler again later, it is up to you to capture it at the time you make the call. Of course, you could just call the handler again; but there are good reasons why this strategy might not be the right one:
So, for example, this code works, but it is very poor code: on getRam( ) set bytes to system attribute "ram " return bytes div (2 ^ 20) end getRam set s to "You have " & getRam( ) & "MB of RAM. Wow! " set s to s & getRam( ) & "MB is a lot!" display dialog s The handler is called twice to get the same unchanging result, which is very inefficient. The right way would be more like this: on getRam( ) set bytes to system attribute "ram " return bytes div (2 ^ 20) end getRam set myRam to getRam( ) set s to "You have " & myRam & "MB of RAM. Wow! " set s to s & myRam & "MB is a lot!" display dialog s The second version calls the handler and immediately stores the result in a variable. Now it suffices to fetch the value from the variable each time it is needed. The result of a handler may be ignored if the caller doesn't care about it (or knows that no value will be returned). In this case the handler is called entirely for its side-effects. Generally the call will appear as the only thing in the line: eraseMyHardDisk( ) |
[ Team LiB ] |