Spaceship?

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

Spaceship?

Uko2
Hi everyone.

I’m wandering if there was any sort of a discussion about a spaceship method used in Ruby.

The concept is that you should implement a method <=>
that returns something negative if the receiver is smaller then a parameter,
positive when the receiver is greater then a parameter,
and 0 if they are equal.

This way if you are implementing comparable object’s the only method you have to redefine is spaceship (<=>).

Yes, I know that i Pharo you have to only redefine < and =. But maybe it would be interesting to use spaceship :)

What do you think?
Cheers!
Uko
Reply | Threaded
Open this post in threaded view
|

Re: Spaceship?

Stéphane Ducasse
do you have a real use case?

Stef

On Nov 4, 2013, at 1:32 PM, Yuriy Tymchuk <[hidden email]> wrote:

> Hi everyone.
>
> I’m wandering if there was any sort of a discussion about a spaceship method used in Ruby.
>
> The concept is that you should implement a method <=>
> that returns something negative if the receiver is smaller then a parameter,
> positive when the receiver is greater then a parameter,
> and 0 if they are equal.
>
> This way if you are implementing comparable object’s the only method you have to redefine is spaceship (<=>).
>
> Yes, I know that i Pharo you have to only redefine < and =. But maybe it would be interesting to use spaceship :)
>
> What do you think?
> Cheers!
> Uko


Reply | Threaded
Open this post in threaded view
|

Re: Spaceship?

philippeback

Type and double dispatch + nil considérations come to mind. 

nil <=> 7 gives?
7.0 <=> 3 gives ?

Object <=>: anObject

(self = anObject) ifTrue: [ ^0].

(self < anObject) ifTrue: [ ^ -1] ifFalse: [ ^ 1].

"otherwise let's raise an error"

What can now go wrong from here ?


Phil
On Monday, November 4, 2013, Stéphane Ducasse wrote:
do you have a real use case?

Stef

On Nov 4, 2013, at 1:32 PM, Yuriy Tymchuk <<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;yuriy.tymchuk@me.com&#39;)">yuriy.tymchuk@...> wrote:

> Hi everyone.
>
> I’m wandering if there was any sort of a discussion about a spaceship method used in Ruby.
>
> The concept is that you should implement a method <=>
> that returns something negative if the receiver is smaller then a parameter,
> positive when the receiver is greater then a parameter,
> and 0 if they are equal.
>
> This way if you are implementing comparable object’s the only method you have to redefine is spaceship (<=>).
>
> Yes, I know that i Pharo you have to only redefine < and =. But maybe it would be interesting to use spaceship :)
>
> What do you think?
> Cheers!
> Uko




--
---
Philippe Back
Dramatic Performance Improvements
Mob: +32(0) 478 650 140 | Fax: +32 (0) 70 408 027
Blog: http://philippeback.be | Twitter: @philippeback

High Octane SPRL
rue cour Boisacq 101 | 1301 Bierges | Belgium

Pharo Consortium Member - http://consortium.pharo.org/
Featured on the Software Process and Measurement Cast - http://spamcast.libsyn.com
Sparx Systems Enterprise Architect and Ability Engineering EADocX Value Added Reseller
 


Reply | Threaded
Open this post in threaded view
|

Re: Spaceship?

Uko2
In reply to this post by Stéphane Ducasse
Now she someone want’s to have a comparable object he has to use TComparable and define < and =.
With spaceship he has to define only <=>. I’m not sure what’s better. Just wanted to hear other peoples opinion

On 04 Nov 2013, at 13:35, Stéphane Ducasse <[hidden email]> wrote:

> do you have a real use case?
>
> Stef
>
> On Nov 4, 2013, at 1:32 PM, Yuriy Tymchuk <[hidden email]> wrote:
>
>> Hi everyone.
>>
>> I’m wandering if there was any sort of a discussion about a spaceship method used in Ruby.
>>
>> The concept is that you should implement a method <=>
>> that returns something negative if the receiver is smaller then a parameter,
>> positive when the receiver is greater then a parameter,
>> and 0 if they are equal.
>>
>> This way if you are implementing comparable object’s the only method you have to redefine is spaceship (<=>).
>>
>> Yes, I know that i Pharo you have to only redefine < and =. But maybe it would be interesting to use spaceship :)
>>
>> What do you think?
>> Cheers!
>> Uko
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Spaceship?

kilon.alios
It looks to me that this would be the source of less readable code, I prefer the choosing message approach by Kent Beck (Smalltalk Best Practice Patterns) where intent is clearly stated. Unless there is an advantage I am missing here. This is an example that less verbose code does not mean simpler code. Of course this will largely depend on the specifics of the case used. 


On Mon, Nov 4, 2013 at 3:37 PM, Yuriy Tymchuk <[hidden email]> wrote:
Now she someone want’s to have a comparable object he has to use TComparable and define < and =.
With spaceship he has to define only <=>. I’m not sure what’s better. Just wanted to hear other peoples opinion

On 04 Nov 2013, at 13:35, Stéphane Ducasse <[hidden email]> wrote:

> do you have a real use case?
>
> Stef
>
> On Nov 4, 2013, at 1:32 PM, Yuriy Tymchuk <[hidden email]> wrote:
>
>> Hi everyone.
>>
>> I’m wandering if there was any sort of a discussion about a spaceship method used in Ruby.
>>
>> The concept is that you should implement a method <=>
>> that returns something negative if the receiver is smaller then a parameter,
>> positive when the receiver is greater then a parameter,
>> and 0 if they are equal.
>>
>> This way if you are implementing comparable object’s the only method you have to redefine is spaceship (<=>).
>>
>> Yes, I know that i Pharo you have to only redefine < and =. But maybe it would be interesting to use spaceship :)
>>
>> What do you think?
>> Cheers!
>> Uko
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Spaceship?

