On 7/1/2015 7:49 PM, Niall Douglas wrote:
Out of interest, what do you think of my free function ternary logic programming:
tribool t; if(true_(t)) { /* true */ } else if(false_(t)) { /* false */ } else if(unknown(t)) { /* other state */ }
Nobody here seemed to like it. I am looking for something the average programmer will notice immediately and not accidentally assume it's boolen logic going on here.
I'm jumping in in the middle of this thread, but are you purposefully naming the value--true, false, unknown--to make the programmer more aware of the ternary logic? Otherwise, one could do the following, which boost::tribool supports (I'm probably stating the obvious; please forgive me): tribool t; if (t) { /* true */ } else if (!t) { /* false */ } else { /* unknown */ } This reminds me of the original three-way IF statement in FORTRAN, except it was an arithmetic test for less-than, equal-to, and greater-than zero. Oh, and it was sort of a computed goto, so there's that. This is what it looked (looks?) like: IF (N) LTLABEL, EQLABEL, GTLABEL I suppose the literal translation to ternary logic and C++ would be something like this: if (t) true_statement; else false_statement; else unknown_statement; Yes, two else's! One would have to be careful with the curly braces, as always, but I don't think this would be ambiguous if added to the grammar. Or how about introducing a new keyword, such as "otherwise?" That should be enough to tip off a programming that they're not in Kansas anymore: if (t) true_statement; else false_statement; otherwise unknown_statement; And how about a quaternary operator while we're at it? t ? true_expression : false_expression : unknown_expression Paul