scalaz 中的类型类和继承 [英] Typeclasses and inheritance in scalaz

查看:24
本文介绍了scalaz 中的类型类和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第二次尝试定义问题,我无法理解它.

This is my second try to define the problem, I can't get my head around it.

我希望能够定义一个代数类型并在其上定义一个简单的类型类,比如 Show.在haskell中,我这样做:

I want to be able to define an algebraic type and define a simple typeclass over it, let's say Show. In haskell I do:

data Tree a = EmptyTree | Node a deriving (Show)

现在,如果我输入 EmptyTree - haskell 可以显示它,所以它属于 Show.

Now, if I type EmptyTree - haskell can show it, so it belongs to Show.

现在我正在尝试在 Scala 中做同样的事情:

Now I am trying to do the same in scala:

sealed abstract class Tree[+T]
case object EmptyTree extends Tree[Nothing]
case class Node[T](value: T) extends Tree[T]

然后我在它周围定义Show:

implicit def show[T] = Show.showA[Tree[T]]

我可以println((EmptyTree : Tree[Int]).show).但我不能做 println(EmptyTree.show) (响应是 value show is not a member of object EmptyTree)

I can do println((EmptyTree : Tree[Int]).show). But I can't do println(EmptyTree.show) (response is value show is not a member of object EmptyTree)

我必须写额外的:

implicit class MyShowOps[A, +T <: Tree[A]](t: T) {
  def showMy(implicit ev: Show[Tree[A]]): String = ev.shows(t)
}

只有这样我才能做println(EmptyTree.showMy)

听起来仍然不正确,我相信要么我试图做错事,我不应该像那样应用 Show 并且应该仅将我的构造用作 Tree[T] 或者我缺少 Scalaz 的正确构造.

It still doesn't sound correct, I believe either I am trying to do a wrong thing and I am not supposed to apply Show like that and should use my construction only as Tree[T] or I am missing a proper construction from Scalaz.

推荐答案

Scala 的 ADT 表示与 Haskell 的不同之处在于它的构造函数有自己的类型.这部分是关于实际的互操作性——在 JVM 上使用子类型是很自然的——并且它具有优点和缺点.

Scala's representation of ADTs differs from Haskell's in that its constructors have their own types. This is partly about practical interoperability—using subtyping is natural on the JVM—and it has both advantages and disadvantages.

您遇到了一个缺点,即具有静态类型化为构造函数类型的值通常会使类型推断和隐式解析复杂化.

You're running into one of the disadvantages, which is that having values that are statically typed as a constructor type often complicates type inference and implicit resolution.

类型类实例是静态解析的,在您的情况下,特别是 Show 不是逆变的,因此 Tree[T] 的实例不是 Tree[T] 的实例代码>EmptyTree.type.从 Scalaz 的角度来看,最惯用的解决方案是提供返回 ADT 类型的智能构造函数:

Type class instances are statically resolved, and in your case specifically Show isn't contravariant, so an instance for Tree[T] isn't an instance for EmptyTree.type. The most idiomatic solution from the Scalaz perspective is to provide smart constructors that return the ADT type:

import scalaz.Show, scalaz.syntax.show._

sealed abstract class Tree[+T]

object Tree {
  private[this] case object EmptyTree extends Tree[Nothing]
  private[this] case class Node[T](value: T) extends Tree[T]

  val emptyTree: Tree[Nothing] = EmptyTree
  def node[T](value: T): Tree[T] = Node(value)

  implicit def show[T]: Show[Tree[T]] = Show.showA[Tree[T]]
}

现在您可以编写Tree.emptyTree.show.

请注意,这个问题也会出现在更简单的上下文中.例如,假设我们要以 Option 作为累加器来折叠一个列表:

Note that this problem also turns up in even simpler contexts. For example, suppose we want to fold over a list with an Option as the accumulator:

scala> List(1, 2, 3).foldLeft(Some(0))((acc, i) => acc.map(_ + i))
<console>:11: error: type mismatch;
 found   : Option[Int]
 required: Some[Int]
       List(1, 2, 3).foldLeft(Some(0))((acc, i) => acc.map(_ + i))
                                                          ^

因为 Some(0) 的推断类型是 Some[Int],而不是 Option[Int],推断的类型参数foldLeft 方法对 map 的结果限制太大.

Because the inferred type for Some(0) is Some[Int], not Option[Int], the type parameter that's inferred for the foldLeft method is too restrictive for the result of the map.

如果标准库为这种情况提供了 Option.noneOption.some 构造函数",那就太好了,但事实并非如此,所以你要么必须在第一个参数上添加类型注释或使用类似 Scalaz 的 nonesome 之类的东西:

It would be nice if the standard library provided Option.none and Option.some "constructors" for cases like this, but it doesn't, so you either have to put a type annotation on the first argument or use something like Scalaz's none and some:

scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._

scala> List(1, 2, 3).foldLeft(some(0))((acc, i) => acc.map(_ + i))
res0: Option[Int] = Some(6)

在您的情况下,您大概可以控制 ADT 定义,因此您可以自己提供这样的智能构造函数.

In your case you presumably control the ADT definition, so you can provide smart constructors like this yourself.

这篇关于scalaz 中的类型类和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