Nicolas Cellier
Beware of cases where you don't have total order.
For example, in recent Squeak/Pharo we add to redefine the whole set of operators on numbers, not only < and =, just because NaN is not ordered...


2013/11/4 kilon alios <[hidden email]>
It looks to me that this would be the source of less readable code, I prefer the choosing message approach by Kent Beck (Smalltalk Best Practice Patterns) where intent is clearly stated. Unless there is an advantage I am missing here. This is an example that less verbose code does not mean simpler code. Of course this will largely depend on the specifics of the case used. 


On Mon, Nov 4, 2013 at 3:37 PM, Yuriy Tymchuk <[hidden email]> wrote:
Now she someone want’s to have a comparable object he has to use TComparable and define < and =.
With spaceship he has to define only <=>. I’m not sure what’s better. Just wanted to hear other peoples opinion

On 04 Nov 2013, at 13:35, Stéphane Ducasse <[hidden email]> wrote:

> do you have a real use case?
>
> Stef
>
> On Nov 4, 2013, at 1:32 PM, Yuriy Tymchuk <[hidden email]> wrote:
>
>> Hi everyone.
>>
>> I’m wandering if there was any sort of a discussion about a spaceship method used in Ruby.
>>
>> The concept is that you should implement a method <=>
>> that returns something negative if the receiver is smaller then a parameter,
>> positive when the receiver is greater then a parameter,
>> and 0 if they are equal.
>>
>> This way if you are implementing comparable object’s the only method you have to redefine is spaceship (<=>).
>>
>> Yes, I know that i Pharo you have to only redefine < and =. But maybe it would be interesting to use spaceship :)
>>
>> What do you think?
>> Cheers!
>> Uko
>
>




Reply | Threaded
Open this post in threaded view
|

Re: Spaceship?

Nicolas Cellier
we add^H^H^H had


2013/11/4 Nicolas Cellier <[hidden email]>
Beware of cases where you don't have total order.
For example, in recent Squeak/Pharo we add to redefine the whole set of operators on numbers, not only < and =, just because NaN is not ordered...


2013/11/4 kilon alios <[hidden email]>
It looks to me that this would be the source of less readable code, I prefer the choosing message approach by Kent Beck (Smalltalk Best Practice Patterns) where intent is clearly stated. Unless there is an advantage I am missing here. This is an example that less verbose code does not mean simpler code. Of course this will largely depend on the specifics of the case used. 


On Mon, Nov 4, 2013 at 3:37 PM, Yuriy Tymchuk <[hidden email]> wrote:
Now she someone want’s to have a comparable object he has to use TComparable and define < and =.
With spaceship he has to define only <=>. I’m not sure what’s better. Just wanted to hear other peoples opinion

On 04 Nov 2013, at 13:35, Stéphane Ducasse <[hidden email]> wrote:

> do you have a real use case?
>
> Stef
>
> On Nov 4, 2013, at 1:32 PM, Yuriy Tymchuk <[hidden email]> wrote:
>
>> Hi everyone.
>>
>> I’m wandering if there was any sort of a discussion about a spaceship method used in Ruby.
>>
>> The concept is that you should implement a method <=>
>> that returns something negative if the receiver is smaller then a parameter,
>> positive when the receiver is greater then a parameter,
>> and 0 if they are equal.
>>
>> This way if you are implementing comparable object’s the only method you have to redefine is spaceship (<=>).
>>
>> Yes, I know that i Pharo you have to only redefine < and =. But maybe it would be interesting to use spaceship :)
>>
>> What do you think?
>> Cheers!
>> Uko
>
>





Reply | Threaded
Open this post in threaded view
|

Re: Spaceship?

Uko2
In reply to this post by Nicolas Cellier
No one prohibits you from redefining other operators.

It’s just that a > b is defined by default as b < a. So why it is this way and not a < b is b > a ;)

With spaceship there is one method to rule them all. But Pharo’s implementation is interesting too. I never had an idea that you can define things like that

On 04 Nov 2013, at 18:54, Nicolas Cellier <[hidden email]> wrote:

Beware of cases where you don't have total order.
For example, in recent Squeak/Pharo we add to redefine the whole set of operators on numbers, not only < and =, just because NaN is not ordered...


2013/11/4 kilon alios <[hidden email]>
It looks to me that this would be the source of less readable code, I prefer the choosing message approach by Kent Beck (Smalltalk Best Practice Patterns) where intent is clearly stated. Unless there is an advantage I am missing here. This is an example that less verbose code does not mean simpler code. Of course this will largely depend on the specifics of the case used. 


On Mon, Nov 4, 2013 at 3:37 PM, Yuriy Tymchuk <[hidden email]> wrote:
Now she someone want’s to have a comparable object he has to use TComparable and define < and =.
With spaceship he has to define only <=>. I’m not sure what’s better. Just wanted to hear other peoples opinion

On 04 Nov 2013, at 13:35, Stéphane Ducasse <[hidden email]> wrote:

> do you have a real use case?
>
> Stef
>
> On Nov 4, 2013, at 1:32 PM, Yuriy Tymchuk <[hidden email]> wrote:
>
>> Hi everyone.
>>
>> I’m wandering if there was any sort of a discussion about a spaceship method used in Ruby.
>>
>> The concept is that you should implement a method <=>
>> that returns something negative if the receiver is smaller then a parameter,
>> positive when the receiver is greater then a parameter,
>> and 0 if they are equal.
>>
>> This way if you are implementing comparable object’s the only method you have to redefine is spaceship (<=>).
>>
>> Yes, I know that i Pharo you have to only redefine < and =. But maybe it would be interesting to use spaceship :)
>>
>> What do you think?
>> Cheers!
>> Uko
>
>