Search code examples
overridingsmalltalkpharo

Overriding < in Pharo


I am trying to override the '<' operator in pharo, because i want to have a SortedCollection of a class i have implemented (TimeCal).

TimeCal has the following variables: year month day hour minute.

My idea was to convert all the variables to minutes, and then compare those with the comparand that the < operator receives. I do, however, get an error

"BlockClosure(Object)>>doesNotUnderstand: #>"

And here is my code:

< comparand
| thisInMins comparandInMins |
thisInMins := [(year * 525600) + (month * 43829) + (day * 1440) + (hour * 60) + minute].
comparandInMins := [(comparand year * 525600) + (comparand month * 43829) + (comparand day * 1440) + (comparand hour * 60) + comparand minute].

(thisInMins > comparandInMins)
ifTrue: [true] ifFalse: [false]

And the code i use to test it:

time1 := TimeCal new. 
time1 hour: 12.
time1 day: 12.
time1 month: 11.
time2 := TimeCal new. 
time2 hour: 12.
time2 day: 12.
time2 month: 8.
testing := time1 < time2.

I am not sure if what I am doing is correct. I could not find any proper guide on how to do it.


Solution

  • What about this

    < other
        year = other year ifFalse: [^year < other year].
        month = other month ifFalse: [^month < other month].
        day = other day ifFalse: [^day < other day].
        hour = other hour ifFalse: [^hour < other hour].
        ^minute < other minute