我试图提取隐式参数的抽象类型成员(a la Shapeless),例如
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
:
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
//
}
答案 0 :(得分:0)
通过在fu0
中使用以下定义,您基本上重新构建fo
,因此编译器并不确切知道发生了什么。
implicit
fo: fo[t] { type f[a] = in[a] }
您已经定义了fo
,并且通过使用该静态特征,您不必使用显式输入。
def fu0[t, in[_]]
(t: t)
(implicit fo: fo[t] ): String = s"Hi $t: $fo"
它适用于受到启发的示例,但我认为在您的实际案例中,问题在于嵌套类型更深层次。如果我的评估准确无误,请提供更具体的测试用例,以便我们对其进行调查。这是一个非常有趣的话题。
<强>生殖强>
使用scalaVersion := "2.12.4"
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 )
}
}
结果:
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