0
votes

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") ^

2
The parameter list with implicit must be the last one, and using T : Numeric there is no need for the implicit param. Either def withPrintWriter[T](file: File)(op: PrintWriter => Unit)(implicit count: Numeric[T]): Unit or def withPrintWriter[T : Numeric](file: File)(op: PrintWriter => Unit): Unit (using val count = implicitly[Numeric[T]] in the fun body.cchantep
Thanks for the tip. I've manage to resolve function signature issue: withPrintWriter[T: Numeric](file: File)( op: PrintWriter => Unit)(implicit count: T) : Unit ; but getting issue when calling this function: val file = new File("date.txt") val count : Int = 5 withPrintWriter[Int]( file )( writer => writer.println( new java.util.Date ) ) ( count ) Error: Not enough arguments provided! :(Pavel
implicit count: T as no sense therecchantep
got it! its work perfect! ThanksPavel

2 Answers

0
votes

From just a quick glance, it looks like your method def is missing an = before the body (brackets)...eg def foo(a: Int): Int = { ... }

0
votes

Looks like have to post the as answer to my question :) implicit was not required to use in the original code! Looks simple ...

    def withPrintWriter[T: Numeric](file: File)(count: T)( op: PrintWriter => Unit) {       
    val writer = new PrintWriter(file)      
    try {           
        for(x <- 0 to count.asInstanceOf[Int] )
        {                           
            op(writer)
        }
    } finally {
        writer.close()
    }
}


val file = new File("date.txt")
val count : Int = 5 

withPrintWriter[Int]{ file }{ count }    
{ 
    writer => writer.println( new java.util.Date ) 
}