Generate equality

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Generate equality

Herby Vojčík
Hi!

Do you think it would be reasonable to have, a la "generate accessors" /
"generate initialize method", a "generate equality" thingie that would
generate #= and #hash for the method, mechanically?

Just created a new "piece of data" class and felt like it would be helpful.

Herby

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Peter Uhnak
generate accessors

there is option to generate accessors (right click on class)... but I find it rarely useful after initial setup (having accessors for all instance variables is rarely needed, and it is annoying to uncheck all the time).
In the latter scenario I have a small hack (startup script) that adds the option to instance variable (works with slot class layouts only) suggestions menu (not updated for calypso yet... https://github.com/peteruhnak/pharo-scripts/blob/master/config/6.0/suggestions-for-inst-vars.st )

for initialize, Nautilus had some shortcuts to generate code which you can try https://gist.github.com/peteruhnak/d9ee54f67d88b20ba85618e189b15669#generating

Peter

On Wed, Feb 7, 2018 at 5:10 PM, Herbert Vojčík <[hidden email]> wrote:
Hi!

Do you think it would be reasonable to have, a la "generate accessors" / "generate initialize method", a "generate equality" thingie that would generate #= and #hash for the method, mechanically?

Just created a new "piece of data" class and felt like it would be helpful.

Herby


Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Herby Vojčík
This does not look like the answer to my question. :-/

Ok, to rephrase:

Would you find it useful to have "generate equality" which generates #=
and #hash for a class?

Herby

P.S.: I know there is "generate accessors". I mentioned it as "a thing
which is there, and from which one can get inspiration".

Peter Uhnák wrote:

>  > generate accessors
>
> there is option to generate accessors (right click on class)... but I
> find it rarely useful after initial setup (having accessors for all
> instance variables is rarely needed, and it is annoying to uncheck all
> the time).
> In the latter scenario I have a small hack (startup script) that adds
> the option to instance variable (works with slot class layouts only)
> suggestions menu (not updated for calypso yet...
> https://github.com/peteruhnak/pharo-scripts/blob/master/config/6.0/suggestions-for-inst-vars.st
> )
>
> for initialize, Nautilus had some shortcuts to generate code which you
> can try
> https://gist.github.com/peteruhnak/d9ee54f67d88b20ba85618e189b15669#generating
>
> Peter
>
> On Wed, Feb 7, 2018 at 5:10 PM, Herbert Vojčík <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi!
>
>     Do you think it would be reasonable to have, a la "generate
>     accessors" / "generate initialize method", a "generate equality"
>     thingie that would generate #= and #hash for the method, mechanically?
>
>     Just created a new "piece of data" class and felt like it would be
>     helpful.
>
>     Herby
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Stephane Ducasse-3
In reply to this post by Herby Vojčík
Noury proposed a traits for that long time ago and it looked smart.
I do not remember where.

On Wed, Feb 7, 2018 at 5:10 PM, Herbert Vojčík <[hidden email]> wrote:

> Hi!
>
> Do you think it would be reasonable to have, a la "generate accessors" /
> "generate initialize method", a "generate equality" thingie that would
> generate #= and #hash for the method, mechanically?
>
> Just created a new "piece of data" class and felt like it would be helpful.
>
> Herby
>

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

NorbertHartl
In reply to this post by Herby Vojčík
I think that implementing = and hash is almost always wrong. So I guess the answer is no :)

Norbert

> Am 07.02.2018 um 17:10 schrieb Herbert Vojčík <[hidden email]>:
>
> Hi!
>
> Do you think it would be reasonable to have, a la "generate accessors" / "generate initialize method", a "generate equality" thingie that would generate #= and #hash for the method, mechanically?
>
> Just created a new "piece of data" class and felt like it would be helpful.
>
> Herby

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Esteban A. Maringolo
I wouldn't start a flamewar on this, but being able to separate
equality and identity is important.

Otherwise we should have to remove #= or #== since of either is redundant. :)

If you're dealing with ORM having a good implementation of #= is
important, since you might have several instances representing the
same "abstraction" (I wouldn't call it an object). Then you have to
implement #hash as well.

What is wrong most of the times is the implementation of #= and #hash itself.
I remember that Eclipse for Java provides Herbert's required feature,
where it can coherent equals() and hashcode() functions based on the
attributes of your choice.

Regards!

Esteban A. Maringolo


2018-02-07 17:09 GMT-03:00 Norbert Hartl <[hidden email]>:

> I think that implementing = and hash is almost always wrong. So I guess the answer is no :)
>
> Norbert
>
>> Am 07.02.2018 um 17:10 schrieb Herbert Vojčík <[hidden email]>:
>>
>> Hi!
>>
>> Do you think it would be reasonable to have, a la "generate accessors" / "generate initialize method", a "generate equality" thingie that would generate #= and #hash for the method, mechanically?
>>
>> Just created a new "piece of data" class and felt like it would be helpful.
>>
>> Herby
>

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Herby Vojčík


