简体   繁体   English

扩展功能缺少类型参数

[英]Missing type parameter for expanded function

I'm learning Scala and the following simple program got me stuck: 我正在学习Scala,下面的简单程序使我陷入了困境:

class ObjectPrinter[T <: AnyVal](x: T) {
  def print(t: T) = { // <--- error here
    case Is(i) => println("Integer: " + i)
    case Ds(d) => println("Double: " + d)
    case _ => println("Default")
  }

  case class Is(i : Int) extends ObjectPrinter[Int](i);
  case class Ds(d: Double) extends ObjectPrinter[Double](d);
}

The error message is the following: 错误消息如下:

Missing type parameter for expanded function. 扩展功能缺少类型参数。 The argument type of an anonymous function must be fully known. 匿名函数的参数类型必须是完全已知的。 Expected type was: ? 预期类型为:

The message is completely unclear to me. 该消息对我来说完全不清楚。 What do they mean, missing type parameter? 它们是什么意思,缺少类型参数? I thought the type parameter follows after the case , like Is(i) . 我认为以后的类型参数如下case ,如Is(i) What function is expanded? 扩展了什么功能?

UPD: I want to return a function depending on the type of the argument passed in as a parameter. UPD:我想返回一个函数,具体取决于作为参数传入的参数的类型。

{
  case Is(i) => println("Integer: " + i)
  case Ds(d) => println("Double: " + d)
  case _ => println("Default")
}

is short for 是短的

y => y match {
  case Is(i) => println("Integer: " + i)
  case Ds(d) => println("Double: " + d)
  case _ => println("Default")
}

(that's the expanded function the error is talking about. But the compiler has no way to tell what do you want y 's type to be. (这是错误所讨论的扩展函数。但是编译器无法告诉您您希望y的类型是什么。

If this is what you want, the simplest way to specify the type would be 如果这是您想要的,指定类型的最简单方法是

(_: SomeType) match {
  case Is(i) => println("Integer: " + i)
  case Ds(d) => println("Double: " + d)
  case _ => println("Default")
}

But this looks quite strange: you are using neither x nor t there. 但这看起来很奇怪:您在此处既不使用x也不使用t

I would implement it through typeclass 我将通过typeclass实现它

case class ObjectPrinter[T <: AnyVal](x: T)

object ObjectPrinter {
  implicit val printInt = new Print[Int] {
    override def print(t: ObjectPrinter[Int]): Unit = println("Integer: " + t.x)
  }

  implicit val printDouble = new Print[Double] {
    override def print(t: ObjectPrinter[Double]): Unit = println("Double: " + t.x)
  }

  def print[T <: AnyVal](t: ObjectPrinter[T])(implicit print: Print[T]) = {
    print.print(t)
  }
}

trait Print[T <: AnyVal] {
  def print(t: ObjectPrinter[T]): Unit
}

Alternatively, this one - compiles 或者,这个-编译

class ObjectPrinter[T <: AnyVal](x: T) {
  def print(t: T): Unit = t match { 
    case i: Int => println("Integer: " + i)
    case d: Double => println("Double: " + d)
    case _ => println("Default")
  }

  case class Is(i : Int) extends ObjectPrinter[Int](i);
  case class Ds(d: Double) extends ObjectPrinter[Double](d);
}

(though I do not see any usecase how you would use print, ie why do you enforce t and x be the same type) (尽管我看不到任何用例,如何使用打印,即为什么将tx强制为相同类型)

May be you meant to have such implementation: 可能是您打算执行以下操作:

class ObjectPrinter[T <: AnyVal](x: T) 

object ObjectPrinter {
  def print[T <: AnyVal](op: ObjectPrinter[T]): Unit = op match { // <--- error here
    case Is(i: Int) => println("Integer: " + i)
    case Ds(d: Double) => println("Double: " + d)
    case _ => println("Default")
  }

}

case class Is(i : Int) extends ObjectPrinter[Int](i)
case class Ds(d: Double) extends ObjectPrinter[Double](d)

object Test extends App {
  ObjectPrinter.print(Is(5))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM