Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
January 19, 2007 Extending basic types - bool | ||||
---|---|---|---|---|
| ||||
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 |
January 19, 2007 Re: Extending basic types - bool | ||||
---|---|---|---|---|
| ||||
Posted in reply to inteja | 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. |
January 19, 2007 Re: Extending basic types - bool | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | 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
|
January 19, 2007 Re: Extending basic types - bool | ||||
---|---|---|---|---|
| ||||
Posted in reply to %u | "%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. |
January 19, 2007 Re: Extending basic types - bool | ||||
---|---|---|---|---|
| ||||
Posted in reply to %u | %u Wrote:
> 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.
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.
|
January 19, 2007 Re: Extending basic types - bool | ||||
---|---|---|---|---|
| ||||
Posted in reply to inteja | inteja wrote:
> %u Wrote:
>
>> 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.
>
> 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.
|
January 19, 2007 Re: Extending basic types - bool | ||||
---|---|---|---|---|
| ||||
Posted in reply to %u | %u wrote: > 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. 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. |
Copyright © 1999-2021 by the D Language Foundation