Esteban A. Maringolo wrote:

> I wouldn't start a flamewar on this, but being able to separate
> equality and identity is important.
>
> Otherwise we should have to remove #= or #== since of either is redundant. :)
>
> If you're dealing with ORM having a good implementation of #= is
> important, since you might have several instances representing the
> same "abstraction" (I wouldn't call it an object). Then you have to
> implement #hash as well.
>
> What is wrong most of the times is the implementation of #= and #hash itself.
> I remember that Eclipse for Java provides Herbert's required feature,
> where it can coherent equals() and hashcode() functions based on the
> attributes of your choice.
>
> Regards!
>
> Esteban A. Maringolo
>
>
> 2018-02-07 17:09 GMT-03:00 Norbert Hartl<[hidden email]>:
>> I think that implementing = and hash is almost always wrong. So I guess the answer is no :)

Just as mechanically generated initializers / accessors are not always
directly what you want, you then change them later, but: it is helpful
to be able to generate the default version of them.

I though the same for the #=/#hash pair.

Never thought it would cause a flame (just a "is it important enough"
discussion)...

Herby

>> Norbert
>>
>>> Am 07.02.2018 um 17:10 schrieb Herbert Vojčík<[hidden email]>:
>>>
>>> Hi!
>>>
>>> Do you think it would be reasonable to have, a la "generate accessors" / "generate initialize method", a "generate equality" thingie that would generate #= and #hash for the method, mechanically?
>>>
>>> Just created a new "piece of data" class and felt like it would be helpful.
>>>
>>> Herby
>

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Richard Sargent
Administrator
On Wed, Feb 7, 2018 at 12:51 PM, Herbert Vojčík <[hidden email]> wrote:


Esteban A. Maringolo wrote:
I wouldn't start a flamewar on this, but being able to separate
equality and identity is important.

Otherwise we should have to remove #= or #== since of either is redundant. :)

If you're dealing with ORM having a good implementation of #= is
important, since you might have several instances representing the
same "abstraction" (I wouldn't call it an object). Then you have to
implement #hash as well.

What is wrong most of the times is the implementation of #= and #hash itself.
I remember that Eclipse for Java provides Herbert's required feature,
where it can coherent equals() and hashcode() functions based on the
attributes of your choice.

Regards!

Esteban A. Maringolo


2018-02-07 17:09 GMT-03:00 Norbert Hartl<[hidden email]>:
I think that implementing = and hash is almost always wrong. So I guess the answer is no :)

Just as mechanically generated initializers / accessors are not always directly what you want, you then change them later, but: it is helpful to be able to generate the default version of them.

I though the same for the #=/#hash pair.

Never thought it would cause a flame (just a "is it important enough" discussion)...

I agree it is important to have tools that help with commonly done operations. So, I'm generally in favour of making such a tool available.

By default, generate comparisons of all the class's instance variables, inheriting non-Object implementations via super, when there are any.

If there exists metadata for the instance variables, perhaps that metadata could be augment to indicate which instance variables are relevant to the object's uniqueness. And in such a case, the generator could restrict itself to such instance variables, by default.

 

Herby

Norbert

Am 07.02.2018 um 17:10 schrieb Herbert Vojčík<[hidden email]>:

Hi!

Do you think it would be reasonable to have, a la "generate accessors" / "generate initialize method", a "generate equality" thingie that would generate #= and #hash for the method, mechanically?

Just created a new "piece of data" class and felt like it would be helpful.

Herby



Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

hernanmd
In reply to this post by Herby Vojčík
Hi Herbert,

How the "generate equality" behavior would be the default?
Mechanically = non-interactive?

Cheers,

Hernán


2018-02-07 13:10 GMT-03:00 Herbert Vojčík <[hidden email]>:

> Hi!
>
> Do you think it would be reasonable to have, a la "generate accessors" /
> "generate initialize method", a "generate equality" thingie that would
> generate #= and #hash for the method, mechanically?
>
> Just created a new "piece of data" class and felt like it would be helpful.
>
> Herby
>

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Stephane Ducasse-3
Guys you should really check what noury did :)



