类型别名避免类型优化中的名称冲突 [英] Type aliasing to avoid name clash in type refinement

查看:214
本文介绍了类型别名避免类型优化中的名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用别名,因此在以下名称冲突情况下,我不需要更改任何类型参数/成员:

Can I use aliasing so I don't need to change either of the type parameter/member in the following name clash situation:

trait Bar {
  type A
}

trait Foo {
  def get[A]: Option[Bar { type A = A }]  // "illegal cyclic reference"
}

我知道我会写

trait Foo {
  def get[A1]: Option[Bar { type A = A1 }]
}

但是我真的不希望更改类型名称.

But I would really prefer not to change the type name.

推荐答案

您可以例如做这样的事情:

You could e.g. do something like this:

trait Bar {
  type A
}

trait Foo {
  type M[X] = Bar { type A = X }
  def get[A]: Option[M[A]]
}

或内联:

def get[A]: Option[({ type X[Y] = Bar { type A = Y }})#X[A]] = ???

或者,如果您愿意:

def get[A]: Option[({ type A1 = A; type X = Bar { type A = A1 }})#X] = ???

这篇关于类型别名避免类型优化中的名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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