Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala继承的方法名称冲突_Scala_Testing_Namespaces_Scalatest - Fatal编程技术网

Scala继承的方法名称冲突

Scala继承的方法名称冲突,scala,testing,namespaces,scalatest,Scala,Testing,Namespaces,Scalatest,我正在尝试编写一些代码来测试数据库模型。测试框架和数据库框架都使用“===”操作符,并且测试框架的操作符被优先考虑。如何明确地使用一种方法或另一种方法 例如: import org.scalatest.FunSuite class TestDBModels extends FunSuite{ test("Test DoublePropertyEntry with a few new values") { Schemas.doubleProperties.deleteWhere(p

我正在尝试编写一些代码来测试数据库模型。测试框架和数据库框架都使用“===”操作符,并且测试框架的操作符被优先考虑。如何明确地使用一种方法或另一种方法

例如:

import org.scalatest.FunSuite

class TestDBModels extends FunSuite{
  test("Test DoublePropertyEntry with a few new values") {
    Schemas.doubleProperties.deleteWhere(p => (p.id === p.id)))
  }
}
错误:

type mismatch;
found   : Option[String]
required: org.squeryl.dsl.ast.LogicalBoolean
Schemas.doubleProperties.deleteWhere(p => (p.===(p.id, p.id)))

你有很多选择。第一种也是最简单的方法是使用显式方法调用,而不是隐式转换。例如,要显式使用scalatest==:

Schemas.doubleProperties.deleteWhere(p => (convertToEqualizer(p.id) === p.id)))
如果此名称太长,可以缩短名称:

def toEq(left: Any) = convertToEqualizer(left: Any)
Schemas.doubleProperties.deleteWhere(p => (toEq(p.id) === p.id)))
ConvertToeQualifizer是scalatest的隐式转换方法。另一个选项是作为非隐式方法重写ConvertToeQualifizer:

override def convertToEqualizer(left: Any) = new Equalizer(left)

这将阻止这种特定的隐式转换的发生。请参阅和。

这里的要点是将隐式定义重写为非隐式定义。这将关闭scalatest的
==
方法的自动使用。然后,如果您想使用断言和scalatest的
==
,您可以明确地调用convertToEqualizer,或者简单地调用
新均衡器。就个人而言,我会使用
shoulldmatchers
,因为我更喜欢
a应该等于(b)
而不是听起来更命令的
assert(a==b)
,或者在这种情况下,
assert(toEq(a)==b)