Rationale for not allowing overload of && and ||?
bearophile
bearophileHUGS at lycos.com
Wed Jun 18 13:57:28 PDT 2008
Joe Gauterin Wrote:
> What is the rationale for not allowing overloading of && and ||? It seems to me that, with the 'lazy' keyword, D is one of the few languages where overloaded && and || can correctly implement short circuited evaluation.
Maybe you are talking about the lazy or/and of languages like Python. I have created lazyAnd(), lazyOr() in my libs, and I think I like them more explicitly named, to avoid confusion. They are shaped like:
Types[0] lazyAnd(Types...)(lazy Types items) {
// some static asserts here to avoid some problems
foreach (el; items[0 .. $-1])
if (not(el))
return el;
return items[$-1];
}
Where not() is a boolean that is true/false if the item is 0, 0.0, null, an empty array, an empty set, empty AA, something with length zero, etc etc.
Bye,
bearophile
More information about the Digitalmars-d
mailing list