Type aliasing to avoid name clash in type refinement

若如初见. 提交于 2019-12-10 20:02:02

问题


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"
}

I know I can write

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

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


回答1:


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]]
}

Or inline:

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

Or, if you prefer:

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


来源:https://stackoverflow.com/questions/16375857/type-aliasing-to-avoid-name-clash-in-type-refinement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!