12

Possible Duplicate:
What do <:<, <%<, and =:= mean in Scala 2.8, and where are they documented?

I don't understand what the =:=[A,B] stands for and how it can be useful. I've done some research but it's difficult to search something which has no alphanum chars in it. Can someone help me with a real example ?

Community
  • 1
  • 1
paradigmatic
  • 40,153
  • 18
  • 88
  • 147

3 Answers3

11

From Scala 2.8 onwards parameterized types have been afforded even more constraint capabilities via generalized type constraint classes. These classes enable further specialisation in methods, and complement context bounds, as follows:

A =:= B asserts that A and B must be the equal

A <:< B asserts that A must be a subtype of B

A sample usage of these classes would be to enable a specialization for addition of numeric elements in a collection, or for bespoke print formatting, or to allow for customized liability calculations on specific bet or fund types in a traders portfolio. For example:

case class PrintFormatter[T](item : T) {
def formatString(implicit evidence: T =:= String) = { // Will only work for String PrintFormatters
     println("STRING specialised printformatting...")
}
    def formatPrimitive(implicit evidence: T <:< AnyVal) = { // Will only work for Primitive PrintFormatters
println("WRAPPED PRIMITIVE specialised printformatting...")
}
}

val stringPrintFormatter = PrintFormatter("String to format...")
stringPrintFormatter formatString
// stringPrintFormatter formatPrimitive // Will not compile due to type mismatch

val intPrintFormatter = PrintFormatter(123)
intPrintFormatter formatPrimitive
// intPrintFormatter formatString // Will not compile due to type mismatch

You can find a whole short course about Scala types here: http://scalabound.org/?p=323

Mario Fusco
  • 13,548
  • 3
  • 28
  • 37
  • 1
    How is `<:<` different from `:` then? – ninjagecko Jun 21 '11 at 09:58
  • 3
    @ninjagecko: `a:A` asserts that the _term_ `a` has the _type_ `A`... it's analogous to the "is an element of" relation (a ∈ A) in set theory. `A <:< B` asserts that the _type_ A is a subtype of the _type_ B, which is analogous to the subset relation (A ⊆ B) in set theory. Just as ⊆ can be defined in terms of ∈, we can interpret `A <:< B` as the proposition, "for all terms `t`, if `t:A`, then `t:B`". – Tom Crockett Aug 21 '11 at 23:29
4

The =:=[A,B] class is evidence for A and B beeing the same class. See e.g. http://apocalisp.wordpress.com/2010/06/10/type-level-programming-in-scala-part-2-implicitly-and/ for an explanation.

It's really hard to find some examples. Here is a little bit code:

Landei
  • 54,104
  • 13
  • 100
  • 195
0

The best thing to find out is to look at the source of Predef. There you find this:

object =:= {
  implicit def tpEquals[A]: A =:= A = new (A =:= A) {def apply(x: A) = x}
}

Thus an object of type A =:= B is only available implicitly if A and B are the same type.

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
  • But that is an object which *uses* the class `=:=[A, A]`. It does not explain what `A =:= A` means. – Debilski Jun 21 '11 at 12:40
  • It does tell you how the compiler can get an instance of A=:=B implicitly, which is the only thing the class is used for. The class _only_ acts as a witness of the fact that A equals B. – Kim Stebel Jun 21 '11 at 13:10