Using explicit typing in the library
In the first version of the Hello World library we didn’t use any explicit types. In this lesson we will update the library with:
- An explicit return type
- A typed variable that will hold the message to be returned
Adding a return type
The first change we will make is to define the return type of the handler.
In the initial version of the library the return type was not specified so was taken to be the general typeoptional any, meaning any value, including nothing, could be returned.
We want to ensure the sayHello
handler always returns a string so change the handler definition to specify String as the return type.
public handler sayHello() returns String
… handler code
end handler
Adding a typed variable
To ensure we return a String from the sayHello handler we will also update the handler code to store the message in a String variable and return the value of the variable.
Update the sayHello handler with:
- String variable declaration
- Command to update the value of the variable with the “Hello World!” message
- Return statement returning the value of the String variable
public handler sayHello() returns String variable tMessage as String put "Hello World!" into tMessage return tMessage end handler
Testing the Extended Library
Test that your library compiles and behaves correctly.
- Open the Extension Builder.
- Load the updated LiveCode Builder file.
- Click the Test button.
- Execute ‘put sayHello()’ in the Message Box.
The string returned by the library is displayed in the Message Box.