Implicit Not Visible in Scope

22 views
Skip to first unread message

Joe San

unread,
Apr 21, 2017, 2:11:14 PM4/21/17
to scala-user
In the following example., I was expecting something to be printed at line 1 and 2, but strangely I do not see that happening. My IDE complaints on line 3 that it is an unused import statement. Any ideas what is going wrong?

import scala.io.Source


class CSVReader[A] {
def parse(path: String) = ReaderWithFile[A](path)

case class ReaderWithFile[A](path: String) {
def using(cfg: CSVParserConfig): Seq[A] = {
val lines = Source.fromFile(path).getLines().mkString("\n")
println(lines) // line 1
println(cfg) // line 2
null
}
}
object ReaderWithFile {
implicit def parser2parsed[A](parser: ReaderWithFile[A]): Seq[A] = parser.using(defaultParserCfg)
}
}
object CSVReader extends App {

def parser[A] = new CSVReader[A]

val myParser = parser

import myParser.ReaderWithFile.parser2parsed // line 3, IDE says that this import is unused!

myParser parse "csv-parser/test.csv" // expecting line 1 and 2 getting printed, but not!

// but this works
myParser parse "csv-parser/test.csv" using CSVParserConfig()

}

Oliver Ruebenacker

unread,
Apr 21, 2017, 3:10:47 PM4/21/17
to Joe San, scala-user

     Hello,

  Not sure why you expect lines 1 and 2 to be printed is method using is not called?

  Also, parser2parsed is not used.

     Best, Oliver

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Joe San

unread,
Apr 21, 2017, 3:23:51 PM4/21/17
to Oliver Ruebenacker, scala-user
So the idea is if the user calls just this:

 myParser parse "csv-parser/test.csv"

I want the default to be applied!

Oliver Ruebenacker

unread,
Apr 21, 2017, 3:28:27 PM4/21/17
to Joe San, scala-user
  Not sure what you mean by "default"?

  Since myParser is of type CSVReader, it has a parse method that takes a String, so there is no reason for the compiler to look for an implicit conversion.

Joe San

unread,
Apr 21, 2017, 3:34:40 PM4/21/17
to Oliver Ruebenacker, scala-user
So what I want to achieve is effectively the following:

myParser parse "/Users/jothi/Projects/Private/scala-projects/csv-parser/test.csv"

// but this works
myParser parse "/Users/jothi/Projects/Private/scala-projects/csv-parser/test.csv" using CSVParserConfig(Pipe)
So if the using CSVParserConfig is not provided, I should be able to box using defaultCSVParserConfig into the call!

But if the caller does not provide a specific config, I should apply the default as specified in the implicit

implicit def parser2parsed[B](parser: ReaderWithFile[B]): Seq[B] = parser.using(defaultParserCfg)

Oliver Ruebenacker

unread,
Apr 21, 2017, 3:44:39 PM4/21/17
to Joe San, scala-user
  If the expression would appear in a context where Seq[A] is required, then the implicit conversion would apply (for example, if you add a ": Seq[A]" at the end. BTW, what is A? It's nowhere defined.

  Otherwise, why not just add an argument with default value to the parse method?

Joe San

unread,
Apr 21, 2017, 3:52:11 PM4/21/17
to Oliver Ruebenacker, scala-user
Adding a default argument would not let me write the call as expressive as it is now!
Reply all
Reply to author
Forward
0 new messages