部分应用类型参数
我拼命地试图解决以下问题:
trait Access[Res[_]] { def access[C]: Res[C] }
trait CList[C1, A] extends Access[CList[_, A]] // ?!
def test[C1, C2, A](c: CList[C1, A]): CList[C2, A] = c.access[C2]
scalac 只是说:“错误:涉及特征 CList 的非法循环引用”
。我怎样才能编译这个?
I'm desperately trying to solve the following:
trait Access[Res[_]] { def access[C]: Res[C] }
trait CList[C1, A] extends Access[CList[_, A]] // ?!
def test[C1, C2, A](c: CList[C1, A]): CList[C2, A] = c.access[C2]
scalac just says: "error: illegal cyclic reference involving trait CList"
. how can I make this compile?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能对 lambda 类型感兴趣。您在答案中使用的部分应用程序实际上是 在 scalaz 中实现< /a>.
然而,由于代码的可读性往往较差,他们开始使用 lambda 类型。有问题的类型可以写为:
这通过在结构类型内部的参数化类型
λ
上创建类型投影来实现,从而捕获外部类型参数(在本例中为A
) 。您的答案中描述的有关方差的另一个问题可以通过将
Access
中的Res
参数设置为协变来解决。进行这些更改后,您的代码应如下所示:
You might be interested in type lambdas. The partial application you used in your answer is actually implemented in scalaz.
As the code tends to get less readable though, they started using type lambdas instead. The type in question could be written as
This works by creating a type projection on a parameterized type
λ
inside a structural type thus capturing the outer type parameter (in this caseA
).The other problem concerning variance described in your answer could be solved by making the
Res
parameter inAccess
covariant.After these changes your code should look like this:
只是为了更新一些东西
将此编译器插件添加到您的sbt中以进行种类投影,您将使用
?
获得一个不错的语法。这删除了看起来很混乱的类型投影样板!
所以你可以编写类似
Either[String, ?]
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.7 ")
它是使用下面相同的旧类型投影实现的
您还可以在这里找到它:
https://underscore.io/blog/posts/2016/12/05/type-lambdas.html
Just to update things
add this compiler plugin to your sbt for kind projection and you'll get a nice syntax using
?
.This removes the type projection boilerplate which looks messy!
So you can write stuff like
Either[String, ?]
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.7")
it's implemented with the same old type projection underneath
You can also find it here:
https://underscore.io/blog/posts/2016/12/05/type-lambdas.html
谷歌搜索“部分类型应用程序”,我发现 James Iry 在 scala 辩论列表上发布了这个解决方案( http://scala-programming-language.1934581.n4.nabble.com/Partial-type-inference-td2007311.html ;进行了调整,以便更改参数顺序) :
cheese louise,这真的是 2011 年在 scala 中做到这一点的唯一方法吗?!!
编辑:
这会因
A
中的协方差而失败:,-(googling for "partial type application" i found this solution posted by James Iry on the scala debate list ( http://scala-programming-language.1934581.n4.nabble.com/Partial-type-inference-td2007311.html ; adapted so the arg order is changed):
cheese louise, is this really the only way to do that in scala in 2011 ?!!
EDIT:
This fails with covariance in
A
:,-(我知道这是一个非常老的问题,但无论如何:
I know this is a really old question, but anyway:
这是一种对我“部分应用类型参数”有用的方法:
我有一个像这样的函数
,我只需要提示一个类型参数,以便编译器推断其余的类型参数。这对我有用:
更新 foo 以获取 InferType 类型的附加参数:
用法:
Here's a method that worked for me to "partially apply type parameters":
I had a function like
Such that I needed to hint only one type parameter for the compiler to infer the rest. This worked for me:
Update foo to take an additional parameter of type InferType:
Usage: