|
Scala example source code file (spec-absfun.scala)
The Scala spec-absfun.scala source code
/** Test inheritance. See #3085.
* Anonymous functions extend AbstractFunction1[SpecializedPair[Int], Unit]. The
* specialized type SpecializedPair$mcI$sp should not leak into the superclass because
* the definition of apply would vary covariantly, and erasure won't consider it an
* override of the abstract apply, leading to an AbstractMethodError at runtime.
*/
object Test {
private val Max = 1000
def main(args: Array[String]) {
notSpecialized()
specialized()
println(runtime.BoxesRunTime.integerBoxCount)
}
def notSpecialized() {
val pairs = for { i <- 1 to Max; j <- 1 to i } yield new Pair(i, j)
val time0 = System.nanoTime
pairs foreach { p => p.first * p.second }
val time1 = System.nanoTime
// println(time1 - time0)
}
def specialized() {
val pairs = for { i <- 1 to Max; j <- 1 to i } yield new SpecializedPair(i, j)
val time0 = System.nanoTime
pairs foreach { p => p.first * p.second }
val time1 = System.nanoTime
// println(time1 - time0)
}
}
class Pair[A](_first: A, _second: A) {
def first = _first
def second = _second
}
class SpecializedPair[@specialized(Int) A](_first: A, _second: A) {
def first = _first
def second = _second
}
Other Scala examples (source code examples)Here is a short list of links related to this Scala spec-absfun.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.