Scala conversions

This post is composed of info on scala source desugaring originating from this thread. I was afraid the information would be lost forever so here’s what I managed to salvage after a fight with markdown (tables, oh my…).

Types

Source typeType
Int => IntFunction1[Int, Int]
(Int, Int, Int) => IntFunction3[Int, Int, Int, Int]
(String, Int)Tuple2[String, Int]
String <:< Test<:<[String, Test]

Values

Source valueDesugaredType
nullNullNull
()()Unit
11Int
2.0f2.0fFloat
3.03.0Double
"hello""hello"String
'dSymbol("d")Symbol
(5,6)Tuple2(5,6)Tuple2[Int, Int]
(7,8,9)Tuple3(7,8,9)Tuple3[Int, Int, Int]

Anonymous Function1

x: Int => x * 1

// desugared
new Function1[Int, Int] {
    def apply(x: Int): Int = x * 1
}

Anonymous Function2

(_: Int) * (_: Int)

// desugared
(x1: Int, x2: Int) => x1 * x2

Type: Function2[Int, Int, Int]

Partial function explicitly typed as a Function1

{ case x: Int => x }: (Any => Int)

// desugared
new Function1[Any, Int] {
   def apply(x: Any) = x match { case x: Int => x}
}

Partial function

{ case x: Int => x }: PartialFunction[Any, Int]

// desugared
new PartialFunction[Any, Int] {
   def apply(x: Any) = x match { case x: Int => x}
   def isDefinedAt(x: Any) = x match { case x: Int => true; case _ => false}
}

XML Syntax

<a:b><c></c></a:b>

// desugared
new xml.Elem("a", "b", null, xml.TopScope,
new xml.Elem(null, "c", null, xml.TopScope))

Type: xml.Elem

Thanks to:


This information is true as of scala 2.7. Having said that, the desugarings in 2.8 shouldn’t have changed too much.