digitalmars.D - "is" operator for structures?
- Gor Gyolchanyan (27/27) May 09 2012 I've stumbled upon a case, where I discovered, that what I did
- bearophile (4/7) May 09 2012 Bye,
- Gor Gyolchanyan (8/15) May 09 2012 Because the opBinary is a perfectly valid method. The inability to
- bearophile (6/7) May 09 2012 Thank for your answer, but I don't carte of "why" the D compiler
- Don Clugston (5/11) May 09 2012 I think you're asking for opBinary to be a keyword.
- Steven Schveighoffer (15/19) May 09 2012 This also works too:
- Gor Gyolchanyan (15/37) May 09 2012 I didn't know structs actually have an "is" operator. Good to know,
- Steven Schveighoffer (7/15) May 09 2012 The fact that it's not overloadable is useful in many situations. There...
- Gor Gyolchanyan (8/23) May 09 2012 Yeah, that's what I did. I replaced it with opEquals(typeof(null)).
- Gor Gyolchanyan (11/33) May 09 2012 If "is" was overloadable, one could make a legitimate reference types
- Steven Schveighoffer (6/10) May 09 2012 Yes, this is probably the only legitimate use case. I'm not sure how to...
- Gor Gyolchanyan (13/23) May 09 2012 Btw, I noticed how classes have two different comparison operators
- Jacob Carlborg (4/6) May 09 2012 We could create new operators :)
- Steven Schveighoffer (3/9) May 09 2012 Is there a unicode glyph for fist pump? :)
- H. S. Teoh (7/19) May 09 2012 [...]
- Matt Soucy (3/20) May 09 2012 I can only imagine what a fistpump operator would do...
- Gor Gyolchanyan (8/37) May 09 2012 I think it would be a unary prefix operator, which returns the
- Jacob Carlborg (4/9) May 09 2012 Haha :)
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
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
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:--=20 Bye, 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
May 09 2012
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
On 09/05/12 16:13, bearophile wrote:Gor Gyolchanyan: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.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
On Wed, 09 May 2012 10:13:01 -0400, bearophile <bearophileHUGS lycos.com> wrote:Gor Gyolchanyan: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. -SteveBecause 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.
May 09 2012
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: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=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.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 wayaround it. -Steve--=20 Bye, Gor Gyolchanyan.
May 09 2012
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
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:here areI 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=certain cases you are truly looking for physical equality, and not logica=lequality. Generally, if you want logical equality, use opEquals. -Steve--=20 Bye, Gor Gyolchanyan.
May 09 2012
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: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=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.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 wayaround it. -Steve--=20 Bye, Gor Gyolchanyan.
May 09 2012
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
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:w toIf "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=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
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
On Wed, 09 May 2012 16:25:35 -0400, Jacob Carlborg <doob me.com> wrote:On 2012-05-09 18:13, Steven Schveighoffer wrote:Is there a unicode glyph for fist pump? :) -SteveThis also works too: int opBinary(string s: "booya!")(...)We could create new operators :)
May 09 2012
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:[...] U+270A? :-) T -- "I speak better English than this villain Bush" -- Mohammed Saeed al-Sahaf, Iraqi Minister of InformationOn 2012-05-09 18:13, Steven Schveighoffer wrote:Is there a unicode glyph for fist pump? :)This also works too: int opBinary(string s: "booya!")(...)We could create new operators :)
May 09 2012
On 05/09/2012 06:05 PM, H. S. Teoh wrote:On Wed, May 09, 2012 at 05:04:59PM -0400, Steven Schveighoffer wrote:I can only imagine what a fistpump operator would do... Also, this is my favorite new glyph.On Wed, 09 May 2012 16:25:35 -0400, Jacob Carlborg<doob me.com> wrote:[...] U+270A? :-) TOn 2012-05-09 18:13, Steven Schveighoffer wrote:Is there a unicode glyph for fist pump? :)This also works too: int opBinary(string s: "booya!")(...)We could create new operators :)
May 09 2012
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:rote: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=--=20 Bye, Gor Gyolchanyan.I can only imagine what a fistpump operator would do... Also, this is my favorite new glyph.[...] U+270A? :-) TOn 2012-05-09 18:13, Steven Schveighoffer wrote:Is there a unicode glyph for fist pump? :)This also works too: int opBinary(string s: "booya!")(...)We could create new operators :)
May 09 2012
On 2012-05-10 00:05, H. S. Teoh wrote:On Wed, May 09, 2012 at 05:04:59PM -0400, Steven Schveighoffer wrote:Haha :) -- /Jacob CarlborgIs there a unicode glyph for fist pump? :)[...] U+270A? :-)
May 09 2012