Boolean enumeration: Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
Line 16: Line 16:
==Declaring boolean variables==
==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:
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>
==Using booleans in If conditions==
The whole point of <var>Boolean</var> variables is to be able to test them for truth and, depending on the result, perform some processing. For example, if <var>%bool</var> is a <var>Boolean</var> variable, the following performs the contents of the <var>If</var> block if <var>%bool</var> is set to <var>True</var>:
<p class="code">if %bool then
</p>
Note that in this example, if <var>%bool</var> is not set (null), then the request would be canceled with a null object reference error.
==Using logical operators with booleans==
User language has three logical operators:
<ul>
<li>Not</li>
<li>And</li>
<li>Or</li>
</ul>
Strictly speaking, in User Language these operators actually operate on numeric values where 0 is treated as <var>False</var> and any non-zero value is treated as <var>True</var>. However, because of the importance of boolean logic in programming, <var>Boolean</var> variables or values can be used instead of numbers for these operators. A boolean <var>True</var> is treated as 1 and a <var>False</var> value is treated as 0. As such, boolean variables can be used in logical operations as in:
<p class="code">if not %bool then
</p>
or
<p class="code">if %bool and (%y gt %x) Then
</p>
==Automatic conversion of numbers to boolean==
In <var class="prod">Sirius Mods 8.1</var> and later, it is also possible to assign a number to a boolean variable. When this is done, the number 0 is converted to <var>False</var> and every other value is converted to <var>True</var>. This is especially useful for assigning the result of a comparison to a boolean:
<p class="code">%bool = %x gt %y
</p>
This assignment can, of course, be implicit when a method has a boolean parameter:
<p class="code">local subroutine foo(%ok is boolean)
...
%(local):foo(%x gt %y)
</p>
Of course, some methods or $functions return a 0 indicating success and a non-zero value indicating an error. So, just as one can do:
<p class="code">if $setg(''NAME', %name) then
</p>
one can also assign the result of a method or $function to a boolean:
<p class="code">%setgSuccess is boolean
...
%setgSuccess = $setg(''NAME', %name)
</p>
One note of caution. Numbers are [[Intrinsic classes|intrinsic variables]] and, as such string values are automatically converted to numbers. Any string that can't be converted to a number is treated as 0. So, if <var>%n</var> is declared as <var>Float</var>, the following:
<p class="code">%n = '1234.56'


<p class="code>%truth    is enumeration boolean
</p>
 
would result in <var>%n</var> containing the number <var>1234.56</var>. The following:
 
<p class="code">%n = 'Not a number'


</p>
</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
would result in <var>%n</var> containing the number <var>0</var>. The following:
 
 
 
<p class="code">%n = 'ten'


</p>
</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) -
would also result in <var>%n</var> containing the number <var>0</var>. Hopefully, this is not too surprising. In addition, it should not be surprising that the following:
 
 
 
 
 
 


            is enumeration boolean
<p class="code">%n = 'true'


</p>
</p>


is identical to


<p class="code">function foo(%n is float, %test is boolean) is boolean
 
would also result in <var>%n</var> containing the number <var>0</var>. However, if <var>%bool</var> is a boolean,
 
<p class="code">%bool = 'true'


</p>
</p>


would result in <var>%bool</var> containing <var>False</var> because when booleans are set from intrinsic variables, the intrinsic values are treated as numbers so 'true' was converted to the number <var>0</var> before being assigned to <var>%bool</var>.
==Using booleans==





Revision as of 03:04, 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

Using booleans in If conditions

The whole point of Boolean variables is to be able to test them for truth and, depending on the result, perform some processing. For example, if %bool is a Boolean variable, the following performs the contents of the If block if %bool is set to True:

if %bool then

Note that in this example, if %bool is not set (null), then the request would be canceled with a null object reference error.


Using logical operators with booleans

User language has three logical operators:

  • Not
  • And
  • Or

Strictly speaking, in User Language these operators actually operate on numeric values where 0 is treated as False and any non-zero value is treated as True. However, because of the importance of boolean logic in programming, Boolean variables or values can be used instead of numbers for these operators. A boolean True is treated as 1 and a False value is treated as 0. As such, boolean variables can be used in logical operations as in:

if not %bool then

or

if %bool and (%y gt %x) Then

Automatic conversion of numbers to boolean

In Sirius Mods 8.1 and later, it is also possible to assign a number to a boolean variable. When this is done, the number 0 is converted to False and every other value is converted to True. This is especially useful for assigning the result of a comparison to a boolean:

%bool = %x gt %y

This assignment can, of course, be implicit when a method has a boolean parameter:

local subroutine foo(%ok is boolean) ... %(local):foo(%x gt %y)

Of course, some methods or $functions return a 0 indicating success and a non-zero value indicating an error. So, just as one can do:

if $setg(NAME', %name) then

one can also assign the result of a method or $function to a boolean:


%setgSuccess is boolean ... %setgSuccess = $setg(NAME', %name)


One note of caution. Numbers are intrinsic variables and, as such string values are automatically converted to numbers. Any string that can't be converted to a number is treated as 0. So, if %n is declared as Float, the following:

%n = '1234.56'

would result in %n containing the number 1234.56. The following:

%n = 'Not a number'


would result in %n containing the number 0. The following:


%n = 'ten'


would also result in %n containing the number 0. Hopefully, this is not too surprising. In addition, it should not be surprising that the following:




%n = 'true'


would also result in %n containing the number 0. However, if %bool is a boolean,

%bool = 'true'

would result in %bool containing False because when booleans are set from intrinsic variables, the intrinsic values are treated as numbers so 'true' was converted to the number 0 before being assigned to %bool.



Using booleans

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.