Agl
Agl

Reputation: 469

Weird Type Mismatch in Scala

I hope this question hasn't been answered yet somewhere else. Didn't find an answer here.

In my localisation system I've got a class named Language

class Language(val name:String, dict:HashMap[String, String]) {
  def apply(key: String):String = (dict get key) match {
    case None    => "°unknown°"
    case Some(s) => s
  } 

  //DO SOME OTHER THINGS
}

and an object named LanguageCentral

object LanguageCentral {
  private var lang:Option[Language] = None
  //SOME OTHER PRIVATE MEMBERS

  def language = lang

  def language_=(l:Option[Language]) = l match {
    case None    => {}
    case Some(l) => setLanguage(l)
  }

  def setLanguage(l:Language) {
    lang = Some(l)
    //DO SOME OTHER THINGS
  }

  //DO LOTS OF OTHER THINGS
}

I haven't written any code that's using this framework yet but trying it in an interactive session revealed a type error I don't really understand:

scala> val l = new LanguageCreator("Languages.csv").getLanguage("English")
l: Option[Language] = Some(Language@7aeb46d)

scala> LanguageCentral.language=l                                         
<console>:23: error: type mismatch;
 found   : Option[Language]
 required: Option[Language]
       LanguageCentral.language=l
                                ^

scala> LanguageCentral setLanguage (l getOrElse null)                     
<console>:24: error: type mismatch;
 found   : Language
 required: Language
       LanguageCentral setLanguage (l getOrElse null)
                                      ^

I really don't have a clue what's wrong. But from my experience with Haskell I guess that the solution is only a minor change away.;)
Could somebody help me? Thx.

P.S.: using Scala 2.8.0.final

Upvotes: 4

Views: 1317

Answers (1)

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297155

To me it looks like there are two distinct Language classes defined. One way of that happening on REPL is like this:

class Language
class LanguageCreator // using Language
// Oops, there's something that needs fixing on Language
class Language
object LanguageCentral // refers to a different Language altogether

Outside REPL, they might just be in different packages. There's a way to make REPL print fully qualified types, but I couldn't find the incantation at the moment.

EDIT

From the compiler side, you may use -uniqid and -explaintypes to get better error messages. I always use the latter, in fact. If you can't understand them, please update your question with them, and I'll take a look at it. Also, -Xprint-types may be of use, though that's a lot of information, so I'd rather avoid it if possible.

Upvotes: 3

Related Questions