www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - binary expression...

reply captain_fid <bell.hue gmail.com> writes:
Please forgive if asked before. My google skills seemed to fail 
me and didn't see any result from search.

My problem is simple (though not my understanding LOL).

struct D {
       int value;
       bool opEquals()(bool value) const { return (value == 
value); }
}

D aD;
if (aD == 1) { // OK
}

if (aD) {      //  Error: expression aD of type D does not have a 
boolean value
}

Is there a way to overload for this? What am I missing?
May 21 2016
next sibling parent reply vit <vit vit.vit> writes:
On Saturday, 21 May 2016 at 18:10:55 UTC, captain_fid wrote:
 Please forgive if asked before. My google skills seemed to fail 
 me and didn't see any result from search.

 My problem is simple (though not my understanding LOL).

 struct D {
       int value;
       bool opEquals()(bool value) const { return (value == 
 value); }
 }

 D aD;
 if (aD == 1) { // OK
 }

 if (aD) {      //  Error: expression aD of type D does not have 
 a boolean value
 }

 Is there a way to overload for this? What am I missing?
struct D { int value; alias value this; ///"implicit cast" bool opEquals()(bool value) const { return (this.value == value); ///'this.' is required } }
May 21 2016
parent captain_fid <bell.hue gmail.com> writes:
On Saturday, 21 May 2016 at 18:31:46 UTC, vit wrote:
 On Saturday, 21 May 2016 at 18:10:55 UTC, captain_fid wrote:
 Please forgive if asked before. My google skills seemed to 
 fail me and didn't see any result from search.

 My problem is simple (though not my understanding LOL).

 struct D {
       int value;
       bool opEquals()(bool value) const { return (value == 
 value); }
 }

 D aD;
 if (aD == 1) { // OK
 }

 if (aD) {      //  Error: expression aD of type D does not 
 have a boolean value
 }

 Is there a way to overload for this? What am I missing?
struct D { int value; alias value this; ///"implicit cast" bool opEquals()(bool value) const { return (this.value == value); ///'this.' is required } }
Thanks both vit and anonymouse. (And I couldn't even provide a good example -- didn't mean to use 'value' for opEquals() arg .... argggggg Regardless -- vit, your suggestion compiled (and I laughed) but I'm not sure what's getting called. The opEquals() overload doesn't seem to be it. Hard returns of true or false don't seem to impact code. Hmmm. I'll also check anoymouse's suggestion. Regardless, thanks for the assistance.
May 21 2016
prev sibling parent reply Anonymouse <asdf asdf.com> writes:
On Saturday, 21 May 2016 at 18:10:55 UTC, captain_fid wrote:
 Please forgive if asked before. My google skills seemed to fail 
 me and didn't see any result from search.

 My problem is simple (though not my understanding LOL).

 struct D {
       int value;
       bool opEquals()(bool value) const { return (value == 
 value); }
 }

 D aD;
 if (aD == 1) { // OK
 }

 if (aD) {      //  Error: expression aD of type D does not have 
 a boolean value
 }

 Is there a way to overload for this? What am I missing?
struct D { int value; bool opEquals(T)(T value) const { return value == this.value; } bool opCast(T : bool)() const { return this != this.init; // or some such } } Not tested, written on my phone so might have missed something.
May 21 2016
parent reply captain_fid <bell.hue gmail.com> writes:
On Saturday, 21 May 2016 at 18:33:53 UTC, Anonymouse wrote:
 On Saturday, 21 May 2016 at 18:10:55 UTC, captain_fid wrote:
 Please forgive if asked before. My google skills seemed to 
 fail me and didn't see any result from search.

 My problem is simple (though not my understanding LOL).

 struct D {
       int value;
       bool opEquals()(bool value) const { return (value == 
 value); }
 }

 D aD;
 if (aD == 1) { // OK
 }

 if (aD) {      //  Error: expression aD of type D does not 
 have a boolean value
 }

 Is there a way to overload for this? What am I missing?
struct D { int value; bool opEquals(T)(T value) const { return value == this.value; } bool opCast(T : bool)() const { return this != this.init; // or some such } } Not tested, written on my phone so might have missed something.
Perfect.. and your 'phoned-in' coding is impressive. opCast is the biggest thing I was missing/forgetting/misunderstanding here. I Still need to look back at what is happening with vit's solution. Sorry again for the bad example to both of you.
May 21 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 05/21/2016 12:56 PM, captain_fid wrote:
 On Saturday, 21 May 2016 at 18:33:53 UTC, Anonymouse wrote:
 On Saturday, 21 May 2016 at 18:10:55 UTC, captain_fid wrote:
 Please forgive if asked before. My google skills seemed to fail me
 and didn't see any result from search.

 My problem is simple (though not my understanding LOL).

 struct D {
       int value;
       bool opEquals()(bool value) const { return (value == value); }
 }

 D aD;
 if (aD == 1) { // OK
 }

 if (aD) {      //  Error: expression aD of type D does not have a
 boolean value
 }

 Is there a way to overload for this? What am I missing?
struct D { int value; bool opEquals(T)(T value) const { return value == this.value; } bool opCast(T : bool)() const { return this != this.init; // or some such } } Not tested, written on my phone so might have missed something.
Perfect.. and your 'phoned-in' coding is impressive. opCast is the biggest thing I was missing/forgetting/misunderstanding
here. opCast is for explicit type conversions. However, you seem to want implicit type conversions.
 I Still need to look back at what is happening with vit's solution.
vit's 'alias ... this' solution is it. Here is some for info: http://ddili.org/ders/d.en/alias_this.html And here is another example if you want the 'bool' value to be calculated as opposed to being a member: struct S { int x; int y; /* Implicit type conversion to 'bool' * (Because myBoolValue() returns 'bool'.) */ alias myBoolValue this; bool myBoolValue() { return (x + y) == 7; // Some special condition } } void main() { auto a = S(1, 2); auto b = S(3, 4); assert(!a); // Calls a.myBoolValue() assert( b); // Calls b.myBoolValue() } Ali
May 21 2016
parent captain_fid <bell.hue gmail.com> writes:
On Saturday, 21 May 2016 at 23:06:10 UTC, Ali Çehreli wrote:
 On 05/21/2016 12:56 PM, captain_fid wrote:
 [...]
fail me
       [...]
value); }
 [...]
have a
 [...]
something.
 [...]
missing/forgetting/misunderstanding here. opCast is for explicit type conversions. However, you seem to want implicit type conversions.
 [...]
solution. vit's 'alias ... this' solution is it. Here is some for info: http://ddili.org/ders/d.en/alias_this.html And here is another example if you want the 'bool' value to be calculated as opposed to being a member: struct S { int x; int y; /* Implicit type conversion to 'bool' * (Because myBoolValue() returns 'bool'.) */ alias myBoolValue this; bool myBoolValue() { return (x + y) == 7; // Some special condition } } void main() { auto a = S(1, 2); auto b = S(3, 4); assert(!a); // Calls a.myBoolValue() assert( b); // Calls b.myBoolValue() } Ali
Thanks Ali (and again for making your work so available - I'm almost always re-reading). For some reason, I've missed 'alias this' and it is fascinating. I'm off to retry these suggestions now...
May 24 2016