关于 joinLeft of Each 的类型约束和具体化 [英] type constraints and reifications regarding to joinLeft of Either

查看:49
本文介绍了关于 joinLeft of Each 的类型约束和具体化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

joinLeft 定义为:

abstract class Either[+A, +B]

def joinLeft[A1 >: A, B1 >: B, C](implicit ev: A1 <:< Either[C, B1]):
    Either[C, B1] = this match {
    case Left(a)  => a
    case Right(b) => Right(b)
}

  • 使用已知的AB,我们需要一个隐式ev: A1 <:<要么[C, B1]

    • With the known A and B, we need an implicit ev: A1 <:< Either[C, B1] that

      1. 满足约束A1 >: A, B1 >: B.
      2. 具体化A1 <:Either[C, B1]

    • 为此,我们需要隐式conforms[A1]conforms[Either[C, B1]]

    • 如果我到现在都还对的话,在我看来,A1B1 有很多选择,只要它们超出下界 AB.所以我想知道 Scala 如何给我们 A1Either[C, B1](以及它们是什么),以便我们获得隐式 conforms 方便 <:< 完成断言 A1 <:Either[C, B1] 的工作.

      If I'm still right until now, there seem to me many choices for A1 and B1 as long as they are beyond lower bounds A and B. So I would like to know how scala gives us A1 and Either[C, B1](and what they are) so that we get implicit conforms to facilitate <:< to do its job of asserting A1 <: Either[C, B1].

      附言
      我认为这个问题与我的另一个问题joinLeft [A1 >: A, B1 >: B, C]... 为什么类型约束 A1 >: A and B1>: B 是必要的?".如果有人也可以看一下,我将不胜感激.

      P.S.
      I think this question is somewhat related to my another one "joinLeft [A1 >: A, B1 >: B, C]… why are type constraint A1 >: A and B1>: B necessary?". I would appreciate if anyone can also take a look at it.

      推荐答案

      你说得对,有很多选择.一般来说,泛型方法的类型推断使用参数来确定泛型参数的值

      You are right, there are many choices. In general, type inference on generic method uses the arguments to determine the value of the generic parameter

      def myMethod[A](aList:List[A])
      

      然而,这并不是使用泛型参数类型推断的唯一方法.值得注意的是,类型参数可以是:

      However, this is not the only way to use type inference of generic arguments. Notably, type parameters can be :

      • 明确的
      • 使用预期结果类型确定

      在这种情况下,由于无法从参数中确定泛型类型参数(因为没有显式参数),您通常将方法的结果分配给类型化变量,或者在方法的末尾使用它具有显式返回类型.

      In this case, as the generic type parameter cannot be determined from the arguments (because there are no explicit arguments), you typically assign the result of the method to a typed variable, or you use it at the end of a method with explicit return type.

      您可能会问自己 A1 将如何解决,这是一个非常有趣的问题.事实上,A1 不存在于输入类型中,也不存在于输出类型中.它有什么用?

      You might ask yourself how A1 will be solved and this is a very interesting question. In fact A1 is not present in the input type, nor in the output type. What is it useful for?

      答案在以下定义中,来自Predef.scala

      The answer is in the following definition, coming from Predef.scala

      @implicitNotFound(msg = "Cannot prove that ${From} <:< ${To}.")
        sealed abstract class <:<[-From, +To] extends (From => To) with Serializable
      

      由于 <:<From 方法中是逆变的

      Since the <:< is contravariant in From the method

      def joinLeft[B1 >: B, C](implicit ev: A <:< Either[C, B1]):
          Either[C, B1] = this match {
          case Left(a)  => a
          case Right(b) => Right(b)
      }
      

      不会正确处理 A 上的子类化.这就是为什么你需要一个额外的泛型类型参数 A1,它由 Scala 编译器通过使用 A1 来解析,它给出了最高优先级的隐式.

      won't handle correctly subclassing on A. That's why you need an extra generic type parameter A1, which is resolved by the Scala compiler by using the A1 which gives the implicit with the highest priority.

      这篇关于关于 joinLeft of Each 的类型约束和具体化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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