www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - && operator overloading

reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
In my C++ version I have implementation
of two functions/operators using rect (rectangle) struct

struct rect {}
// test for intersection of two rects
inline bool operator&& ( rect r1, rect r2 ) {...}
// intersection of two rects
inline rect operator& ( rect r1, rect r2 ) {...}

It seems that in D I can implement only opAnd....
Did I miss something?

How operator && works in D actually?
Is it sort of

bit result = cast(bit)left & cast(bit)right;

?

Andrew.
Mar 03 2005
parent reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
Being able to overload && and || is a bad thing in C++, because it 
screws short-circuit evaluation. I've never seen a case that was of such 
utility that it outweighed this.

AFAIR, D does not support this, and if it does/did, I expect there'll be 
lots of howling about it.

"Andrew Fedoniouk" <news terrainformatica.com> wrote in message 
news:d085h5$2jcj$1 digitaldaemon.com...
 In my C++ version I have implementation
 of two functions/operators using rect (rectangle) struct

 struct rect {}
 // test for intersection of two rects
 inline bool operator&& ( rect r1, rect r2 ) {...}
 // intersection of two rects
 inline rect operator& ( rect r1, rect r2 ) {...}

 It seems that in D I can implement only opAnd....
 Did I miss something?

 How operator && works in D actually?
 Is it sort of

 bit result = cast(bit)left & cast(bit)right;

 ?

 Andrew.







 
Mar 03 2005
next sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
So what's the solution in this instance?

What did the C++ operator&& do?
Was telling you if the rectangles overlap each other?

If so, is the solution to either write a method called "overlap" for the  
rectangle struct, or a stand-alone function to do it. eg.

struct rect {
   bool overlap(rect b) {}
}

bool overlap(rect a, rect b) {}

On Fri, 4 Mar 2005 10:17:46 +1100, Matthew  
<admin stlsoft.dot.dot.dot.dot.org> wrote:
 Being able to overload && and || is a bad thing in C++, because it
 screws short-circuit evaluation. I've never seen a case that was of such
 utility that it outweighed this.

 AFAIR, D does not support this, and if it does/did, I expect there'll be
 lots of howling about it.

 "Andrew Fedoniouk" <news terrainformatica.com> wrote in message
 news:d085h5$2jcj$1 digitaldaemon.com...
 In my C++ version I have implementation
 of two functions/operators using rect (rectangle) struct

 struct rect {}
 // test for intersection of two rects
 inline bool operator&& ( rect r1, rect r2 ) {...}
 // intersection of two rects
 inline rect operator& ( rect r1, rect r2 ) {...}

 It seems that in D I can implement only opAnd....
 Did I miss something?

 How operator && works in D actually?
 Is it sort of

 bit result = cast(bit)left & cast(bit)right;

 ?

 Andrew.
Mar 03 2005
next sibling parent reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
 So what's the solution in this instance?
