Chip's Tips for Developers

Contains coding, but not narcotic.

Boolean operations mixin for Synergy/DE

April 6th, 2009 11:48:39 am pst by Sterling Camden

The new class syntax in Synergy/DE version 9 allows you to define a whole host of operators for your own classes.  These include the boolean operations of testing for True, LogicalNot (! or .not.), LogicalAnd (&& or .and.), LogicalOr (|| or .or.), and ExclusiveOr (.xor.).  Once you’ve defined these in your class, you can write statements like:


if (myobject) ...
if (!myobject)...
if (myobject1 && myobject2) ...

etc.

Unfortunately, just defining op_True doesn’t automatically make the other operations default to anything.  Using the .not. operator (!) on an object that only defines op_True will give a compiler error, because op_LogicalNot is not defined.  Likewise for the others.  Furthermore, in the case of the binary operators LogicalAnd, LogicalOr, and ExclusiveOr, you not only need to define operations in which both operands are members of your class, but also any other combination with different types of operators.  Otherwise:


if (myobject && some_function_that_returns_an_integer())

will get a compilation error.  Likewise, you also need to define different orders of operands.  It turns out that to get full coverage for primitives, you need to define operands for your class and integer, decimal, and alphanumeric (integer also handles boolean, and alphanumeric also handles string), in both orders.  Including the case of two operands from your own class, that’s seven versions of each binary operator!  There are three such operators, plus op_True and op_LogicalNot, for a total of 23 methods just to implement a consistent test for truth.

The downloadable code below provides an include module that will define all those methods for you, using the mixin pattern that I provided earlier.  Within your class definition, you must define BOOLEAN_CLASS as the name of your class (since Synergy/DE does not, as of version 9.1.5, offer any compile-time symbol for the class under compilation), then include Boolean.dbl.  The generated methods will call a member method named “truth” to determine whether your object is true or not — so you need to supply just that one routine.  For example:

public class myclass
.define BOOLEAN_CLASS myclass
.include "Boolean"

method truth, boolean
proc
    mreturn whatever_makes_me_true
endmethod

endclass

Given the code above, you can use an instance of myclass in any boolean expression except one in which the other operand is a member of a different class.  In the latter case, you still have to anticipate the operand mix and either add your own operators or invoke a member of the other class that returns a primitive instead.

 

 

 

Posted in SynergyDE | 2 Comments » RSS 2.0 | Sphere it!

2 Responses to “Boolean operations mixin for Synergy/DE”

  1. [...] Boolean operations mixin for Synergy/DEStaying DRY by combining 23 tests for truth into one.Tags: synergyde boolean mixin oop operators Tags: boolean, consulting, del.icio.us, development, expert, innovation, mixin, oop, operators, synergyde, techrepublic [...]

  2. [...] also includes the Boolean mixin, so you can use any Var as a boolean expression.  Standard DIBOL rules apply:  integer [...]

Leave a Reply

Better Tag Cloud