- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Aliasing <= method to ≤ (\u2264) for all Ordered[T]
Tue, 2009-12-29, 07:03
This is my first attempt at "pimp my library". I'm enamored of Scala's
support of some unicode operators. I'm trying to add some more, starting
with the unicode less-than-or-equal-to operator (≤).
I successfully added it for Int. The following compiles and passes:
class LiterateInt(value: Int) {
def ≤ = value.<= _
}
implicit def intToLiterateInt(xs: Int) = new LiterateInt(xs)
assert(1 ≤ 1)
assert(1 ≤ 2)
assert(!(2 ≤ 1))
I'm pleased how succinct this is.
I could repeat this pattern for all the standard value types, but I'd much
prefer to augment the Ordered trait where the <= operator is defined. All my
attempts have failed. Here's my best attempt:
class LiterateOrdered[T](value: Ordered[T]) {
def ≤ = value.<= _
}
implicit def orderedToLiterateOrdered[T](xs: Ordered[T]) = new
LiterateOrdered[T](xs)
assert(1 ≤ 1)
assert(1 ≤ 2)
assert(!(2 ≤ 1))
assert(1L <= 1L)
assert(1 <= 1L)
This fails to compile with this error:
error: value ≤ is not a member of Int
assert(1 ≤ 1)
^
Is "pimp my library" possible on traits in this way. If so, what's the
proper incantation?
Sincerely,
Seeking Scala Edification
Tue, 2009-12-29, 10:17
#2
Re: Aliasing <= method to ≤ (\u2264) for all Ordered[T]
Try this:
implicit def orderedToLiterateOrdered[T <% Ordered[T]](xs: T) = new
LiterateOrdered(xs)
That means, there is an implicit conversion that goes from T to
Ordered[T] (there is also the 'identity' conversion in Predef).
I would code it like this though, it should be a bit faster:
class LiterateOrdered[T](x: Ordered[T]) {
def ≤(y: T) = x <= y
}
Regards,
Dimitris
2009/12/29 tb74 :
>
> This is my first attempt at "pimp my library". I'm enamored of Scala's
> support of some unicode operators. I'm trying to add some more, starting
> with the unicode less-than-or-equal-to operator (≤).
>
> I successfully added it for Int. The following compiles and passes:
>
> class LiterateInt(value: Int) {
> def ≤ = value.<= _
> }
> implicit def intToLiterateInt(xs: Int) = new LiterateInt(xs)
>
> assert(1 ≤ 1)
> assert(1 ≤ 2)
> assert(!(2 ≤ 1))
>
> I'm pleased how succinct this is.
>
> I could repeat this pattern for all the standard value types, but I'd much
> prefer to augment the Ordered trait where the <= operator is defined. All my
> attempts have failed. Here's my best attempt:
>
> class LiterateOrdered[T](value: Ordered[T]) {
> def ≤ = value.<= _
> }
> implicit def orderedToLiterateOrdered[T](xs: Ordered[T]) = new
> LiterateOrdered[T](xs)
>
> assert(1 ≤ 1)
> assert(1 ≤ 2)
> assert(!(2 ≤ 1))
>
> assert(1L <= 1L)
> assert(1 <= 1L)
>
> This fails to compile with this error:
>
> error: value ≤ is not a member of Int
> assert(1 ≤ 1)
> ^
>
> Is "pimp my library" possible on traits in this way. If so, what's the
> proper incantation?
>
> Sincerely,
> Seeking Scala Edification
> --
> View this message in context: http://old.nabble.com/Aliasing-%3C%3D-method-to-%E2%89%A4-%28%5Cu2264%29...
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>
Tue, 2009-12-29, 10:27
#3
Re: Aliasing <= method to ≤ (\u2264) for all Ordered[T]
Dimitris Andreou wrote:
>
> implicit def orderedToLiterateOrdered[T <% Ordered[T]](xs: T) = new
> LiterateOrdered(xs)
>
> ...
>
> class LiterateOrdered[T](x: Ordered[T]) {
> def ≤(y: T) = x <= y
> }
>
Worked like a charm. Thanks!
Tue, 2009-12-29, 13:17
#4
Re: Aliasing <= method to ≤ (\u2264) for all Ordered[T]
Spoke too soon. Progress made, but not complete success. My assertions with Long values were still using <= operator instead of ≤. It works when both operands are the same Type (fantastic, btw). It fails to compile when the operands are of different types
assert(1L ≤ 1L) // passes
assert(1 ≤ 1L) // fails to compile
1 ≤ 1L
yields the compilation error:
error: type arguments [Any] do not conform to method ordered's type
parameter bounds [A <: scala.math.Ordered[A]]
My current implementation based on Dimitris' reply:
class LiterateOrdered[T](x: Ordered[T]) {
def ≤(y: T): Boolean = x <= y
def ≥(y: T): Boolean = x >= y
}
implicit def orderedToLiterateOrdered[T <% Ordered[T]](xs: T) = new LiterateOrdered(xs)
The troublesome assertion, rewritten a bit:
val x:Int = 1
val y:Long = 1L
assert(x ≤ y) // fails to compile
I'll take a shot in the dark. Is the problem that my orderedToLiterateOrdered
is trumping Predef's int2long
such that x
isn't being converted to a Long as it would be if the expression was x <= y
?
View this message in context: Re: [scala-user] Aliasing <= method to ≤ (\u2264) for all Ordered[T]
Sent from the Scala - User mailing list archive at Nabble.com.
Tue, 2009-12-29, 14:27
#5
Re: Aliasing <= method to ≤ (\u2264) for all Ordered[T]
One fundamental problem with this is that expected operator priority won't work.
On Tue, Dec 29, 2009 at 4:03 AM, tb74 <nabble@tool-man.org> wrote:
--
Daniel C. Sobral
I travel to the future all the time.
On Tue, Dec 29, 2009 at 4:03 AM, tb74 <nabble@tool-man.org> wrote:
This is my first attempt at "pimp my library". I'm enamored of Scala's
support of some unicode operators. I'm trying to add some more, starting
with the unicode less-than-or-equal-to operator (≤).
I successfully added it for Int. The following compiles and passes:
class LiterateInt(value: Int) {
def ≤ = value.<= _
}
implicit def intToLiterateInt(xs: Int) = new LiterateInt(xs)
assert(1 ≤ 1)
assert(1 ≤ 2)
assert(!(2 ≤ 1))
I'm pleased how succinct this is.
I could repeat this pattern for all the standard value types, but I'd much
prefer to augment the Ordered trait where the <= operator is defined. All my
attempts have failed. Here's my best attempt:
class LiterateOrdered[T](value: Ordered[T]) {
def ≤ = value.<= _
}
implicit def orderedToLiterateOrdered[T](xs: Ordered[T]) = new
LiterateOrdered[T](xs)
assert(1 ≤ 1)
assert(1 ≤ 2)
assert(!(2 ≤ 1))
assert(1L <= 1L)
assert(1 <= 1L)
This fails to compile with this error:
error: value ≤ is not a member of Int
assert(1 ≤ 1)
^
Is "pimp my library" possible on traits in this way. If so, what's the
proper incantation?
Sincerely,
Seeking Scala Edification
--
View this message in context: http://old.nabble.com/Aliasing-%3C%3D-method-to-%E2%89%A4-%28%5Cu2264%29-for-all-Ordered-T--tp26951441p26951441.html
Sent from the Scala - User mailing list archive at Nabble.com.
--
Daniel C. Sobral
I travel to the future all the time.
Wed, 2009-12-30, 03:17
#6
Re: Aliasing <= method to ≤ (\u2264) for all Ordered[T]
Daniel Sobral wrote:
>
> One fundamental problem with this is that expected operator priority won't
> work.
>
Agreed. Made apparent by this assertion which won't compile:
assert(!2 ≤ 1)
Which is why I had to write it as:
assert(!(2 ≤ 1))
When I discovered the precedence issues and read some threads regarding it,
it became unlikely that I'd use the ≤ operator. But I still wanted to learn
how to make it happen.
Wed, 2009-12-30, 10:37
#7
Re: Aliasing <= method to ≤ (\u2264) for all Ordered[T]
toolbear74 wrote:
> Daniel Sobral wrote:
>
>> One fundamental problem with this is that expected operator priority won't
>> work.
>>
>>
>
> Agreed. Made apparent by this assertion which won't compile:
> assert(!2 ≤ 1)
>
Neither does !2 <= 1
> Which is why I had to write it as:
> assert(!(2 ≤ 1))
>
> When I discovered the precedence issues and read some threads regarding it,
> it became unlikely that I'd use the ≤ operator. But I still wanted to learn
> how to make it happen.
>
Scalaz (trunk) does this. An answer lies there.
http://scalaz.googlecode.com/svn/continuous/2009-12-22+5279.134594s/scal...
http://scalaz.googlecode.com/svn/continuous/2009-12-22+5279.134594s/src/...
http://code.google.com/p/scalaz/source/browse/continuous/2009-12-22%2B52...
tb74 wrote:
> This is my first attempt at "pimp my library". I'm enamored of Scala's
> support of some unicode operators. I'm trying to add some more, starting
> with the unicode less-than-or-equal-to operator (≤).
>
> I successfully added it for Int. The following compiles and passes:
>
> class LiterateInt(value: Int) {
> def ≤ = value.<= _
> }
> implicit def intToLiterateInt(xs: Int) = new LiterateInt(xs)
>
> assert(1 ≤ 1)
> assert(1 ≤ 2)
> assert(!(2 ≤ 1))
>
> I'm pleased how succinct this is.
>
> I could repeat this pattern for all the standard value types, but I'd much
> prefer to augment the Ordered trait where the <= operator is defined. All my
> attempts have failed. Here's my best attempt:
>
> class LiterateOrdered[T](value: Ordered[T]) {
> def ≤ = value.<= _
> }
> implicit def orderedToLiterateOrdered[T](xs: Ordered[T]) = new
> LiterateOrdered[T](xs)
>
> assert(1 ≤ 1)
> assert(1 ≤ 2)
> assert(!(2 ≤ 1))
>
> assert(1L <= 1L)
> assert(1 <= 1L)
>
> This fails to compile with this error:
>
> error: value ≤ is not a member of Int
> assert(1 ≤ 1)
> ^
>
> Is "pimp my library" possible on traits in this way. If so, what's the
> proper incantation?
>
> Sincerely,
> Seeking Scala Edification
>