Article directory
Chapter 20 ObjectScript - Potential Pitfalls
The following items may be ObjectScript
confusing to new programmers, especially those responsible for maintaining code written by other programmers:
- Within a routine or method, each line must be indented by at least one space or tab unless it contains a label. That is, if there is any type of text at the first character position, the compiler
IDE
treats it as a label.
There is one exception: curly braces are accepted at the first character position.
There must be a space (not a tab) between the command and its first argument. IDE
Otherwise, you have a syntax error in your instructions:
Similarly, the terminal displays a syntax error as follows:
TESTNAMESPACE>write 5
WRITE 5
^
<SYNTAX>
TESTNAMESPACE>
ObjectScript
Operator precedence in an expression is strictly from left to right; operations within an expression are performed in the order in which they appear. You can use explicit parentheses in an expression to force certain operations to precede others.
Usually, parentheses are used even where they are not strictly required. Doing this is useful to other programmers (and later to yourself) because it makes the intent of your code clearer.
- For historical reasons, the empty string ( )
ObjectScript
is not considered equal to a value. To table values, use . ( Is a system function that returns a decimal-based character.)""
ASCII NULL
示ASCII
NULL
$CHAR(0)
$CHAR
ASCII
For example:
write "" = $char(0)
Similarly, when ObjectScript
a value is mapped to SQL
or XML
, the values ""
and $CHAR(0)
are treated differently.
ObjectScript
Some parts of are case-sensitive, while other parts are not. Case-insensitive entries include command names, function names, special variable names, namespace names, and user names.
Case-sensitive items include the names of most elements of the definition: routines, variables, classes, properties, and methods.
- Most command names can be represented in abbreviated form. Therefore,
WRITE
,write
,Write
,W
andW
are allWRITE
valid forms of commands. - For many commands, it is possible to include postconditional expressions (often simply called postconditions).
This expression controls IRIS
whether the command is executed. If the postcondition expression evaluates to true
(non-zero), IRIS
the command will be executed. If the expression evaluates to false
(zero), IRIS
the command is ignored and execution continues with the next command.
Set count = 6
Write:count<5 "Print this if count is less than five"
Write:count>5 "Print this if count is greater than five"
The previous content produces the following output: If count is greater than 5
then print this
Note: If postconditions are new to say, you may find the phrase "postcondition expression" somewhat misleading, as it (wrongly) indicates that the expression is executed after the command. Despite the name, postconditions are executed before the command.
- You can have multiple commands on one line. For example:
set myval="hello world" write myval
When doing this, note that if there are other commands on the line, you must use two spaces after any command that takes no arguments; otherwise, you will get a syntax error.
-
IF
The ,ELSE
,FOR
andDO
commands have two forms:- A newer form of block that uses curly braces to indicate blocks. For example:
if (testvalue=1) { write "hello world" }
ISC
It is recommended to use block form in all new code.- An older line-based form that does not use curly braces. For example:
if (testvalue=1) write "hello world"
-
Thanks to the preceding content,
ObjectScript
it can be written in a very compact form. For example:
s:$g(%d(3))'="" %d(3)=$$fdN3(%d(3)) q
The class compiler automatically generates compact code of the form shown above (although not necessarily using abbreviated commands as in this example). Sometimes it's useful to look at the generated code, track down the source of a problem, or understand how something works.
ObjectScript
There are no real reserved words in , so in theory there could be aset
variable named . However, it is prudent to avoid usingSQL
the names of commands, functions, reserved words, and certain system items;IRIS
Allocate a fixed amount of memory to hold the results of string operations. If the string expression exceeds the amount of allocated space,<MAXSTRING>
an error is generated.
For class definitions, string manipulation limitations affect the size of string properties. IRIS
A system object (called a stream) is provided that can be used when it is necessary to process strings that exceed this limit; in this case, the stream interface class can be used.