This page is no longer maintained — Please continue to the home page at

question on type resolution

No replies
RG Demo
Joined: 2010-09-03,
User offline. Last seen 42 years 45 weeks ago.
I’m experimenting with Scala.  I want to implement a generic file archive.

So I have

trait FileFormat {
 type C    // cell type; a row is Array[C]; a document is Array[Array[C]]

 * readFile read a given File.  Assume each file is made up of
 * a series of "lines/rows".  A lineFilter function allows you to
 * keep only the "lines" that you are interested in.
 def readFile(file: File, lineFilter: Array[C] => Boolean): Array[Array[C]]

// then define several objects for reading different file formats
object FmtCSV extends FileFormat {

 type C = String                 // cell type  

And an archive trait

trait Archive {

 val name: String                   // name of this archive
 val DIR: File                      // path to the archive on disk
 val fileFormat: FileFormat         // how documents are stored

 type C = fileFormat.C      // cell type of row in file

 * readArchive, loop through the files of the archive and apply a
 * function to each row.  Return an ArrayBuffer[Array[C]]
 def readArchive(lineFilter: Array[C] => Boolean,
   files: List[File] = files): ArrayBuffer[Array[C]] = {
   val res = ArrayBuffer[Array[C]]()
   for (file <- files) yield {
     res ++= fileFormat.readFile(file, lineFilter)
. . .

// a concrete archive with CSV files
class FRArchive(val DIR: File) extends Archive {

 val name: String = "FR"
 val fileFormat: FileFormat = FmtCSV
 . . .

//now, from console

val fr = new FRArchive(new File(...))

val lineFilter: Array[fr.C] => Boolean = x => true
val res = fr.readArchive(lineFilter, files)     // read files in the archive

But I want to write something like this instead:
scala> val lineFilter: Array[String] => Boolean = x => true
lineFilter: Array[String] => Boolean = <function1>

scala> val res = fr.readArchive(lineFilter, files)
<console>:23: error: type mismatch;
found   : Array[String] => Boolean
required: Array[ftr.C] => Boolean
      val res = fr.readArchive(lineFilter, files)
I want the compiler to figure out that fr.C == String.  I think it should be able to do it.  How can I help the compiler?


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland