scala - 类型别名以避免类型细化中的名称冲突

标签 scala types namespaces alias

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

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

但我真的不想更改类型名称。

最佳答案

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

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] = ???

关于scala - 类型别名以避免类型细化中的名称冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16375857/

相关文章:

Scala 使用模式匹配获取列表的第一个和最后一个元素

Java:将属性声明为 Java 给定类型或自编写类型(也许带有接口(interface)?)

reactjs - 如何在带有 typescript 的函数中接受 React.Component 参数?

使用 Maven 的 Spring 自定义命名空间

r - 在不附加包的情况下评估包环境中的功能

scala - 在 Windows 10 spark.hive.hiveSessionState 上安装 spark

jquery - Play 2.0 自动完成反向路由不起作用

scala - 根据条件拆分 Spark DataFrame

types - Polars 中是否有相当于 pandas dtypes 命令的命令?

c++ - 代码在 CLion 中编译,但不在命令提示符中