Boolean enumeration: Difference between revisions
m (Redirected page to Enumerations#Using Boolean enumerations) |
mNo edit summary |
||
Line 1: | Line 1: | ||
The '''Boolean''' enumeration implements the standard logical paradigm of "true" and "false". | |||
While, in some sense, it is just like any other enumeration, because the concept of true and false is so basic to programming, <var>Boolean</var> variables are treated specially by User Language. | |||
==Boolean enumeration values== | |||
The system <var>Boolean</var> enumeration values are: | |||
<ul> | |||
<li><b>True</b> | |||
<li><b>False</b> | |||
</ul> | |||
However, like all other enumerations, a <var>Boolean</var> variable can also be unset so have a null value. While this can be useful in detecting references to unset variables, it can also be problematic in certain sitiuations. Use of the <var>Initial</var> clause on variable declarations and the <var>Default</var> clause on method parameter declarations can mitigate most of these issues, nevertheless it is important to keep in mind the possibility of a <var>Boolean</var> variable being null. | |||
==Declaring boolean variables== | |||
Boolean variables can be declared the same way any enumeration variables are declared, with the keyword <var>Enumeration</var> followed by the class name: | |||
<p class="code>%truth is enumeration boolean | |||
</p> | |||
However, because boolean processing is such a basic part of any programming language, <var>Boolean</var> variables can be declared without the <var>Enumeration</var> keyword: | |||
<p class="code>%truth is boolean | |||
</p> | |||
This is also true for method variables and results. For example, then following: | |||
<p class="code">function foo(%n is float, %test is enumeration boolean) - | |||
is enumeration boolean | |||
</p> | |||
is identical to | |||
<p class="code">function foo(%n is float, %test is boolean) is boolean | |||
</p> | |||
Many methods, for example the XmlDoc API <var>[[Exists (XmlDoc/XmlNode function)|Exists]]</var> function, return a <var>Boolean</var> enumeration. | |||
Unlike other <var class="product">Janus SOAP ULI</var> enumerations, <var>Boolean</var> enumeration values | |||
are usable as the condition in an <var>If</var> statement: | |||
<p class="code"> %recset is object recordSet in file sirfiled | |||
... | |||
find records to %recset | |||
rectype = 'FILE' | |||
end find | |||
... | |||
if %recset:isEmpty then | |||
print 'No records found!' | |||
end if | |||
</p> | |||
In the above example, the <var>[[IsEmpty (Recordset function)|IsEmpty]]</var> method returns a <var>Boolean</var> enumeration value. | |||
Strictly speaking, the <var>If</var> clause expects a numeric zero or non-zero value as | |||
its operand, but in this context <var class="product">Janus SOAP ULI</var> automatically converts | |||
a <var>True</var> to a 1 and a <var>False</var> to a 0. | |||
Other places where a <var>Boolean</var> value can be used, that is, where it is automatically | |||
converted to the 0 or 1 that <var class="product">User Language</var> expects are: | |||
<ul> | |||
<li>As a <var>Repeat</var> statement operand. | |||
<li>As an operand for a logical operator such as <var>Not</var>, <var>And</var>, or <var>Or</var>. | |||
This would usually be in an <var>If</var> or <var>Repeat</var> statement. | |||
</ul> | |||
You can also use <var>Boolean</var> literals in these contexts, but they must be | |||
completely qualified as <var>Booleans</var>: otherwise there is a syntactic | |||
ambiguity between the values <var>True</var> or <var>False</var> and | |||
fields by the name of <tt>True</tt> or <tt>False</tt>. | |||
The following is syntactically valid: | |||
<p class="code"> if %(boolean):true then | |||
print 'Truth be told' | |||
end if | |||
</p> | |||
Besides the [[#Common Enumeration methods|common enumeration methods]], the | |||
system <var>Boolean</var> enumeration has additional methods available, which are | |||
described in the following sections. | |||
===IsFalse function=== | |||
This function examines a <var>Boolean</var> enumeration and returns an integer (0 or 1) | |||
according to the <var>Boolean</var> value (<var>True</var> or <var>False</var>). | |||
====IsFalse syntax==== | |||
<p class="syntax"> <span class="term">%num</span> <span class="literal">= </span><span class="term">bool</span><span class="literal">:IsFalse</span> | |||
</p> | |||
====Syntax terms==== | |||
<table class="syntaxTable"> | |||
<tr><th>%num</th> | |||
<td>If specified, a number variable that is assigned the value <code>0</code> if the value of <var class="term">bool</var> is<var>True</var>, and <code>1</code> if <var class="term">bool</var> is <var>False</var>. | |||
</td></tr> | |||
<tr><th>bool</th> | |||
<td>A <var>Boolean</var> enumeration variable or an expression that results in a <var>Boolean</var> enumeration. | |||
</td></tr></table> | |||
===IsTrue function=== | |||
This function examines a <var>Boolean</var> enumeration and returns an integer (1 or 0) | |||
according to the <var>Boolean</var> value (<var>True</var> or <var>False</var>). | |||
====IsTrue syntax==== | |||
<p class="syntax"> <span class="term">%num</span> <span class="literal">= </span><span class="term">bool</span><span class="literal">:IsTrue</span> | |||
</p> | |||
====Syntax terms==== | |||
<table class="syntaxTable"> | |||
<tr><th>%num</th> | |||
<td>If specified, a number variable that is assigned the value <code>1</code> if the value of <var class="term">bool</var> is<var>True</var>, and <code>0</code> if <var class="term">bool</var> is <var>False</var>. | |||
</td></tr> | |||
<tr><th>bool</th> | |||
<td>A <var>Boolean</var> enumeration variable or an expression that results in a <var>Boolean</var> enumeration. | |||
</td></tr></table> |
Revision as of 02:34, 11 October 2012
The Boolean enumeration implements the standard logical paradigm of "true" and "false".
While, in some sense, it is just like any other enumeration, because the concept of true and false is so basic to programming, Boolean variables are treated specially by User Language.
Boolean enumeration values
The system Boolean enumeration values are:
- True
- False
However, like all other enumerations, a Boolean variable can also be unset so have a null value. While this can be useful in detecting references to unset variables, it can also be problematic in certain sitiuations. Use of the Initial clause on variable declarations and the Default clause on method parameter declarations can mitigate most of these issues, nevertheless it is important to keep in mind the possibility of a Boolean variable being null.
Declaring boolean variables
Boolean variables can be declared the same way any enumeration variables are declared, with the keyword Enumeration followed by the class name:
%truth is enumeration boolean
However, because boolean processing is such a basic part of any programming language, Boolean variables can be declared without the Enumeration keyword:
%truth is boolean
This is also true for method variables and results. For example, then following:
function foo(%n is float, %test is enumeration boolean) - is enumeration boolean
is identical to
function foo(%n is float, %test is boolean) is boolean
Many methods, for example the XmlDoc API Exists function, return a Boolean enumeration.
Unlike other Janus SOAP ULI enumerations, Boolean enumeration values are usable as the condition in an If statement:
%recset is object recordSet in file sirfiled ... find records to %recset rectype = 'FILE' end find ... if %recset:isEmpty then print 'No records found!' end if
In the above example, the IsEmpty method returns a Boolean enumeration value. Strictly speaking, the If clause expects a numeric zero or non-zero value as its operand, but in this context Janus SOAP ULI automatically converts a True to a 1 and a False to a 0.
Other places where a Boolean value can be used, that is, where it is automatically converted to the 0 or 1 that User Language expects are:
- As a Repeat statement operand.
- As an operand for a logical operator such as Not, And, or Or. This would usually be in an If or Repeat statement.
You can also use Boolean literals in these contexts, but they must be completely qualified as Booleans: otherwise there is a syntactic ambiguity between the values True or False and fields by the name of True or False. The following is syntactically valid:
if %(boolean):true then print 'Truth be told' end if
Besides the common enumeration methods, the system Boolean enumeration has additional methods available, which are described in the following sections.
IsFalse function
This function examines a Boolean enumeration and returns an integer (0 or 1) according to the Boolean value (True or False).
IsFalse syntax
%num = bool:IsFalse
Syntax terms
%num | If specified, a number variable that is assigned the value 0 if the value of bool isTrue, and 1 if bool is False.
|
---|---|
bool | A Boolean enumeration variable or an expression that results in a Boolean enumeration. |
IsTrue function
This function examines a Boolean enumeration and returns an integer (1 or 0) according to the Boolean value (True or False).
IsTrue syntax
%num = bool:IsTrue
Syntax terms
%num | If specified, a number variable that is assigned the value 1 if the value of bool isTrue, and 0 if bool is False.
|
---|---|
bool | A Boolean enumeration variable or an expression that results in a Boolean enumeration. |