Skip to content

Instantly share code, notes, and snippets.

@ymnk
Created June 24, 2010 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymnk/451094 to your computer and use it in GitHub Desktop.
Save ymnk/451094 to your computer and use it in GitHub Desktop.
scalac -Xprint:erasure Test1.scala
scalac -Xprint:erasure Test2.scala
scalac -Xprint:erasure Test3.scala | egrep 'def bar|def b()'
object Test1 {
def foo[X](x: X): X = x
def bar(x: Int): Int = x
def goo[@specialized X](x: X): X = x
def main(arg: Array[String]) = {
foo(0)
bar(0)
goo(0)
}
}
// This code has come from a blog entry at
// http://d.hatena.ne.jp/kmizushima/20090717/1247835420
object Test2 {
abstract class IntAction extends (Int => Unit) {
def apply(x: Int): Unit
}
def upto_1(from: Int, to: Int)(f: Int => Unit) {
var i = from
while(i <= to) { f(i); i += 1 }
}
def upto_2(from: Int, to: Int)(f: IntAction) {
var i = from
while(i <= to) { f(i); i += 1 }
}
val MAX = 10000000
object bench1 extends scala.testing.Benchmark {
def run {
var sum = 0L
upto_1(0, MAX){i => sum += i }
println("sum = " + sum)
}
}
object bench2 extends scala.testing.Benchmark {
def run {
var sum = 0L
upto_2(0, MAX)(new IntAction {
def apply(i: Int) { sum += i; }
})
println("sum = " + sum)
}
}
object bench3 extends scala.testing.Benchmark {
def run {
var sum = 0L
var i = 0
while(i <= MAX) { sum += i; i += 1 }
println("sum = " + sum)
}
}
object bench4 extends scala.testing.Benchmark {
def run {
var sum = 0L
for(i <- (0 to MAX)){ sum += i; ()}
println("sum = " + sum)
}
}
object bench5 extends scala.testing.Benchmark {
def run {
var sum = 0L
for(i <- (0 to MAX)){ sum += i; i }
println("sum = " + sum)
}
}
def main(arg: Array[String]) = {
println(bench1.runBenchmark(10))
println(bench2.runBenchmark(10))
println(bench3.runBenchmark(10))
println(bench4.runBenchmark(10))
println(bench5.runBenchmark(10))
}
}
/**
* SID # 9 - Scala Specialization
*
* 3.1 Member Specialization
* Not all members of a class are specialized. Currently, specialized variants of
* a member m are created only when m’s type contains at least one specialized
* naked type parameter, or an array of a naked specialized type parameter.
*/
abstract class Test3[@specialized T, U] {
// the following members are specialized
def foo1(x: T): U
def foo2(x: Int): Array[T]
val a: Array[T]
// the following members are not specialized
def bar1(x: U): Unit
def bar2(x: List[T]): U
val b: List[T]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment