Check if exactly one of a set of conditions is met

Raymond Chen seems to think that this technique of testing whether exactly one of a set of conditions is met is a neat idea, so who am I to disagree? Here's a conversion to BBC BASIC:

      Widgets = 0
      Gadgets = 1
      Doodads = 4
      Sockets = 4

      IF FNDoesExactlyOneConditionApply THEN
        PRINT "Exactly one condition applies"
      ELSE
        PRINT "Fewer or more than one condition applies"
      ENDIF
      END

      DEF FNDoesExactlyOneConditionApply
      LOCAL t() : DIM t(2)
      t() = (Widgets = 1), (Gadgets = 1), (Doodads > Sockets)
      = (SUM(t()) = TRUE)

Why (and if) it's better than just adding the conditions together, as one of the commenters suggests, I'm not sure:

      DEF FNDoesExactlyOneConditionApply
      = (Widgets = 1) + (Gadgets = 1) + (Doodads > Sockets) = TRUE

Something about this not being allowed in a 'type-safe' language? I suppose storing Booleans in an array may be more 'legitimate' than adding them together (they're not strictly numbers after all) but I'm out of my depth here.
Sign In or Register to comment.