On Thu, Feb 8, 2018 at 5:07 AM, Hernán Morales Durand
<[hidden email]> wrote:

> Hi Herbert,
>
> How the "generate equality" behavior would be the default?
> Mechanically = non-interactive?
>
> Cheers,
>
> Hernán
>
>
> 2018-02-07 13:10 GMT-03:00 Herbert Vojčík <[hidden email]>:
>> Hi!
>>
>> Do you think it would be reasonable to have, a la "generate accessors" /
>> "generate initialize method", a "generate equality" thingie that would
>> generate #= and #hash for the method, mechanically?
>>
>> Just created a new "piece of data" class and felt like it would be helpful.
>>
>> Herby
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Stephane Ducasse-3
I asked him.
BTW  The second chapter is nice :)

On Thu, Feb 8, 2018 at 9:24 AM, Stephane Ducasse
<[hidden email]> wrote:

> Guys you should really check what noury did :)
>
>
>
> On Thu, Feb 8, 2018 at 5:07 AM, Hernán Morales Durand
> <[hidden email]> wrote:
>> Hi Herbert,
>>
>> How the "generate equality" behavior would be the default?
>> Mechanically = non-interactive?
>>
>> Cheers,
>>
>> Hernán
>>
>>
>> 2018-02-07 13:10 GMT-03:00 Herbert Vojčík <[hidden email]>:
>>> Hi!
>>>
>>> Do you think it would be reasonable to have, a la "generate accessors" /
>>> "generate initialize method", a "generate equality" thingie that would
>>> generate #= and #hash for the method, mechanically?
>>>
>>> Just created a new "piece of data" class and felt like it would be helpful.
>>>
>>> Herby
>>>
>>

book.pdf (219K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Herby Vojčík
In reply to this post by hernanmd


On February 8, 2018 5:07:03 AM GMT+01:00, "Hernán Morales Durand" <[hidden email]> wrote:
>Hi Herbert,
>
>How the "generate equality" behavior would be the default?
>Mechanically = non-interactive?

Like “generate accessors”. So interactively, on demand. Of course.

I tried to draw that analogy on original post, but probably wasn't writing it down clearly enough.

Herby

>
>Cheers,
>
>Hernán
>
>
>2018-02-07 13:10 GMT-03:00 Herbert Vojčík <[hidden email]>:
>> Hi!
>>
>> Do you think it would be reasonable to have, a la "generate
>accessors" /
>> "generate initialize method", a "generate equality" thingie that
>would
>> generate #= and #hash for the method, mechanically?
>>
>> Just created a new "piece of data" class and felt like it would be
>helpful.
>>
>> Herby
>>

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

alistairgrant
In reply to this post by Herby Vojčík
Hi Herby,

On 8 February 2018 at 03:10, Herbert Vojčík <[hidden email]> wrote:
> Hi!
>
> Do you think it would be reasonable to have, a la "generate accessors" /
> "generate initialize method", a "generate equality" thingie that would
> generate #= and #hash for the method, mechanically?
>
> Just created a new "piece of data" class and felt like it would be helpful.

The refactoring browser is already capable of generating #= and #hash:


| r |

r := RBGenerateEqualHashRefactoring
        className: MyClass
        variables: #(vars which should be used in equal and hash).
r execute


HTH,
Alistair

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Herby Vojčík


Alistair Grant wrote:

> Hi Herby,
>
> On 8 February 2018 at 03:10, Herbert Vojčík<[hidden email]>  wrote:
>> Hi!
>>
>> Do you think it would be reasonable to have, a la "generate accessors" /
>> "generate initialize method", a "generate equality" thingie that would
>> generate #= and #hash for the method, mechanically?
>>
>> Just created a new "piece of data" class and felt like it would be helpful.
>
> The refactoring browser is already capable of generating #= and #hash:

Now somebody tell me it actually is there in the Nautilus menu, I just
did not find it. Which would be the best outcome, save making myself
ridiculous online... :-)

> | r |
>
> r := RBGenerateEqualHashRefactoring
>          className: MyClass
>          variables: #(vars which should be used in equal and hash).
> r execute
>
>
> HTH,
> Alistair
>

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Stephane Ducasse-3
http://smalltalkhub.com/#!/~CAR/ReusableBricks/packages/Equals

On Thu, Feb 8, 2018 at 12:47 PM, Herbert Vojčík <[hidden email]> wrote:

