[ Content | View menu ]

The Maybe Operator

Mark Mzyk | December 26, 2007

I doubt I’m going to go off on a purely theoretical tangent like this often, but I’ve had lots of time lately for eating, drinking, and thinking, with not much else going on.

This discussion stems from several events coming together in my brain at once.  First was my previous PHP Insanity post, part I.  In it I talked about PHP’s comparison operators and how I find them lacking.  This lead to a a comment by Chuck that perhaps PHP should have a sort of equal operator, with the syntax ?= .  I call this the maybe operator.  I had even joked with a coworker of mine at work that PHP needed a maybe operator, although we didn’t develop a syntax for it.

All this lead me to wonder just what use would a maybe operator have, if it was built into a language.  The language likely wouldn’t be a binary language, but a ternary language, since there would now be three possible states.  This leads to the realm of ternary logic.

While binary systems rule the day, it is possible to build ternary systems, as a quick check on Wikipedia reveals.

A ternary system would perhaps have 0 = false, 1= true, 2 = maybe (or some other choice).  If might be an interesting exercise to build such a system into an existing language, such as Lisp or Python.  I don’t recommend PHP – it has enough of it’s own idiosyncrasies already and doesn’t need another.
So what uses would a ternary system have?  What benefits does it bring to the table?  Is there a compelling enough reason to build a language from scratch that would be optimized for such a system?  Perhaps not today, but what possibilities lie in the future?

I’m still musing on these ideas myself and don’t have any answers to offer at this time, but I’m curious what any one else thinks.  If you don’t want to comment on a ternary system in general, what do you think about the maybe operator?  Good, bad, or just plain ugly?