3 way if  
Author Message
Kent Paul Dolan





PostPosted: 2003-8-26 2:51:00 Top

java-programmer, 3 way if "Roedy Green" <email***@***.com> wrote:

> If you were to devise a syntax for 3 way if that gave you clauses to
> execute if the expression were positive, negative or zero what sort of
> syntax would you come up with? I find this pattern showing up all the
> time in writing Comparators.

Sure, because it is the output of the comparision functionality.

Trouble is it is too broad, you only need three values, not two ranges
and a central value.

I put a wrapper interface (named "Sortable") around the comparision
functionality so that it would emit specifically -1, 0, or +1, as
manifest constants named LESS_THAN, EQUAL_TO, and GREATER_THAN, on which
I can then use a simple, symmetric, and clean switch syntax.

Given enumerations, that would presumably be even easier, because then
you could exhaust the enumeration type to the satisfaction of the
compiler, and wouldn't have to include a "default" switch case, the only
wart in my current setup.

xanthian.



--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG