Monday, March 22, 2010

Scala: desugaring

This post is composed of the info on scala source desugaring originating from this thread. I was afraid the information would be lost forever so, after a fight with markdown and pandoc I produced this:

Desugaring of scala types
Source typeType
Int => IntFunction1[Int, Int]
(Int, Int, Int) => IntFunction3[Int, Int, Int, Int]
(String, Int)Tuple2[String, Int]
String <:< Test<:<[String, Test]
Desugaring of scala 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]

Now lets get down to functions (first line - source, second - desugared, optional third - type):
Anonymous Function1
x: Int => x * 1

new Function1[Int, Int] {
    def apply(x: Int): Int = x * 1
}
Anonymous Function2
(_: Int) * (_: Int)

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

Type: Function2[Int, Int, Int]
Partial function explicitly typed as a Function1
{ case x: Int => x }: (Any => Int)

new Function1[Any, Int] {
   def apply(x: Any) = x match { case x: Int => x}
}
Partial function
{ case x: Int => x }: PartialFunction[Any, Int]

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>

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

Type: xml.Elem 
 
 
Thanks to:
  • David Flemstrom
  • Johannes Rudolph
  • Jason Zaugg

No comments: