Trying to extend loan pattern with type parameter and getting next error. Looks like just just a syntax error. I do suspect that this is a currying limitation? and return type must be provided in some way in my case. Thanks!
import java.io._
//def withPrintWriter(file: File)( op: PrintWriter => Unit) {
def withPrintWriter[T: Numeric](file: File)(implicit count: Numeric[T])( op: PrintWriter => Unit) {
import count._
val writer = new PrintWriter(file)
try {
for(x <- 0 to count.toInt() )
{
op(writer)
}
} finally {
writer.close()
}
}
val file = new File("date.txt")
withPrintWriter[Int]( file )( 5 ){
//withPrintWriter( file ){
writer => writer.println( new java.util.Date )
}
Error: c:\Sources\scala\main.scala:101: error: '=' expected but '(' found. def withPrintWriter[T: Numeric](file: File)(implicit count: Numeric[T])( op: PrintWriter => Unit) { ^ c:\Sources\scala\main.scala:115: error: illegal start of simple expression val file = new File("date.txt") ^
implicit
must be the last one, and usingT : Numeric
there is no need for the implicit param. Eitherdef withPrintWriter[T](file: File)(op: PrintWriter => Unit)(implicit count: Numeric[T]): Unit
ordef withPrintWriter[T : Numeric](file: File)(op: PrintWriter => Unit): Unit
(usingval count = implicitly[Numeric[T]]
in the fun body. – cchantepimplicit count: T
as no sense there – cchantep