为什么map和Set在scala.Predef中别名?
发布时间:2020-12-16 19:22:08 所属栏目:安全 来源:网络整理
导读:10次??中有9次,简单地使用Map和Set就像我期望的那样,但偶尔会出乎意料地被击中 error: type mismatch; [INFO] found : scala.collection.Set[String][INFO] required: Set[String] 例如,来自REPL: scala case class Calculator[+T](name: String,parameters
10次??中有9次,简单地使用Map和Set就像我期望的那样,但偶尔会出乎意料地被击中
error: type mismatch; [INFO] found : scala.collection.Set[String] [INFO] required: Set[String] 例如,来自REPL: scala> case class Calculator[+T](name: String,parameters: Set[String]) defined class Calculator scala> val binding=Map.empty[String,String] binding: scala.collection.immutable.Map[String,String] = Map() scala> Calculator("Hello",binding.keySet) <console>:9: error: type mismatch; found : scala.collection.Set[String] required: Set[String] Calculator("Hello",binding.keySet) ^ 我想我理解错误,即对别名类型的函数调用返回实际类型. 因此在我看来,解决方案是导入无锯齿类型.在我的项目中的每个其他文件现在将生成类型不匹配错误,所以我将不得不在每个文件中导入它.这导致我在标题中提出的问题 – Predef中别名的目的是什么,如果最终我还需要导入实际的包? 我的理解是否有缺陷,或者我的用例不是典型的,或两者兼而有之? 解决方法
你误解了这个问题.并不是它不能识别类型别名与别名的类型相同.这是类型别名是scala.collection.immutable.Set,它与scala.collection.Set不同.
编辑:顺便说一句,我认为我已经解决了这个问题,正如类型诊断中的注释所证明的那样: ... Also,if the * type error is because of a conflict between two identically named * classes and one is in package scala,fully qualify the name so one * need not deduce why "java.util.Iterator" and "Iterator" don't match. 显然需要更多的工作. 编辑2010年7月17日:好的,我花了很长时间,但现在至少它说了一些难以理解的东西. files/neg/type-diagnostics.scala:4: error: type mismatch; found : scala.collection.Set[String] required: scala.collection.immutable.Set[String] def f = Calculator("Hello",binding.keySet) ^ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |