How to convert the method timed into a function?
val timing = new StringBuffer
def timed[T](label: String, code: => T): T = {
val start = System.currentTimeMillis()
val result = code
val stop = System.currentTimeMillis()
timing.append(s"Processing $label took ${stop - start} ms.\n")
result
}
Below causes "error: not found: type T"
val timing = new StringBuffer
val timed: (String, => T) => T = (label, code) => {
val start = System.currentTimeMillis()
val result = code
val stop = System.currentTimeMillis()
timing.append(s"Processing $label took ${stop - start} ms.\n")
result
}
There is no such thing as generic function in Scala (and generic value at all), only generic methods are.
Generic functions will appear in Scala 3.
https://github.com/lampepfl/dotty/pull/4672
http://dotty.epfl.ch/docs/reference/overview.html#new-constructs
val timing = new StringBuffer
val timed: [T] => (String, /*=>*/ T) => T = [T] => (label: String, code: /*=>*/ T) => {
val start = System.currentTimeMillis()
val result = code
val stop = System.currentTimeMillis()
timing.append(s"Processing $label took ${stop - start} ms.\n")
result
}
in Dotty 0.20.0-RC1.