Scala 中参数化类型的声明中是否允许使用中缀符号?

Is infix notation allowed in declarations of a parameterized type in Scala?

我一直在努力理解 this post 中所提问题的答案。

下面的代码再现了已接受答案中提出的解决方案。

object Finder {
  def find[T <: Node](name: String)(implicit e: T DefaultsTo Node): T = 
    doFind(name).asInstanceOf[T]
}    

sealed class DefaultsTo[A, B]
trait LowPriorityDefaultsTo {
  implicit def overrideDefault[A,B] = new DefaultsTo[A,B]
}
object DefaultsTo extends LowPriorityDefaultsTo {
   implicit def default[B] = new DefaultsTo[B, B]
}

如果隐式定义为

我会理解的
(implicit e:  DefaultsTo[T, Node])

但是它是用看起来像中缀表示法的东西定义的。我从来不知道类型 DefaultsTo[T, Node] 可以写成 T DefaultsTo Node。这是怎么回事?

But instead it is defined in something that looks like infix notation. I never knew that a type DefaultsTo[T,Node] could be written as "T DefaultsTo Node". Is this what is going on ?

是的。规则非常简单:类型必须恰好有 2 个参数。它主要用于具有 <:< 等符号名称的类型,但正如您所见,它也适用于字母数字名称。

Scala specification, 3.2.8 Infix Types:

InfixType     ::=  CompoundType {id [nl] CompoundType}

An infix type T1 op T2 consists of an infix operator op which gets applied to two type operands T1 and T2. The type is equivalent to the type application op[T1,T2]. The infix operator op may be an arbitrary identifier.

All type infix operators have the same precedence; parentheses have to be used for grouping. The associativity of a type operator is determined as for term operators: type operators ending in a colon ‘:’ are right-associative; all other operators are left-associative.

In a sequence of consecutive type infix operations t0 op t1 op2 … opn tn, all operators op1,…,opn must have the same associativity. If they are all left-associative, the sequence is interpreted as (…(t0 op1 t1) op2…) opn tn, otherwise it is interpreted as t0 op1 (t1 op2 (…opn tn)…).