Do you mean wrt rect intersections? One should write intersect() methods/functions. class rect { . . . public: bool intersect(rect const &rhs) const; . . . }; inline bool intersect(rect const &lhs, rect const &rhs) { return lhs.intersect(rhs); } The difference in discoverability of the source is (or should be!!) obvious: rect r1; rect r2; if(r1.intersect(r2)) { . . . or if(intersect(r1, r2)) { . . . vs if(r1 && r2) { . . .
 What did the C++ operator&& do?
Not sure what you mean here. I'll assume you're asking about the circumvention of short circuit evaluation bool b1; bool f(int); if( b1 && f(101)) { the function f() is _only_ called if b1 is true. If b1 is false, then f() is not called. However, when one overloads && in C++ _for any type_, both sides are evaluated, and short-circuit evaluation no longer applies. There is nothing from the syntax of the expression to suggest this important change in meaning. Imagine the consequences for template code!!
 If so, is the solution to either write a method called "overlap" for 
 the  rectangle struct, or a stand-alone function to do it. eg.

 struct rect {
   bool overlap(rect b) {}
 }

 bool overlap(rect a, rect b) {}
Indeed. With some const references .... ;)
 On Fri, 4 Mar 2005 10:17:46 +1100, Matthew 
 <admin stlsoft.dot.dot.dot.dot.org> wrote:
 Being able to overload && and || is a bad thing in C++, because it
 screws short-circuit evaluation. I've never seen a case that was of 
 such
 utility that it outweighed this.

 AFAIR, D does not support this, and if it does/did, I expect there'll 
 be
 lots of howling about it.

 "Andrew Fedoniouk" <news terrainformatica.com> wrote in message
 news:d085h5$2jcj$1 digitaldaemon.com...
 In my C++ version I have implementation
 of two functions/operators using rect (rectangle) struct

 struct rect {}
 // test for intersection of two rects
 inline bool operator&& ( rect r1, rect r2 ) {...}
 // intersection of two rects
 inline rect operator& ( rect r1, rect r2 ) {...}

 It seems that in D I can implement only opAnd....
 Did I miss something?

 How operator && works in D actually?
 Is it sort of

 bit result = cast(bit)left & cast(bit)right;

 ?

 Andrew. 
Mar 03 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
I have posted confusingly, you see, Q1 was directed at Matthew, but Q2 was  
directed at the OP.. I need to label things when I do that ;)

On Fri, 4 Mar 2005 10:30:54 +1100, Matthew  
<admin stlsoft.dot.dot.dot.dot.org> wrote:
 So what's the solution in this instance?
Do you mean wrt rect intersections?
Yep.
 What did the C++ operator&& do?
Not sure what you mean here. I'll assume you're asking about the circumvention of short circuit evaluation
I wasn't. I was asking wrt to rect intersections. Your explaination was welcome regardless, I see the problem now.
 If so, is the solution to either write a method called "overlap" for
 the  rectangle struct, or a stand-alone function to do it. eg.

 struct rect {
   bool overlap(rect b) {}
 }

 bool overlap(rect a, rect b) {}
Indeed. With some const references .... ;)
If I had my way 'in' would be const and pass by reference when appropriate. Regan
Mar 03 2005
prev sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
 What did the C++ operator&& do?
 Was telling you if the rectangles overlap each other?
Yes it is. // returns intersection rect operator&(rect r1, rect r2) { return (r1 & r2).is_empty(); } // do they overlap or not? bool operator&&(rect r1, rect r2) { return (r1 & r2).is_empty(); } // does r1 contain p2 or not? bool operator&&(rect r1, point p2) { return [...true/false...]; } // the same as previous bool operator&&(point p2, rect r1) { return [...true/false...]; } // union of two rects bool operator | (rect r1, rect r2) { .... return outline; } // region of parts not in r1 and not in r2 rect[] operator^ (rect r1, rect r2) { return ...; } etc.
 If so, is the solution to either write a method called "overlap" for the 
 rectangle struct, or a stand-alone function to do it. eg.

 struct rect {
   bool overlap(rect b) {}
 }
Sure I can do that. I also can use method array.item(int idx) instead of redefining of opIndex. I am not against this. I can understand arguments like short-circuit evaluation. If this is the case of omitting of && overloading then this is fine. Reasonable enough to keep compiler simple. Andrew.
 bool overlap(rect a, rect b) {}

 On Fri, 4 Mar 2005 10:17:46 +1100, Matthew 
 <admin stlsoft.dot.dot.dot.dot.org> wrote:
 Being able to overload && and || is a bad thing in C++, because it
 screws short-circuit evaluation. I've never seen a case that was of such
 utility that it outweighed this.

 AFAIR, D does not support this, and if it does/did, I expect there'll be
 lots of howling about it.

 "Andrew Fedoniouk" <news terrainformatica.com> wrote in message
 news:d085h5$2jcj$1 digitaldaemon.com...
 In my C++ version I have implementation
 of two functions/operators using rect (rectangle) struct

 struct rect {}
 // test for intersection of two rects
 inline bool operator&& ( rect r1, rect r2 ) {...}
 // intersection of two rects
 inline rect operator& ( rect r1, rect r2 ) {...}

 It seems that in D I can implement only opAnd....
 Did I miss something?

 How operator && works in D actually?
 Is it sort of

 bit result = cast(bit)left & cast(bit)right;

 ?

 Andrew. 
Mar 03 2005
prev sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
Matthew, I will agree with you if there (in D) would not such features
as operator overloading. But they are there. But not in full.

I am porting
http://www.terrainformatica.com/org/gool/geometry.h
which we are using in years. And & and && are make sense there
and help a lot. Trust me.

'&' and '&&' are semanticly different.
First one returns value of arguments type second one returns bool.

Compact representation is what makes languages different from

despite of the fact that these two are just two faces of the same stuff
these days.

Well... I can live without &&. I even can live without operator overloading 
at all.
Probably it makes sense to remove it from language? Or at least
move them out of the Cathedral?

 AFAIR, D does not support this, and if it does/did, I expect there'll be 
 lots of howling about it.
this? what about 'this'? Are you talking about ctor/dtor names in structs? Sorry, didn't get the meaning of this statement. Andrew.
 In my C++ version I have implementation
 of two functions/operators using rect (rectangle) struct

 struct rect {}
 // test for intersection of two rects
 inline bool operator&& ( rect r1, rect r2 ) {...}
 // intersection of two rects
 inline rect operator& ( rect r1, rect r2 ) {...}

 It seems that in D I can implement only opAnd....
 Did I miss something?

 How operator && works in D actually?
 Is it sort of

 bit result = cast(bit)left & cast(bit)right;

 ?

 Andrew.
Mar 03 2005
parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Andrew Fedoniouk" <news terrainformatica.com> wrote in message 
news:d0875e$2kuh$1 digitaldaemon.com...
 Matthew, I will agree with you if there (in D) would not such features
 as operator overloading. But they are there. But not in full.

 I am porting
 http://www.terrainformatica.com/org/gool/geometry.h
 which we are using in years. And & and && are make sense there
 and help a lot. Trust me.
They only help you because you know that the meaning of && has been subverted for elements of code with which you're familiar, and that you remain mindful of that by your constant exposure to it. The fact remains that any chance to the meaning of built-in operators is only 'workable' in a general sense if (almost) the entire community is aware of them _and_ accepts them. The two instances of which I'm aware are string concatenation in C++, Java, .NET and insertion/extraction operators in C++. (The latter stink terribly, but for other reasons than that they violate the principle.)
 '&' and '&&' are semanticly different.
 First one returns value of arguments type second one returns bool.
Of course. I have no (all-encompassing) objection to the overloading of &.
 Well... I can live without &&. I even can live without operator 
 overloading at all.
 Probably it makes sense to remove it from language?
No, but some of the current ones may need looking at.
 AFAIR, D does not support this, and if it does/did, I expect there'll 
 be lots of howling about it.
this? what about 'this'? Are you talking about ctor/dtor names in structs?
overloading &&. But like I said, only as far as I recall.
 Sorry, didn't get the meaning of this statement.

 Andrew.

 In my C++ version I have implementation
 of two functions/operators using rect (rectangle) struct

 struct rect {}
 // test for intersection of two rects
 inline bool operator&& ( rect r1, rect r2 ) {...}
 // intersection of two rects
 inline rect operator& ( rect r1, rect r2 ) {...}

 It seems that in D I can implement only opAnd....
 Did I miss something?

 How operator && works in D actually?
 Is it sort of

 bit result = cast(bit)left & cast(bit)right;

 ?

 Andrew.
Mar 03 2005