在Scala中,是否存在任何条件无法将隐式视图传播到其他隐式函数? [英] In scala, are there any condition where implicit view won't be able to propagate to other implicit function?

查看:97
本文介绍了在Scala中,是否存在任何条件无法将隐式视图传播到其他隐式函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设定义了一个名为"summoner"的类,该类能够从作用域中调用隐式视图:

Assuming that A class called 'summoner' was defined, that is capable of summoning implicit views from the scope:

  case class Summoner[R]() {

    def summon[T](v: T)(implicit ev: T => R): R = ev(v)
  }

我发现它在大多数情况下都有效,但是在某些情况下它不起作用,例如以下是(不是太)使用singleton-ops库的简短案例:

I found that it works most of the time, but there are cases where it doesn't work, e.g. the following is a (not too) short case which uses the singleton-ops library:


import shapeless.Witness
import singleton.ops.+
import singleton.ops.impl.Op

  trait Operand {

    def +[
        X >: this.type <: Operand,
        Y <: Operand
    ](that: Y): Op2[X, Y] = {

      Op2[X, Y](this, that)
    }
  }

  object Operand {

    abstract class ProvenToBe[O <: Arity]()(implicit val out: O) extends Operand {}

    object ProvenToBe {

      implicit class Trivial[O <: Arity, T <: ProvenToBe[O]](
          val self: T
      ) extends Proof {

        override type Out = O

        override def out: Out = self.out
      }
    }
  }

  trait Proof extends Serializable {

    def self: Operand

    type Out <: Arity

    def out: Out
  }

  object Proof {

    trait Out_=[+O <: Arity] extends Proof {
      type Out <: O
    }

    trait Invar[S] extends Out_=[Arity.Const[S]] {

      type SS = S
    }
  }

  trait Arity extends Operand {}

  object Arity {

    trait Const[S] extends Arity {

      type SS = S
    }

    object Const {

      implicit class Same[S](val self: Const[S]) extends Proof.Invar[S] {
        override type Out = Const[S]

        override def out: Const[S] = self
      }
    }

    class FromOp[S <: Op]() extends Const[S]

    object FromOp {

      implicit def summon[S <: Op](implicit s: S): FromOp[S] = new FromOp[S]()
    }

    class FromLiteral[S <: Int](val w: Witness.Lt[Int]) extends Const[S] {}

    object FromLiteral {

      implicit def summon[S <: Int](implicit w: Witness.Aux[S]): FromLiteral[S] =
        new FromLiteral[S](w)
    }

    def apply(w: Witness.Lt[Int]): FromLiteral[w.T] = {

      FromLiteral.summon[w.T](w) //TODO: IDEA inspection error
    }

  }

  case class Op2[
      +A1 <: Operand,
      +A2 <: Operand
  ](
      a1: A1,
      a2: A2
  ) extends Operand {}

  object Op2 {

    implicit class ProveInvar[
        A1 <: Operand,
        A2 <: Operand,
        S1,
        S2
    ](
        val self: Op2[A1, A2]
    )(
        implicit
        bound1: A1 => Proof.Invar[S1],
        bound2: A2 => Proof.Invar[S2]
    ) extends Proof.Invar[S1 + S2] {

      override type Out = Arity.FromOp[S1 + S2]

      override def out: Out = new Arity.FromOp[S1 + S2]()
    }
  }

尝试按原样使用隐式视图时:

When attempting to use the implicit view as-is:

  implicit val a = Arity(3)
  implicit val b = Arity(4)

  val op = a + b

  op: Proof // implicit view works

但是在使用召唤师时:

  val summoner = Summoner[Proof]()

  summoner.summon(op) // oops

[Error] /home/peng/git/shapesafe/spike/src/main/scala/edu/umontreal/kotlingrad/spike/arity/package.scala:141: No implicit view available from edu.umontreal.kotlingrad.spike.arity.package.Op2[edu.umontreal.kotlingrad.spike.arity.package.Arity.FromLiteral[Int(3)],edu.umontreal.kotlingrad.spike.arity.package.Arity.FromLiteral[Int(4)]] => edu.umontreal.kotlingrad.spike.arity.package.Proof.
one error found

FAILURE: Build failed with an exception.

此错误消息看起来很平淡,几乎类似于常见的隐式类型不匹配错误,但是以前的用法已经消除了这种可能性.所以我的问题是:

This error message looks quite bland, almost resembles a common implicit type mismatch error, but previous usage has already culled out that possibility. So my questions are:

  1. 此行为是什么原因?

  1. What's the cause of this behaviour?

