www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - "is" operator for structures?

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
I've stumbled upon a case, where I discovered, that what I did
actually isn't allowed:

struct SomeFancyPointer
{
public:
    bool opBinary(string op : "is")(typeof(null))
    {
        return _address is null;
    }

private:
    void* _address;
}
unittest
{
    assert(SomeFancyPointer.init is null);
}

After I got a message about SomeFancyPointer being incompatible with
typeof(null) of "is" comparison, I looked up at dlang.org the operator
overloading page and discovered, that "is" operator is not allowed to
be overloaded.
Granted, this limitation makes perfect sense for classes (just as
opAssign's limitation makes sense), but struct types are not reference
types, so an intrinsic "is" operator is usesless for them.
My question is, why not allow is operator to be overloadable for structures?

-- 
Bye,
Gor Gyolchanyan.
May 09 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Gor Gyolchanyan:

 My question is, why not allow is operator to be overloadable 
 for structures?
Or why the bad D compiler doesn't statically refuse the code like:
     bool opBinary(string op : "is")(typeof(null))
Bye, bearophile
May 09 2012
parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Because the opBinary is a perfectly valid method. The inability to
overload "is" only affects the rewrite of "A is B" to
"A.opBinart!`is`(B)".

On Wed, May 9, 2012 at 5:46 PM, bearophile <bearophileHUGS lycos.com> wrote=
:
 Gor Gyolchanyan:


 My question is, why not allow is operator to be overloadable for
 structures?
Or why the bad D compiler doesn't statically refuse the code like:
 =C2=A0 =C2=A0bool opBinary(string op : "is")(typeof(null))
Bye, bearophile
--=20 Bye, Gor Gyolchanyan.
May 09 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Gor Gyolchanyan:

 Because the opBinary [...]
Thank for your answer, but I don't carte of "why" the D compiler accepts that. I only care about the D compiler statically refusing that. Bye, bearophile
May 09 2012
next sibling parent Don Clugston <dac nospam.com> writes:
On 09/05/12 16:13, bearophile wrote:
 Gor Gyolchanyan:

 Because the opBinary [...]
Thank for your answer, but I don't carte of "why" the D compiler accepts that. I only care about the D compiler statically refusing that. Bye, bearophile
I think you're asking for opBinary to be a keyword. If it were statically rejected, surely you'll also want to have: void opBinary(string xx, int x) {} rejected as well. Along with a multitude of other things.
May 09 2012
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 09 May 2012 10:13:01 -0400, bearophile <bearophileHUGS lycos.com>  
wrote:

 Gor Gyolchanyan:

 Because the opBinary [...]
Thank for your answer, but I don't carte of "why" the D compiler accepts that. I only care about the D compiler statically refusing that.
This also works too: int opBinary(string s: "booya!")(...) or this too: int opBinry(string s: "+")(...) opBinary is a valid symbol, and as a valid symbol, it is a valid function, no matter whether the compiler calls it in a special way. I don't think it is a terrible thing, and I think statically disallowing that would be a worse idea. And to answer the OP, 'is' is special, it signals a bitwise compare, no matter what the contents of the type being compared. That being said, I understand why you want to do that. I don't see any way around it. -Steve
May 09 2012
next sibling parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
I didn't know structs actually have an "is" operator. Good to know,
there's a way to memcmp them this way.
But being able to overload it wouldn't do any damage. The overloader
of "is" should clearly know, that "is" is an identity check and not an
arbitrary domain-specific equality check.
Overloading "is" gives a syntax sugar for nullable structures and an
optimization opportunity for large ones (for example a CRC checksum
comparison).

On Wed, May 9, 2012 at 8:13 PM, Steven Schveighoffer
<schveiguy yahoo.com> wrote:
 On Wed, 09 May 2012 10:13:01 -0400, bearophile <bearophileHUGS lycos.com>
 wrote:

 Gor Gyolchanyan:

 Because the opBinary [...]
Thank for your answer, but I don't carte of "why" the D compiler accepts that. I only care about the D compiler statically refusing that.
This also works too: int opBinary(string s: "booya!")(...) or this too: int opBinry(string s: "+")(...) opBinary is a valid symbol, and as a valid symbol, it is a valid function=
,
 no matter whether the compiler calls it in a special way.

 I don't think it is a terrible thing, and I think statically disallowing
 that would be a worse idea.

 And to answer the OP, 'is' is special, it signals a bitwise compare, no
 matter what the contents of the type being compared.

 That being said, I understand why you want to do that. =C2=A0I don't see =
any way
 around it.

 -Steve
--=20 Bye, Gor Gyolchanyan.
May 09 2012
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 09 May 2012 12:17:30 -0400, Gor Gyolchanyan  
<gor.f.gyolchanyan gmail.com> wrote:

 I didn't know structs actually have an "is" operator. Good to know,
 there's a way to memcmp them this way.
 But being able to overload it wouldn't do any damage. The overloader
 of "is" should clearly know, that "is" is an identity check and not an
 arbitrary domain-specific equality check.
 Overloading "is" gives a syntax sugar for nullable structures and an
 optimization opportunity for large ones (for example a CRC checksum
 comparison).
The fact that it's not overloadable is useful in many situations. There are certain cases you are truly looking for physical equality, and not logical equality. Generally, if you want logical equality, use opEquals. -Steve
May 09 2012
parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Yeah, that's what I did. I replaced it with opEquals(typeof(null)).

On Wed, May 9, 2012 at 8:45 PM, Steven Schveighoffer
<schveiguy yahoo.com> wrote:
 On Wed, 09 May 2012 12:17:30 -0400, Gor Gyolchanyan
 <gor.f.gyolchanyan gmail.com> wrote:

 I didn't know structs actually have an "is" operator. Good to know,
 there's a way to memcmp them this way.
 But being able to overload it wouldn't do any damage. The overloader
 of "is" should clearly know, that "is" is an identity check and not an
 arbitrary domain-specific equality check.
 Overloading "is" gives a syntax sugar for nullable structures and an
 optimization opportunity for large ones (for example a CRC checksum
 comparison).
The fact that it's not overloadable is useful in many situations. =C2=A0T=
here are
 certain cases you are truly looking for physical equality, and not logica=
l
 equality.

 Generally, if you want logical equality, use opEquals.

 -Steve
--=20 Bye, Gor Gyolchanyan.
May 09 2012
prev sibling next sibling parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
If "is" was overloadable, one could make a legitimate reference types
via structs. The opAssign would change the reference, opEquals would
call the opEquals of the referred object, opBinary(string op : `is`)
would compare the references... Just like classes.

On Wed, May 9, 2012 at 8:13 PM, Steven Schveighoffer
<schveiguy yahoo.com> wrote:
 On Wed, 09 May 2012 10:13:01 -0400, bearophile <bearophileHUGS lycos.com>
 wrote:

 Gor Gyolchanyan:

 Because the opBinary [...]
Thank for your answer, but I don't carte of "why" the D compiler accepts that. I only care about the D compiler statically refusing that.
This also works too: int opBinary(string s: "booya!")(...) or this too: int opBinry(string s: "+")(...) opBinary is a valid symbol, and as a valid symbol, it is a valid function=
,
 no matter whether the compiler calls it in a special way.

 I don't think it is a terrible thing, and I think statically disallowing
 that would be a worse idea.

 And to answer the OP, 'is' is special, it signals a bitwise compare, no
 matter what the contents of the type being compared.

 That being said, I understand why you want to do that. =C2=A0I don't see =
any way
 around it.

 -Steve
--=20 Bye, Gor Gyolchanyan.
May 09 2012
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 09 May 2012 12:21:05 -0400, Gor Gyolchanyan  
<gor.f.gyolchanyan gmail.com> wrote:

 If "is" was overloadable, one could make a legitimate reference types
 via structs. The opAssign would change the reference, opEquals would
 call the opEquals of the referred object, opBinary(string op : `is`)
 would compare the references... Just like classes.
Yes, this is probably the only legitimate use case. I'm not sure how to make it work, exactly. But the functionality of 'is' should not be affected, it's too valuable the way it is to change it. -Steve
May 09 2012
parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Btw, I noticed how classes have two different comparison operators
(one for the reference and one for the object), while they have only
one assignment operator (for the reference only),
I think having two assignment operators would be very good for a
number of cases. Combined with a "final class" declaration it would
essentially be a reference type struct with a default constructor.
I've wanted such a thing for a long time now.

On Wed, May 9, 2012 at 8:48 PM, Steven Schveighoffer
<schveiguy yahoo.com> wrote:
 On Wed, 09 May 2012 12:21:05 -0400, Gor Gyolchanyan
 <gor.f.gyolchanyan gmail.com> wrote:

 If "is" was overloadable, one could make a legitimate reference types
 via structs. The opAssign would change the reference, opEquals would
 call the opEquals of the referred object, opBinary(string op : `is`)
 would compare the references... Just like classes.
Yes, this is probably the only legitimate use case. =C2=A0I'm not sure ho=
w to
 make it work, exactly. =C2=A0But the functionality of 'is' should not be
 affected, it's too valuable the way it is to change it.

 -Steve
--=20 Bye, Gor Gyolchanyan.
May 09 2012
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-05-09 18:13, Steven Schveighoffer wrote:

 This also works too:

 int opBinary(string s: "booya!")(...)
We could create new operators :) -- /Jacob Carlborg
May 09 2012
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 09 May 2012 16:25:35 -0400, Jacob Carlborg <doob me.com> wrote:

 On 2012-05-09 18:13, Steven Schveighoffer wrote:

 This also works too:

 int opBinary(string s: "booya!")(...)
