首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >scala能够推断类型构造函数抽象类型成员吗?

scala能够推断类型构造函数抽象类型成员吗?
EN

Stack Overflow用户
提问于 2017-10-23 01:27:58
回答 1查看 253关注 0票数 0

例如,我正在尝试提取隐式参数(a la Shapeless)的抽象类型成员

代码语言:javascript
复制
trait F[T] { type Out }

object F {
  type Aux[T, out] = F[T] { type Out = out }
}

def glhf[t, out](implicit f: F.Aux[t, out]): out = ???

这对于任何类型的提取(甚至复杂的交叉隐式类型变量)都很有吸引力。

但是,当抽象类型成员是类型构造函数而不是简单类型时,编译器无法在调用点统一类型变量。

我做了一个小的测试案例,里面有一个奇怪的编译错误。编译器错误本身没有多大意义,所以我想知道这是否是编译器错误?有关错误消息的详细信息,请参阅代码示例。

scala-2.12.4编译,用-Xlog-implicits编译额外的消息,甚至用-Ypartial-unification编译,以防出现问题。

incubator/Main.scala

代码语言:javascript
复制
package incubator

object wat {

  /**
   * A "type class", "implicit evidence" type, etc...
   *
   * @tparam t just for looks, and facilitate
   *  the implicit resolution scenario
   */
  trait fo[t] {
    /**
     * An abstract type member THAT IS A TYPE CONSTRUCTOR
     */
    type f[_]
  }

  //
  // Types that will be used for `fo`'s abstract type `f[_]`
  //
  trait F1[t]
  trait F2[t]
  //
  // Couple of case for type class `fo`
  //
  trait loo
  implicit object loo extends loo with fo[loo] {
    type f[t] = F1[t]
  }
  //
  trait poo
  implicit object poo extends poo with fo[poo] {
    type f[t] = F2[t]
  }

  // Double checking, this compiles
  val w0 = implicitly[ fo[loo] ]
  val w1 = implicitly[ fo[poo] ]

  /**
   * *** PROBLEM HERE ***
   * 
   * A method call, in which the abstract TYPE CONSTRUCTOR type member
   * needs to be inferred by the compiler.
   *
   * This fails to be implicitly resolved, because the compiler
   * fails to instantiate the type parameters, (probably) because
   * it is unable to infer abstract type `f`. See further below
   * for the failed invocation.
   *
   */
  def fu0[t, in[_]](t: t)(
    implicit
    fo: fo[t] { type f[a] = in[a] }
  ): String = s"Hi $t: $fo"

  // These will work fine, since we explicitly set type param `in`
  val w2 = fu0[loo, F1](loo: loo)
  val w3 = fu0[poo, F2](poo: poo)

  // *** PROBLEM HERE ***
  // The following fails to compile
  val w4 = fu0(loo: loo) // type ascription for test simplification
  val w5 = fu0(poo: poo) // type ascription for test simplification

  //
  // Error message:
  //
  // (notice the "type f has one type parameter, but type in has one"
  //  part of the error)
  //
  // [info] .../incubator/Main.scala:64:15: poo is not a valid implicit value for incubator.wat.fo[incubator.wat.poo]{type f[a] = in[a]} because:
  // [info] type parameters weren't correctly instantiated outside of the implicit tree: inferred kinds of the type arguments (incubator.wat.poo.f[t]) do not conform to the expected kinds of the type parameters (type in).
  // [info] incubator.wat.poo.f[t]'s type parameters do not match type in's expected parameters:
  // [info] type f has one type parameter, but type in has one
  // [info]   val w5 = fu0(poo: poo) // type ascription for test simplification
  // [info]               ^
  // [info] .../incubator/Main.scala:64:15: incubator.this.wat.poo is not a valid implicit value for incubator.wat.fo[incubator.wat.poo]{type f[a] = in[a]} because:
  // [info] type parameters weren't correctly instantiated outside of the implicit tree: inferred kinds of the type arguments (incubator.wat.poo.f[t]) do not conform to the expected kinds of the type parameters (type in).
  // [info] incubator.wat.poo.f[t]'s type parameters do not match type in's expected parameters:
  // [info] type f has one type parameter, but type in has one
  // [info]   val w5 = fu0(poo: poo) // type ascription for test simplification
  // [info]               ^
  // [error] .../incubator/Main.scala:64:15: could not find implicit value for parameter fo: incubator.wat.fo[incubator.wat.poo]{type f[a] = in[a]}
  // [error]   val w5 = fu0(poo: poo) // type ascription for test simplification
  // [error]               ^
  // [error] two errors found
  // [error] (compile:compileIncremental) Compilation failed
  // [error] Total time: 1 s, completed Oct 22, 2017 4:48:35 PM
  //

}
EN

回答 1

Stack Overflow用户

发布于 2017-10-23 02:29:35

通过在fu0中使用下面的定义,您基本上重新编写了fo,这样编译器就不会确切地知道发生了什么。

代码语言:javascript
复制
implicit
fo: fo[t] { type f[a] = in[a] }

您已经定义了fo,通过使用该静态特征,您将不必使用显式类型。

代码语言:javascript
复制
def fu0[t, in[_]]
    (t: t)
    (implicit fo: fo[t] ): String = s"Hi $t: $fo"

它在引起争议的示例上工作,但我假设在您的实际情况中,问题存在于嵌套类型的更深层次。如果我的评估是准确的,请提供更具体的测试案例,以便我们可以研究它。这是一个相当有趣的话题。

复制

使用scalaVersion := "2.12.4"清理设置

代码语言:javascript
复制
package io.sosc

object Main {

    trait fo[t] {

        type f[_]
    }

    trait F1[t]
    trait F2[t]

    trait loo

    implicit object loo extends loo with fo[loo] {
        type f[t] = F1[t]
    }

    trait poo

    implicit object poo extends poo with fo[poo] {
        type f[t] = F2[t]
    }

    def fu0[t, in[_]]
        (t: t)
        (implicit fo: fo[t] ): String = s"Hi $t: $fo"


    def main( args: Array[ String ] ): Unit = {



        val w0 = implicitly[ fo[loo] ]
        val w1 = implicitly[ fo[poo] ]

        val w2 = fu0[loo, F1](loo: loo)
        val w3 = fu0[poo, F2](poo: poo)

        println( w2 )
        println( w3 )

        val w4 = fu0(loo: loo)
        val w5 = fu0(poo: poo)

        println( w4 )
        println( w5 )
    }
}

结果:

代码语言:javascript
复制
Hi io.sosc.Main$loo$@465ba3d7: io.sosc.Main$loo$@465ba3d7
Hi io.sosc.Main$poo$@675b0a69: io.sosc.Main$poo$@675b0a69
Hi io.sosc.Main$loo$@465ba3d7: io.sosc.Main$loo$@465ba3d7
Hi io.sosc.Main$poo$@675b0a69: io.sosc.Main$poo$@675b0a69
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46877110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档