你怎么知道的?

推荐答案

我告诉过您有关使用reify-Xlog-implicits调试隐式函数以及在

I told you about debugging implicits with reify, -Xlog-implicits and manual resolution of implicits in In scala 2 or 3, is it possible to debug implicit resolution process in runtime?

如果您打印树

import scala.reflect.runtime.universe._
println(reify{
  op: Proof
}.tree)

您将看到如何解决隐式转换

you'll see how implicit conversion is resolved

(App.this.Op2.ProveInvar(App.this.op)(((self) => Arity.this.Const.Same(self)), ((self) => Arity.this.Const.Same(self))): App.this.Proof)

确实是手动解决的

summoner.summon[Op2[Arity.FromLiteral[3], Arity.FromLiteral[4]]](op)(t =>
  Op2.ProveInvar(t)(a1 => Arity.Const.Same(a1), a2 => Arity.Const.Same(a2))
)

可以编译,但是编译器本身找不到隐式转换

compiles but compiler itself can't find implicit conversion

summoner.summon[Op2[Arity.FromLiteral[3], Arity.FromLiteral[4]]](op) //doesn't compile

如果打开-Xlog-implicits,您将看到详细信息

If you switch -Xlog-implicits on you'll see details

Information: $conforms is not a valid implicit value for App.Arity.FromLiteral[3] => App.Proof.Invar[Nothing] because:
hasMatchingSymbol reported error: type mismatch;
 found   : App.Arity.FromLiteral[3] => App.Arity.FromLiteral[3]
 required: App.Arity.FromLiteral[3] => App.Proof.Invar[Nothing]
  summoner.summon[Op2[Arity.FromLiteral[3], Arity.FromLiteral[4]]](op)

Information: Arity.this.Const.Same is not a valid implicit value for App.Arity.FromLiteral[3] => App.Proof.Invar[Nothing] because:
hasMatchingSymbol reported error: type mismatch;
 found   : App.Arity.Const[Nothing] => App.Arity.Const.Same[Nothing]
 required: App.Arity.FromLiteral[3] => App.Proof.Invar[Nothing]
  summoner.summon[Op2[Arity.FromLiteral[3], Arity.FromLiteral[4]]](op)

Information: App.this.Op2.ProveInvar is not a valid implicit value for App.Op2[App.Arity.FromLiteral[3],App.Arity.FromLiteral[4]] => App.Proof because:
hasMatchingSymbol reported error: No implicit view available from App.Arity.FromLiteral[3] => App.Proof.Invar[Nothing].
  summoner.summon[Op2[Arity.FromLiteral[3], Arity.FromLiteral[4]]](op)

正如我在

As I told you in When calling a scala function with compile-time macro, how to failover smoothly when it causes compilation errors? you can not always check existence of implicit conversion with implicit parameter (implicit ev: T => R). Sometimes existence of implicit instance T => R is not the same as existence of implicit conversion T => R (not all implicit conversions are typeclass-based). Try to replace

val summoner = Summoner[Proof]()
summoner.summon(op) //doesn't compile

使用

summonImplicitView[Proof](op) //compiles

def summonImplicitView[B] = new PartiallyAppliedSummonImplicitView[B]

class PartiallyAppliedSummonImplicitView[B] {
  def apply[A](a: A): B = macro summonImplicitViewImpl[A, B]
}

def summonImplicitViewImpl[A: c.WeakTypeTag, B: c.WeakTypeTag](c: whitebox.Context)(a: c.Tree): c.Tree = {
  import c.universe._
  val tpA = weakTypeOf[A]
  val tpB = weakTypeOf[B]
  val view = c.inferImplicitView(tree = a, from = tpA, to = tpB, silent = false)
  q"$view($a)"
}

您还可以尝试从,但是此类型类并非始终有效,因为它是基于类型的,并且并非所有隐式转换都是基于类型的,它在隐式解析期间会忽略v的值.

but this type class will work not always because it's type-based and not all implicit conversions are type-based, it ignores value of v during implicit resolution.

我想我终于找到了这个问题(这样,如果我们修复它,Summoner将可以在没有宏的情况下使用).您再次失去了类型优化功能.

I guess I finally found the issue (such that if we fix it Summoner will work without macros). You again lost type refinement.

case class Summoner[R]() {
  def summon[T](v: T)(implicit ev: T => R): R = ev(v)
}

val summoner = Summoner[Proof {type Out <: Arity.FromOp[3 + 4]}]() 

