0

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

4
  • 1
    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. Commented Apr 7, 2016 at 12:02
  • 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! :( Commented Apr 7, 2016 at 12:27
  • implicit count: T as no sense there Commented Apr 7, 2016 at 12:30
  • got it! its work perfect! Thanks Commented Apr 7, 2016 at 12:33

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

Well, apparently = is not required as I can see. Next function signature works fine: def withPrintWriter[T: Numeric](file: File)( op: PrintWriter => Unit)(count: T) {
= is not required because def foo() { automatically define the return type to Unit (is the same as def foo(): Unit = {)
Right, my bad...I wasn't paying attention to the fact that this was returning Unit; my apologies if this was misleading!
0

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 ) 
}   

3 Comments

The asInstanceOf[T] is unsafe and make the view bound : Numeric useless. Better doing val limit = implicitly[Numeric[T]].toInt(count); (0 to limit).foreach { _ => op(writer) }
Thanks for the tip, I didn't know that, what this unsafey could lead to? I would suspect to crash type conversion not possible?
isInstanceOf is unsafe in itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.