English 中文(简体)
Scala 案例类别对内部类型中的错误进行汇编?
原标题:Scala Case class matching compile error with aliased inner types?

我如何使用与别类相匹配的个案类别? 当我把CB等带出集装箱时,就进行这项工作。

class DoStuff[TKey](
  val c : Container[TKey]#CB
)
{
  type CB = Container[TKey]#CB
  type C1 = Container[TKey]#C1
  type C2 = Container[TKey]#C2

  c match {
    case C1(e1) => e1  //   - not found: value e1   - not found: value C1
    case C2(e2) => e2 //    - not found: value e2   - not found: value C2
  }
}

trait Container[TKey]
{
    abstract trait CB
    case class C1(val e : AnyRef) extends CB
    case class C2(val e : AnyRef) extends CB
}

感谢!

最佳回答

权利 在Schala的班级比较粗略。 在我向各位展示你所提供法典的改写版本之前,请举一个简单的例子。

case class Foo(x: Int) {
  case class Bar(y: String) 
}

现在审议以下法典:

val x = new Foo(1)
val y = new Foo(2)

val a = new x.Bar("one")
val b = new y.Bar("two") 

www.un.org/chinese/sc/presidency.asp Foo。 但我们可以更具体地说,<代码>a的类型是x。 Bar b的类型:y。 Bar - which means that a is an instance of the inner category Bar with the exbject x, similar for b.

您可以实际看到,这些类型不同,其方式是:typeOf(a)typeOf(b),其中eOf是此种定义的一种通用方法。 (仅给出其论点的类型,其依据为相当的冰类推论,并给出使用Manifests>的参照标准。)

def typeOf[T](x: T)(implicit m: scala.reflect.Manifest[T]) = m.toString

作为内物体,请注明其外在物体。 因此,你可以打电话<代码>new x.Bar(“one”),但不能打电话到new Foo#Bar(......)?”——就如第二种情况,你没有指明你试图建造的新物体的内心物体是什么。

So, let s return to your code snippet. When you are pattern matching, you are actually calling a constructor - when calling C1(e1). As C1 is an alias for Container[TKey]#C1 you have tried to call a constructor of an inner class without specifying its outer object, which fails due to the reasons outlined above. The way I would write the code would be as follows:

trait Container[TKey] {
    abstract trait CB
    case class C1(val e : AnyRef) extends CB
    case class C2(val e : AnyRef) extends CB
}

class DoStuff[TKey] (val c: Container[TKey], val element: Container[TKey]#CB) {
  element match {
    case c.C1(e1) => Some(e1)
    case c.C2(e2) => Some(e2)
    case _        => None
  }
}

现在,这汇编了你们想要的东西,希望你们能够这样做。 但非常谨慎地这样做! 由于类型学,Schala不能保证element实际上是c.CB/code>或d。 CB,其中CB in the case of c and d are the same.

考虑这个例子:

def matcher(arg: Foo#Bar) = {
  arg match {
    case x.Bar(n) => println("x");
    case y.Bar(n) => println("y");
  }
}

<代码>xy与前。 运行情况如下:

matcher(a)
matcher(b) 

页: 1

因此,我将重写该法典,以明确在集装箱内有一个元素:

trait Container[TKey] {
    abstract trait CB
    case class C1(val e : AnyRef) extends CB
    case class C2(val e : AnyRef) extends CB
    val element: CB
}

class DoStuff[TKey](val c: Container[TKey]) {
  c.element match {
    case c.C1(e1) => Some(e1)
    case c.C2(e2) => Some(e2)
    case _        => None
  }
}

希望:

----------

问题回答

暂无回答




相关问题
Having many stacks with different types

I m making a C program that needs to use two stacks. One needs to hold chars, the other needs to hold doubles. I have two structs, node and stack: struct node { double value; struct node *...

Creating (boxed) primitive instance when the class is known

I need a method that returns an instance of the supplied class type. Let s assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

How to generate an instance of an unknown type at runtime?

i ve got the following in C#: string typename = "System.Int32"; string value = "4"; theses two strings should be taken to generate an object of the specified type with the specified value... result ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

热门标签