www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Extending basic types - bool

reply inteja <inteja nosmalldreams.net> writes:
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
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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
parent reply %u <unknown dontsay.com> writes:
Frits van Bommel Wrote:
 The operators !, ., &&, ||, ?:, and a few others will likely never be 
 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
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"%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 Crime

Very clever, %u.
Jan 19 2007
prev sibling next sibling parent reply inteja <inteja nosmalldreams.net> writes:
%u Wrote:

 Frits van Bommel Wrote:
 The operators !, ., &&, ||, ?:, and a few others will likely never be 
 overloadable.


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.
Jan 19 2007
parent Johan Granberg <lijat.meREM OVE.gmail.com> writes:
inteja wrote:

 %u Wrote:
 
 Frits van Bommel Wrote:
 The operators !, ., &&, ||, ?:, and a few others will likely never be
 overloadable.

D has lazy evaluation it should be easy to make at least &&, || and ?: overloadable.

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.

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.
Jan 19 2007
prev sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
%u wrote:
 Frits van Bommel Wrote:
 The operators !, ., &&, ||, ?:, and a few others will likely never be 
 overloadable.


It's a bad idea to change the meaning of currently-valid syntax.
 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 Crime

As 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