// or even
//val summoner = Summoner[Proof {type Out <: Arity.FromOp[3 + 4]; type SS = 3 + 4}]()

summoner.summon(op) //compiles

这就是为什么您在-Xlog-implicits日志中包含Nothing的原因.

That's why you had Nothings in -Xlog-implicits logs.

我想我已经修复了您的代码.在编写逻辑时,您将隐式实例与隐式转换混合在一起.隐式转换是棘手的.我建议仅根据类型类(MyTransform)编写逻辑,然后如果需要转换,请针对这些类型类定义它们(myConversion).

I guess I fixed your code. While writing your logic you mixed implicit instances with implicit conversions. Implicit conversions are tricky. I would recommend to write your logic only in terms of type classes (MyTransform) and then if you need conversions define them (myConversion) with respect to these type classes.

// doesn't extend T => R intentionally
trait MyTransform[-T, +R] {
  def transform(v: T): R
}
implicit def myConversion[T, R](v: T)(implicit mt: MyTransform[T, R]): R = mt.transform(v)

case class Summoner[R]() {    
  def summon[T](v: T)(implicit ev: MyTransform[T, R]): R = ev.transform(v)
}

trait Operand {  
  def +[
    X >: this.type <: Operand,
    Y <: Operand
  ](that: Y): Op2[X, Y] = {    
    Op2[X, Y](this, that)
  }
}
object Operand {   
  abstract class ProvenToBe[O <: Arity]()(implicit val out: O) extends Operand {}    
  object ProvenToBe {   
    implicit def trivial[O <: Arity, T <: ProvenToBe[O]]: MyTransform[T, Trivial[O, T]] = self => new Trivial(self)

    /*implicit*/ class Trivial[O <: Arity, T <: ProvenToBe[O]](
      val self: T
    ) extends Proof {   
      override type Out = O
      override def out: Out = self.out
    }
  }
}

trait Proof extends Serializable {    
  def self: Operand
  type Out <: Arity
  def out: Out
}
object Proof {
  trait Out_=[+O <: Arity] extends Proof {
    type Out <: O
  }

  trait Invar[S] extends Out_=[Arity.Const[S]] {
    type SS = S
  }
}

trait Arity extends Operand {}
object Arity {
  trait Const[S] extends Arity {
    type SS = S
  }
  object Const {
    implicit def same[S]: MyTransform[Const[S], Same[S]] = self => new Same(self)

    /*implicit*/ class Same[S](val self: Const[S]) extends Proof.Invar[S] {
      override type Out = Const[S]
      override def out: Const[S] = self
    }
  }

  class FromOp[S <: Op]() extends Const[S]
  object FromOp {
    implicit def summon[S <: Op](implicit s: S): FromOp[S] = new FromOp[S]()
  }

  class FromLiteral[S <: Int](val w: Witness.Lt[Int]) extends Const[S] {}
  object FromLiteral {
    implicit def summon[S <: Int](implicit w: Witness.Aux[S]): FromLiteral[S] =
      new FromLiteral[S](w)
  }

  def apply(w: Witness.Lt[Int]): FromLiteral[w.T] = {
    FromLiteral.summon[w.T](w) //TODO: IDEA inspection error
  }
}

case class Op2[
  +A1 <: Operand,
  +A2 <: Operand
](
   a1: A1,
   a2: A2
 ) extends Operand {}
object Op2 {
  implicit def proveInvar[A1 <: Operand, A2 <: Operand, S1, S2](implicit
    bound1: MyTransform[A1, Proof.Invar[S1]],
    bound2: MyTransform[A2, Proof.Invar[S2]]
  ): MyTransform[Op2[A1, A2], ProveInvar[A1, A2, S1, S2]]
  = self => new ProveInvar(self)

  /*implicit*/ class ProveInvar[
    A1 <: Operand,
    A2 <: Operand,
    S1,
    S2
  ](
     val self: Op2[A1, A2]
   )/*(
     implicit
     bound1: A1 => Proof.Invar[S1],
     bound2: A2 => Proof.Invar[S2]
   )*/ extends Proof.Invar[S1 + S2] {
    override type Out = Arity.FromOp[S1 + S2]
    override def out: Out = new Arity.FromOp[S1 + S2]()
  }
}

implicit val a = Arity(3)
implicit val b = Arity(4)

val op = a + b

op: Proof // compiles

val summoner = Summoner[Proof]()
summoner.summon(op) // compiles

这篇关于在Scala中,是否存在任何条件无法将隐式视图传播到其他隐式函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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