Search code examples
functionscalareturncase-classmismatch

scala return case mismatch for case class


I am suppose to return a case class called Entry, so I can't change the function's signature. When I filled my case class and try to return it, I get the following error:

<console>:13: error: type mismatch;
 found   : Entry(in method apply)
 required: <empty>.Entry

I am not sure why the function is asking for that type

def apply(line: String) : Entry = {
   case class Entry(account:String, password:String, UID:Int, GID:Int,
                    GECOS:String, directory:String, shell:String)
   val fields = line.split(":")
   val x = Entry(fields(0), fields(1), fields(2).toInt, fields(3).toInt,
         fields(4), fields(5), fields(6))
    x
}

Solution

  • This is a guess but I believe Entry is defined elsewhere. I say this because you have a reference to the "Entry" class in the function signature but you're also defining a case class within the function body. The Entry that you are returning is not the Entry that the function expects.