如何定义扩展此特征的任何事物

发布于 2024-10-20 16:52:41 字数 482 浏览 1 评论 0 原文

请参阅以下代码片段:

 trait Fruit {
   val color:String
   def == (fruit:Fruit) = this.color == fruit.color
 }

 case class Orange(color:String) extends Fruit 

 case class Apple(color:String) extends Fruit

正如预期的那样,Orange("red") == Orange("red")true。但是,我想强制只能比较相同类型的水果,因此例如 Orange("red") == Apple("red") 应该给出错误。我们能否以一种优雅的方式在特质 Fruit== 签名中强制执行这一点?

编辑:我希望在编译时捕获错误,而不是在运行时捕获。

Refer to the following code snippet:

 trait Fruit {
   val color:String
   def == (fruit:Fruit) = this.color == fruit.color
 }

 case class Orange(color:String) extends Fruit 

 case class Apple(color:String) extends Fruit

As expected, Orange("red") == Orange("red") is true. However, I would like to enforce that only the same type of fruits can be compared, so for instance Orange("red") == Apple("red") should give an error. Can we enforce this in the signature of == in trait Fruit in an elegant way?

EDIT: I want the error to be caught at compile time, not at runtime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木森分化 2024-10-27 16:52:41

Scalaz 有一个 Equal “类型类”来解决这个问题,尽管使用了不同的运算符。

https://github.com/scalaz/scalaz/blob/ master/core/src/main/scala/scalaz/Equal.scala

它的核心基本上是这样的(尽管我在他们使用一些 unicode 的地方使用 === )

/** Defines a type safe === operator */
trait Equals[A] {
 def ===(y : A) : Boolean
}

/** A conventient way to define Equals traits based on the == operator */
def equalA[A](x : A) = new Equals[A] {
  def ===(y : A) = x == y
}

并且像这样使用

// one for oranges
implicit val EqualsOrange = equalA[Orange] _

// one for apples
implicit val EqualsApple = equalA[Apple] _


Orange("red") === Orange("red") // true

Orange("red") === Orange("green") // false

Orange("red") === Apple("red") // Compile error

Scalaz has an Equal "type class" that solves this problem, albeit with a different operator.

https://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/Equal.scala

The heart of it is basically this (although I use === where they use some unicode)

/** Defines a type safe === operator */
trait Equals[A] {
 def ===(y : A) : Boolean
}

/** A conventient way to define Equals traits based on the == operator */
def equalA[A](x : A) = new Equals[A] {
  def ===(y : A) = x == y
}

And is used like so

// one for oranges
implicit val EqualsOrange = equalA[Orange] _

// one for apples
implicit val EqualsApple = equalA[Apple] _


Orange("red") === Orange("red") // true

Orange("red") === Orange("green") // false

Orange("red") === Apple("red") // Compile error
微凉徒眸意 2024-10-27 16:52:41

不幸的是,您无法静态地检查这一点...至少不能使用 ==,它使用 Java 的 Object#equals 方法,其中所有内容都强调以原始方式定义对象。

如果您想要类型安全,那么您唯一的选择是实现另一个运算符,可能类似于 =|=,然后将其与类型类结合起来以确保您的安全。

我相信 scalaz 对于类型安全相等也有一些有用的东西,但对这个库的了解还不足以肯定地说明这一点。

您可以采取的另一种方法(仅在运行时安全)是使用 canEqual 模式 如此处所述。这已经被案例类使用,并提供了一种在适合相等时选择性地破坏 LSP 的好方法。

Unfortunately, you can't check this statically... At least, not using ==, which uses Java's Object#equals method where everything is emphatically defined in terms of raw objects.

If you want type safety, then your only choice is to implement another operator, perhaps something like =|=, then combine this with type classes to ensure your safety.

I believe that scalaz also has something useful for type-safe equality, but don't know the library well enough to state this for certain.

The other approach you can take, which will only be safe at runtime, is to use the canEqual pattern as described here. This is already used by case classes and offers a nice way to selectively break the LSP when it's appropriate for equality.

丶情人眼里出诗心の 2024-10-27 16:52:41

如果您愿意将该方法的名称从 == 更改为其他名称,我们可以这样做:

trait Fruit {
 type FruitType <: Fruit
 val color:String
 def === (fruit:FruitType) = this.color == fruit.color

}


case class Orange(color:String) extends { type FruitType = Orange } with Fruit 

case class Apple(color:String) extends {type FruitType = Apple } with Fruit

然后,如果我们将 Apples 与 Oranges 进行比较,我们会得到:

Apple("red") === Orange("red")
<console>:11: error: type mismatch;
 found   : Orange
 required: Apple
       Apple("red") === Orange("red")

Apples 与 Apples of the same color 我们会得到:

Apple("red") === Apple("red")
res10: Boolean = true

Apples with Apples of a different我们得到的颜色:

Apple("green") === Apple("red")
res11: Boolean = false

If you are happy to change the name of the method from == to something else we could do this:

trait Fruit {
 type FruitType <: Fruit
 val color:String
 def === (fruit:FruitType) = this.color == fruit.color

}


case class Orange(color:String) extends { type FruitType = Orange } with Fruit 

case class Apple(color:String) extends {type FruitType = Apple } with Fruit

Then if we compare Apples with Oranges we get:

Apple("red") === Orange("red")
<console>:11: error: type mismatch;
 found   : Orange
 required: Apple
       Apple("red") === Orange("red")

and Apples with Apples of the same color we get:

Apple("red") === Apple("red")
res10: Boolean = true

and Apples with Apples of a different color we get:

Apple("green") === Apple("red")
res11: Boolean = false
再见回来 2024-10-27 16:52:41

尝试:

 trait Fruit {
   val color: String
   def == (fruit: Fruit) = getClass == fruit.getClass && color == fruit.color
 }

Try:

 trait Fruit {
   val color: String
   def == (fruit: Fruit) = getClass == fruit.getClass && color == fruit.color
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文