English 中文(简体)
混编的类型成员和类型参数时的假设
原标题:Scala type inference when mixing type member and type parameter

我有以下定义:

trait Xode[+R] {
  type S = Option[R]
}
case class AXode(a: Int) extends Xode[Int]
case class BXode(b: String) extends Xode[String]

object xtore {
  private var m: Map[Xode[_], Xode[_]#S] = Map()

  def put[A <: Xode[_]](k: A)(v: A#S) {
    m += k -> v    
  }  
}

在执行以下措施时,没有发现任何错误,尽管我预期安东和安特区是备选办法。

xtore.put(AXode(5))(Some("apples")) // <-- no error

可能发生的情况是<代码>。 缩略语 Xode[_], 然后Xode[_]#S 。 备选案文[_]。 我能否用类型参数表达我的意向?

如果R是抽象的一类成员,而不是一个类型参数,则按预期操作。 或依附方法类型和打字:k.S 是否有任何其他建筑?

最佳回答

Some (maybe slightly wrong explanation)

<代码>X[_]系指X[Any]。 尤其是,贵方的地图没有将野生原构造中的关键类型与<代码>Xode的类型构造联系起来。 除put外,如果需要,Scalac可扩大AXode[Any]。 申斥: I m 不是100% 保证我写什么

Code that passes the question

以下人士拒绝接受<条码>xtore.put(AXode(5))(Some(“apples”),并接受<条码>xtore.put(AXode(5))(Some(0)。 请注意,您还需要掌握<代码>的类型参数。 页: 1 简称表 我并不完全理解,可能有一个更简单的解决办法。

  def put[A,B[X] <: Xode[X]](k: B[A])(v: B[A]#S) {
    m += k -> v    
  }
问题回答

暂无回答




相关问题
Haskell -- more type inference problems

I have the following expression: getCount :: (Num a) => a -> [a] getCount int = foldl processOneCount [0,0,0,0,0,0,0,0,0,0] (map (singleDigitCount) (map (digitToInt) (...

Is there an equivalent to the C# "var" keyword in C++/CLI?

In C#, I like the var keyword for situations like this: var myList = new List<MyType>(); Is there any equivalent in C++/CLI, or do I have to repeat the type name everytime just like this: List&...

Conditionally assign C# var: As elegant as it gets?

I understand that the C# keyword var implies the type at compile time and therefore requires the declaration and assignment in the same place, making the following construct illegal: var something; ...

What causes this Standard-ML type error?

I was trying to make a tail-recursive version of this very simple SML function: fun suffixes [] = [[]] | suffixes (x::xs) = (x::xs) :: suffixes xs; During the course of this, I was using type ...

热门标签