Frontier Scripting Tutorial
About This Tutorial
What Does Frontier Do?
Keywords, Handlers, Verbs, and Calls
Loops, Variables, Parameters, and Conditionals
The Handler Rule
Returns, Addresses, and Dereferencing
Scope and With
Strings and Files
Outlines and Tables
Running, Debugging, and Getting Help
Datatypes
A Real-Life Problem
String Parsing and Substitution
Manipulating Files and Folders
Final Touches
|
|
Introduction
We have several times had occasion to mention datatypes. We've implicitly mentioned numbers of various kinds; we've talked about strings; we've distinguished string4s from strings, and characters from both; we've seen that an outline is a datatype. It's time to take a brief run through the main datatypes Frontier knows about.
Datatypes are important for several reasons. For one thing, as we've already seen, you can't use the verb "new" unless you know the datatype of the thing you want to create.
Also, a table entry always tells you what datatype it is (in the "kind" column), and it's nice to be able to appreciate what this means.
Coercion
More significant, most verbs are restricted as to the kind of datatype they work on; they are expecting each of their parameters to be of a certain datatype. If you feed a wrong datatype as a parameter, Frontier will make some attempt to translate it into the right datatype; this is called coercion.
For example, if a verb expects a string4 such as 'TEXT' it will not appear to you to be an error if you hand it instead a string, "TEXT"; that's because it's pretty to clear to Frontier how to turn the string "TEXT" into a string4, so it just goes ahead and does it without complaining.
However, such automatic coercion is often not possible, and an error can occur. Also, you need in general to know the datatype you're working with, especially as concerns the result returned from a verb call, so that it doesn't take you by surprise in some way.
Main datatypes
The main datatypes are as follows. (There are others; you can learn what they are and find out more about them from the documentation, but these are the main ones to keep in mind.)
- Things with their own internal edit windows.
I'm talking here about things like outline, script, wptext, and table. It's intuitively clear what these are and what they have in common, and they form a special group, the "non-scalars," as opposed to all other types which are called "scalars."
Note that a variable can be one of these types; it is perfectly legal to declare a local variable, use "new" to turn it into an outline, make it the target, and start editing it with "op" verbs! You can even open an edit window on it.
- Numbers.
The datatypes number, long, and short are integers; the first two are identical, and in general you should use "number" when you want to specify that something should be an integer.
The datatypes single and double are for decimal numbers (single-precision and double-precision floating-point); in general if you want to specify a decimal number you should use "double."
- Strings and characters.
The datatypes string, string4, and char (for single characters) we have already met; the first is written out delimited by double-quotes, the other two by single-quotes.
- Dates and times.
Both dates and times use the special datatype date; for instance, consulting the system clock with a call to clock.now returns a date, although you can't tell this if you call it from the Quick Script window because the result is automatically coerced to a string so you can read it. There are a lot of special date verbs to help you work with dates.
- Directions.
The direction datatype is what was fed as the second parameter to op.insert, when we used it above; it is used by other similar verbs.
In general you don't have to worry about this datatype explicitly because when you want to work with one you use the pre-defined constants up, down, left, right (and flatup, flatdown).
- Booleans.
The boolean datatype is what results from a logical test, such as you might ask Frontier to perform when you use the "if" construct. Frontier provides two constants, true and false, which are understood as booleans.
You might think the only things you can use after "if" are expressions which can clearly be evaluated as true or false, but in fact you can also use numbers because Frontier coerces them to booleans for you, using the rule that zero is coerced to false and everything else to true.
You can create a variable and assign it a boolean value, either by explicitly assigning it true or false or by assigning it the result of a logical test which you ask Frontier to perform for you; this makes it possible for the variable to be used in an "if construct" (that is, it is possible to say: local (myVar = false); if myVar...).
- Lists and Records.
Two special datatypes for containing other objects are list and records.
A list is an ordered series of items delimited by commas; the items themselves can be of any scalar type. It is written out delimited with curly braces; so, for instance, this is a list: {"hi", "there"}.
One great virtue of a list is that it allows a verb to return multiple values as its actual result, instead of using the trick where it is passed an address as one of its parameters, like dialog.getInt and others we've seen.
Records are lists where the individual items have names.
- Addresses
The nature of the address datatype has already been discussed.
- Binaries
Finally, we must note the binary datatype. This is rather a special type. The binary type allows a scalar to contain any kind of raw data.
As such, it really has two types: the scalar is of binary type, but its data has a type of its own. For example, when Frontier's Web capabilities are used to load a GIF file into the database, the file becomes a binary; that binary's data is of type 'gif' or 'GIFf', marking it as a GIF.
Working with datatypes
You can learn the type of any object by using the verb "typeOf"; but if the object is binary and you want to know the type of the binary's data, you have to use the verb "getBinaryType."
You can ask Frontier to convert from one datatype to another by calling a coercion verb. Such verbs generally have the name of the datatype to which you wish to convert. For example, you can turn the number 34 into a string representation (namely "34") by saying:
string (34)
On the other hand, when you actually talk about a datatype, you use a special name which ends in "type." If you ask in Quick Script
typeOf ("hello") == stringType
the reply will be "true."
|