We could create new operators :)
Is there a unicode glyph for fist pump? :) -Steve
May 09 2012
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, May 09, 2012 at 05:04:59PM -0400, Steven Schveighoffer wrote:
 On Wed, 09 May 2012 16:25:35 -0400, Jacob Carlborg <doob me.com> wrote:
 
On 2012-05-09 18:13, Steven Schveighoffer wrote:

This also works too:

int opBinary(string s: "booya!")(...)
We could create new operators :)
Is there a unicode glyph for fist pump? :)
[...] U+270A? :-) T -- "I speak better English than this villain Bush" -- Mohammed Saeed al-Sahaf, Iraqi Minister of Information
May 09 2012
next sibling parent reply Matt Soucy <msoucy csh.rit.edu> writes:
On 05/09/2012 06:05 PM, H. S. Teoh wrote:
 On Wed, May 09, 2012 at 05:04:59PM -0400, Steven Schveighoffer wrote:
 On Wed, 09 May 2012 16:25:35 -0400, Jacob Carlborg<doob me.com>  wrote:

 On 2012-05-09 18:13, Steven Schveighoffer wrote:

 This also works too:

 int opBinary(string s: "booya!")(...)
We could create new operators :)
Is there a unicode glyph for fist pump? :)
[...] U+270A? :-) T
I can only imagine what a fistpump operator would do... Also, this is my favorite new glyph.
May 09 2012
parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
I think it would be a unary prefix operator, which returns the
operand's copy, but with double the storage, having the second half -
wasted. :-D

On Thu, May 10, 2012 at 2:33 AM, Matt Soucy <msoucy csh.rit.edu> wrote:
 On 05/09/2012 06:05 PM, H. S. Teoh wrote:
 On Wed, May 09, 2012 at 05:04:59PM -0400, Steven Schveighoffer wrote:
 On Wed, 09 May 2012 16:25:35 -0400, Jacob Carlborg<doob me.com> =C2=A0w=
rote:
 On 2012-05-09 18:13, Steven Schveighoffer wrote:

 This also works too:

 int opBinary(string s: "booya!")(...)
We could create new operators :)
Is there a unicode glyph for fist pump? :)
[...] U+270A? :-) T
I can only imagine what a fistpump operator would do... Also, this is my favorite new glyph.
--=20 Bye, Gor Gyolchanyan.
May 09 2012
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-05-10 00:05, H. S. Teoh wrote:
 On Wed, May 09, 2012 at 05:04:59PM -0400, Steven Schveighoffer wrote:
 Is there a unicode glyph for fist pump? :)
[...] U+270A? :-)
Haha :) -- /Jacob Carlborg
May 09 2012