Serious First Steps In UserTalk Scripting
by Matt Neuburg
Author of the book Frontier: The Definitive Guide
We have several times had occasion to mention datatypes. Every value in UserTalk has some datatype. We've mentioned numbers of various kinds; we've talked about strings; we've distinguished string4s from strings, and characters from both; we've seen that even an outline is a datatype. It's time to take a brief run through the main datatypes UserTalk 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.
Furthermore, every scalar datatype comes with a verb that transforms values with other datatypes into that datatype. For instance, 4 is a number; "4" is a string. To get from the first to the second, you can say string(4). This is called coercion. Of course, not every datatype can be meaningfully coerced to every other datatype, but Frontier tries remarkably hard.
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 coerce it to the right datatype; this is called implicit 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 clear to Frontier how to turn the string "TEXT" into a string4, so it just goes ahead and does it without complaining.
However, such implicit 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.
You can learn the datatype of any object using the verb typeOf().
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.)
If you want to know the type of the binary's data, use the verb getBinaryType().
- Things with their own internal edit windows.
I'm talking here about things like outline, script, wptext, and table. 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 for your script to declare a local variable, use "new" to turn it into an outline, make it the target, and start editing it with "op" verbs!
- 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. UserTalk provides two constants, true and false, which are understood as booleans.
You might think the only things you can use after "if" are logical expressions which can clearly be evaluated as true or false. But in fact you can also use numbers (Frontier coerces them to booleans for you, using the rule that zero is coerced to false and everything else to true) or strings (the empty string, and "false", are coerced to false, and everything else to true); this is a common strategy with which many cool tricks are possible.
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 record.
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"}.
Records are lists where the individual items have names.
- Addresses
The nature of the address datatype has already been discussed.
- Binaries
The binary datatype is rather special; it 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 'GIFf', marking it as a GIF.
All text is by Matt Neuburg, phd, matt@tidbits.com.
For information about the book Frontier: The Definitive Guide, see my home page:
http://www.tidbits.com/matt
All text copyright Matt Neuburg, 1997 and 1998. ALL RIGHTS RESERVED.
No one else has any right to copy or reproduce in
any form, including electronic. You may download this material but you may not post it for others to
see or distribute it to others without explicit permission from the author.
Downloadable versions at http://www.ojai.net/matt/downloads/scriptingTutorial.hqx and http://www.ojai.net/matt/downloads/scriptingTutorial.zip.
Please do not confuse this tutorial with a certain other Frontier 5 tutorial based upon my earlier work.
This page created with
Frontier, 2/11/2000; 6:58:58 PM.