>
>
> Alistair Grant wrote:
>>
>> Hi Herby,
>>
>> On 8 February 2018 at 03:10, Herbert Vojčík<[hidden email]>  wrote:
>>>
>>> Hi!
>>>
>>> Do you think it would be reasonable to have, a la "generate accessors" /
>>> "generate initialize method", a "generate equality" thingie that would
>>> generate #= and #hash for the method, mechanically?
>>>
>>> Just created a new "piece of data" class and felt like it would be
>>> helpful.
>>
>>
>> The refactoring browser is already capable of generating #= and #hash:
>
>
> Now somebody tell me it actually is there in the Nautilus menu, I just did
> not find it. Which would be the best outcome, save making myself ridiculous
> online... :-)
>
>
>> | r |
>>
>> r := RBGenerateEqualHashRefactoring
>>          className: MyClass
>>          variables: #(vars which should be used in equal and hash).
>> r execute
>>
>>
>> HTH,
>> Alistair
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Herby Vojčík

Stephane Ducasse wrote:
> http://smalltalkhub.com/#!/~CAR/ReusableBricks/packages/Equals

Ah. It uses trait that does it dynamically.

Good to know, usable for some more dynamic scenarios; but here I wanted
something more explicit a la existing "generate xxx", so I went for
Alistair's suggestion, so it was just a matter of Playground and:

(RBGenerateEqualHashRefactoring className: PharktSymbol variables:
#(baseAsset quoteAsset)) execute

Thanks.

(btw, it would be nice if that was actually in a menu; but if Nautilus
if phased out, at least in Calypso in Pharo 7 :-) )

Herby

> On Thu, Feb 8, 2018 at 12:47 PM, Herbert Vojčík<[hidden email]>  wrote:
>>
>> Alistair Grant wrote:
>>> Hi Herby,
>>>
>>> On 8 February 2018 at 03:10, Herbert Vojčík<[hidden email]>   wrote:
>>>> Hi!
>>>>
>>>> Do you think it would be reasonable to have, a la "generate accessors" /
>>>> "generate initialize method", a "generate equality" thingie that would
>>>> generate #= and #hash for the method, mechanically?
>>>>
>>>> Just created a new "piece of data" class and felt like it would be
>>>> helpful.
>>>
>>> The refactoring browser is already capable of generating #= and #hash:
>>
>> Now somebody tell me it actually is there in the Nautilus menu, I just did
>> not find it. Which would be the best outcome, save making myself ridiculous
>> online... :-)
>>
>>
>>> | r |
>>>
>>> r := RBGenerateEqualHashRefactoring
>>>           className: MyClass
>>>           variables: #(vars which should be used in equal and hash).
>>> r execute
>>>
>>>
>>> HTH,
>>> Alistair
>>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Generate equality

Denis Kudriashov
I will add it to Calypso. 
I use it often.

2018-02-08 17:23 GMT+03:00 Herbert Vojčík <[hidden email]>:

Stephane Ducasse wrote:
http://smalltalkhub.com/#!/~CAR/ReusableBricks/packages/Equals

Ah. It uses trait that does it dynamically.

Good to know, usable for some more dynamic scenarios; but here I wanted something more explicit a la existing "generate xxx", so I went for Alistair's suggestion, so it was just a matter of Playground and:

(RBGenerateEqualHashRefactoring className: PharktSymbol variables: #(baseAsset quoteAsset)) execute

Thanks.

(btw, it would be nice if that was actually in a menu; but if Nautilus if phased out, at least in Calypso in Pharo 7 :-) )

Herby


On Thu, Feb 8, 2018 at 12:47 PM, Herbert Vojčík<[hidden email]>  wrote:

Alistair Grant wrote:
Hi Herby,

On 8 February 2018 at 03:10, Herbert Vojčík<[hidden email]>   wrote:
Hi!

Do you think it would be reasonable to have, a la "generate accessors" /
"generate initialize method", a "generate equality" thingie that would
generate #= and #hash for the method, mechanically?

Just created a new "piece of data" class and felt like it would be
helpful.

The refactoring browser is already capable of generating #= and #hash:

Now somebody tell me it actually is there in the Nautilus menu, I just did
not find it. Which would be the best outcome, save making myself ridiculous
online... :-)


| r |

r := RBGenerateEqualHashRefactoring
          className: MyClass
          variables: #(vars which should be used in equal and hash).
r execute


HTH,
Alistair