<:<, =:=, <%<

35 views
Skip to first unread message

Eugen Labun

unread,
Aug 11, 2011, 5:36:39 AM8/11/11
to scala...@googlegroups.com
Hi all,
could you please explain the meaning and usage of <:<, =:=, <%< classes and objects from Predef [1]?
In what situations can they be useful?

Thank you!

--
Eugen Labun

[1] https://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_9_0_1/src//library/scala/Predef.scala#L335

Jason Zaugg

unread,
Aug 11, 2011, 5:39:53 AM8/11/11
to Eugen Labun, scala...@googlegroups.com
On Thu, Aug 11, 2011 at 11:36 AM, Eugen Labun <la...@gmx.net> wrote:
> Hi all,
> could you please explain the meaning and usage of <:<, =:=, <%< classes and objects from Predef [1]?
> In what situations can they be useful?

http://stackoverflow.com/questions/2603003/operator-in-scala
http://stackoverflow.com/questions/3427345/what-do-and-mean-in-scala-2-8-and-where-are-they-documented

Jeff Olson

unread,
Aug 11, 2011, 1:29:20 PM8/11/11
to scala...@googlegroups.com, Eugen Labun
For two types A and B, if the compiler can find an implicit value of type A <:< B then it knows that A conforms to (is a subtype of) B. Likewise, if the compiler can find an implicit value of type A =:= B, then it knows that A and B and the same type. They are useful in encoding generalized type constraints as the above links explain.

scala> def prove[T](implicit proof: T) = true
prove: [T](implicit proof: T)Boolean

scala> prove[Null <:< String]
res0: Boolean = true

scala> prove[String <:< AnyRef]
res1: Boolean = true

scala> prove[Null <:< Long]
<console>:9: error: Cannot prove that Null <:< Long.
       prove[Null <:< Long]
            ^

scala> prove[Long <:< AnyRef]
<console>:9: error: Cannot prove that Long <:< AnyRef.
       prove[Long <:< AnyRef]
            ^

scala> prove[AnyRef =:= java.lang.Object]
res4: Boolean = true

scala> trait Foo { type T }
defined trait Foo

scala> val foo = new Foo { type T = Int }
foo: java.lang.Object with Foo{type T = Int} = $anon$1@258361f6

scala> prove[foo.T =:= Int]
res5: Boolean = true

scala> prove[Foo#T <:< AnyRef]
<console>:10: error: Cannot prove that Foo#T <:< AnyRef.
       prove[Foo#T <:< AnyRef]
            ^

scala> prove[Foo#T <:< Any]
res9: Boolean = true



Reply all
Reply to author
Forward
0 new messages