View mode: basic / threaded / horizontal-split · Log in · Help
May 09, 2012
"is" operator for structures?
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
Re: "is" operator for structures?
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
Re: "is" operator for structures?
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:
>
>
>>    bool opBinary(string op : "is")(typeof(null))
>
>
> Bye,
> bearophile
>



-- 
Bye,
Gor Gyolchanyan.
May 09, 2012
Re: "is" operator for structures?
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
Re: "is" operator for structures?
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
Re: "is" operator for structures?
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
Re: "is" operator for structures?
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.  I don't see any way
> around it.
>
> -Steve



-- 
Bye,
Gor Gyolchanyan.
May 09, 2012
Re: "is" operator for structures?
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.  I don't see any way
> around it.
>
> -Steve



-- 
Bye,
Gor Gyolchanyan.
May 09, 2012
Re: "is" operator for structures?
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
Re: "is" operator for structures?
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
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home