On Tuesday, 14 November 2006 at 13:04:20 UTC, Mariano wrote:
> Wouldn't it be nice to have an ''implies'' operator?
if(A -> B)
I know this can be written as
if(!A || B)
but the implication makes it far more clear for common day speach
if( check_boundaries -> a.length <= max_int )
process(a);
makes more sence than
if( !check_boundaries || a.length <= max_int )
process(a);
or
if( ! (check_boundaries && a.length > max_int ) )
process(a);
besides, I don't think the '->' notation should be a big problem for the parser, and the front end can easily convert this structure to the corresponding not + or/and.
There is, of course, still the issue of precedence, but I think it should match that of || and &&.
And since Walter likes Wikipedia links: http://en.wikipedia.org/wiki/ Logical_implication
Mariano
There is a cruel answer to this request.
For boolean type values, this operator exists in all C-like languages. The problem however only is, the arrow points into the wrong direction!
You might get what I'd like to say:
if (a implies b) { ... }
is
if (a <= b) { ... }
in C-derived languages. I wouldn't call Java to be C-derived.
It's opposite to logic which would write
if (a => b) { ... }
to mean the same thing.
You also could write
if (b >= a) { ... }
if (!a >= !b) { ... }
if (!(a > b)) { ... }
If you would define an "implies" operator like which is available in current generation programming languages then the definition would look like:
a => b := !(cast(bool)a >= cast(bool)b)
Funny enough though, the IEC logic gate symbols use >= 0
to denote the OR-gate.
The bi-implication works better:
if (a == b) { ... } // eqv. to if (a >= b && a <= b) { ... }
if (a != b) { ... } // negated bi-implication
We would need to get used to writing the arrow in the wrong direction! Probably possible, but I find the confusion pretty terrible. At least, there is a mnemonic to say, that the symbol =>
does not exist and therefore <=
is reused instead and implication always is from left to right (in my university it really is).
Or we use this close notation instead:
if (a ? b : true) { ... }
The best syntax for an implication probably would be
if (a ? b) { ... }
and less
if (a !| b) {} := if (!a || b ) { }
which is equivalent.
Only dumb that the null-coalescing operator if (a ?? b) ...
in other languages already has the meaning if (!a ? b) ...
(which is equivalent to the or
operator like in Python).
My guess is though, that rather C++ would come up with something like =>
than D would. C++ now introduced the spaceship operator which is useful when working with comparators:
a <=> b := (int(a > b) - int(a < b))
(Looks like >-<
would have described the meaning better.)
A last solution would be adding this to your global utility functions:
```
pragma(inline) pure @safe @nogc nothrow
bool implies(in bool a, in bool b) {
return a <= b;
}
unittest {
static assert(false .implies (false));
static assert(false .implies (true));
static assert(! true .implies (false));
static assert(true .implies (true));
}
```
The operator precedence is still wrong.