我已经发现模式匹配中可能bind the type parameter
为什么那种情况不起作用?
trait T
case class S[A](a: A) extends T
def pr(t1: T, t2: T) = (t1, t2) match {
case (S(a): S[ta], S(b): S[tb]) => println(a); println(b)
}
^
error: '=>' expected but ':' found.
有关信息,这有效:
def pr(t1: T, t2: T) = (t1, t2) match {
case (s1: S[a], s2: S[b]) => println(s1.a); println(s2.a)
}
这也是:
def pr(t1: T, t2: T) = (t1, t2) match {
case (S(a), S(b)) => println(a); println(b)
}
我需要恢复类型以定义其类型无法推断的其他函数,因为在eta-expansion的上下文中。
更新
正如评论中提到的,我需要的类型只是为了正确的类型检查,而不是其他任何东西。
例如:
trait T
case class S[A](a: A, w: A => Int) extends T
def makeTwo(t1: T, t2: T) = (t1, t2) match {
case (S(a1, w1), S(a2, w2)) =>
val wNew = { (a, b) => w1(a) + w2(b) }
S((a, b), wNew)
}
error: missing parameter type
val wNew = { (a, b) => w1(a) + w2(b) }
^
答案 0 :(得分:1)
引用Scala语法规则:
varid ::= lower idrest
idrest ::= {letter | digit} [‘_’ op]
op ::= opchar {opchar}
opchar ::= “all other characters in \u0020-007F and Unicode
categories Sm, So except parentheses ([]) and periods”
// 'a' and 'a_-' is valid, 'a(' is not.
类型模式匹配规则说:
Pattern1 ::= varid ‘:’ TypePat
| ‘_’ ‘:’ TypePat
所以
def pr(list: Any) = list match {
case a :String => // works
case a_- :String => // works a_- is valid instance of 'varid'
case a() :String => // does not work.
case a(b) :List[Int] // does not work either!
}
因此:
case S(a): S[ta] is not valid syntax for pattern match.
但是以下是有效的
case (s1 :S[a], s2: S[b])
根据元组模式匹配规则:
SimplePattern ::= ‘(’ [Patterns] ‘)’
Patterns ::= Pattern {‘,’ Patterns}
除了您列出的两个可能选项外,您还可以使用:
case tup: (S[_], S[_]) =>
以下似乎有效:
trait T
case class S[A](a: A, w: A => Int) extends T
def makeTwo(t1: T, t2: T) = (t1, t2) match {
case (S(a1, w1), S(a2, w2)) =>
val wNew = { tup:(Any, Any) => w1(tup._1) + w2(tup._2) }
S((a1, a2), wNew)
}
def someString (s: String) = { s.length }
val twoS = makeTwo(S("Hello", someString), S("World!", someString))
println(twoS.w(twoS.a)) // gives 11
答案 1 :(得分:0)
至少在某些情况下,你可以得到你想要的东西。
如果您将类型ClassTag
的上下文绑定(隐式参数的语法糖)添加到您希望由编译器强制执行的类型参数,它将具有执行此操作所需的内容。
,例如,我有一个Akka管道的通用位,我用来缓冲生产者和消费者之间的消息,我希望它是类型安全的。
该类的定义如下:
class TimedBatchQueue[T : ClassTag](batchLimit: Int,
MaxQueueTime: FiniteDuration,
receiver: ActorRef)
extends Actor
接收功能包括缓冲信息的情况:
def receive = {
...
case mesg: T => /* Enqueue the message */
case mesg => /* Bounce the unacceptable message back to the sender encapsulated in a reject message */
}