Strange type mismatch in Scala

I hope this question has not yet been answered. Did not find the answer here.

In my localization system, I have a class called 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 have not yet written the code that used this framework, but, having tried it in an interactive session, I found a type error that I really don't 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 understand what happened. But in my experience with Haskell, I assume that the solution is only a minor change.)
Can anyone help me? thanks.

PS: using Scala 2.8.0.final

+4
source share
1 answer

It seems to me that there are two different languages. One way to do this in REPL:

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

Outside of REPL, they can simply be in different packages. There you can make REPL print fully qualified types, but I could not find the spell at the moment.

EDIT

On the compiler side, you can use -uniqid and -explaintypes to get improved error messages. In fact, I always use the latter. If you cannot understand them, update your question and I will look at it. In addition, -Xprint-types can be useful, although this is a lot of information, so I would rather avoid this if possible.

+3
source

All Articles