digitalmars.D.learn - Extending basic types - bool
- inteja (11/11) Jan 19 2007 How do I make my own boolean class that operates *exactly* the same as t...
- Frits van Bommel (20/36) Jan 19 2007 At the bottom of http://www.digitalmars.com/d/operatoroverloading.html:
- %u (6/8) Jan 19 2007 This was set up long before Walter implemented lazy evaluation. Now that...
- Jarrett Billingsley (5/10) Jan 19 2007 The operator overloading docs might also be referring to the a.b dot
- inteja (4/8) Jan 19 2007 But even if possible, wouldn;t this conflict with what Frits also said i...
- Johan Granberg (4/22) Jan 19 2007 Instead of overloading logical operator which can be confusing, couldn't
- Frits van Bommel (8/16) Jan 19 2007 I was mainly referring to the boolean operators listed there: !, &&, ||
How do I make my own boolean class that operates *exactly* the same as the basic D type bool, but with extended functionality? Primarily I'm interested in overloading && and || but this isn't possible in D. typedef and alias aren't sufficient as I need extra functionality. To use my own Bool class, currently I need the following syntax: Bool a = new Bool(true); Bool b = new Bool(false); Bool c = a.getValue() && b.getValue(); OR (by overloading functions with opCall): Bool c = a() && b(); Either of the above still seem counterintuitive. I just want to be able to use: Bool c = a && b; Thanks in advance. inteja
Jan 19 2007
inteja wrote:How do I make my own boolean class that operates *exactly* the same as the basic D type bool, but with extended functionality? Primarily I'm interested in overloading && and || but this isn't possible in D. typedef and alias aren't sufficient as I need extra functionality. To use my own Bool class, currently I need the following syntax: Bool a = new Bool(true); Bool b = new Bool(false); Bool c = a.getValue() && b.getValue(); OR (by overloading functions with opCall): Bool c = a() && b(); Either of the above still seem counterintuitive. I just want to be able to use: Bool c = a && b;At the bottom of http://www.digitalmars.com/d/operatoroverloading.html: ----- Future Directions The operators !, ., &&, ||, ?:, and a few others will likely never be overloadable. ----- The reason is likely that those operators already have a definition when used with a class instance. They convert null references to false and others to true. The reason it doesn't work for structs either is probably that Walter wants to keep the set of operators that can be overloaded the same for classes and structs. You can get pretty close to the syntax by using opAnd though (overloading & instead of &&). You might want to use a struct instead of a class though, since then it's an error to use &&, while for classes it will silently convert to bool as I said above. Also, both bools and structs are passed by value, while classes are passed by reference.
Jan 19 2007
Frits van Bommel Wrote:The operators !, ., &&, ||, ?:, and a few others will likely never be overloadable.This was set up long before Walter implemented lazy evaluation. Now that D has lazy evaluation it should be easy to make at least &&, || and ?: overloadable. The module .-operator is IMO a misconstruction. The !-operator as a prefix operator is a solely C-ish-negation; at least I have not seen it anywhere else. Thus there is no need to overload it ever. -- Anonymity is not a Crime
Jan 19 2007
"%u" <unknown dontsay.com> wrote in message news:eoqiqk$1qdv$1 digitaldaemon.com...Frits van Bommel Wrote:The operators !, ., &&, ||, ?:, and a few others will likely never be overloadable.The module .-operator is IMO a misconstruction.The operator overloading docs might also be referring to the a.b dot operator.Anonymity is not a CrimeVery clever, %u.
Jan 19 2007
%u Wrote:Frits van Bommel Wrote:But even if possible, wouldn;t this conflict with what Frits also said i.e. "The reason is likely that those operators already have a definition when used with a class instance."? If it is technically feasible, I guess we need to wait for clarification from Walter? inteja.The operators !, ., &&, ||, ?:, and a few others will likely never be overloadable.This was set up long before Walter implemented lazy evaluation. Now that D has lazy evaluation it should be easy to make at least &&, || and ?: overloadable.
Jan 19 2007
inteja wrote:%u Wrote:Instead of overloading logical operator which can be confusing, couldn't there be an opTrue operator or somthing similar merging several operators into one in the same way as opCompare.Frits van Bommel Wrote:But even if possible, wouldn;t this conflict with what Frits also said i.e. "The reason is likely that those operators already have a definition when used with a class instance."? If it is technically feasible, I guess we need to wait for clarification from Walter? inteja.The operators !, ., &&, ||, ?:, and a few others will likely never be overloadable.This was set up long before Walter implemented lazy evaluation. Now that D has lazy evaluation it should be easy to make at least &&, || and ?: overloadable.
Jan 19 2007
%u wrote:Frits van Bommel Wrote:It's a bad idea to change the meaning of currently-valid syntax.The operators !, ., &&, ||, ?:, and a few others will likely never be overloadable.This was set up long before Walter implemented lazy evaluation. Now that D has lazy evaluation it should be easy to make at least &&, || and ?: overloadable.The module .-operator is IMO a misconstruction.I was mainly referring to the boolean operators listed there: !, &&, || and ?:.The !-operator as a prefix operator is a solely C-ish-negation; at least I have not seen it anywhere else. Thus there is no need to overload it ever.There is if you want to make a class/struct that exactly mimics a bool.-- Anonymity is not a CrimeAs I commented before, pseudonymity is nicer. Though this sig may be a good way to distinguish different "%u"s :). At least, as long as only one person uses it.
Jan 19 2007