在Scala中将unicode符号用作中缀运算符

在Scala中将unicode符号用作中缀运算符,scala,operators,infix-notation,Scala,Operators,Infix Notation,有人知道为什么下面的代码无法识别吗∙ 作为有效的中缀运算符 object Main extends App { val c = (I() ∙ I()) } sealed abstract class Term case class I() extends Term case class ∙(x: Term, y: Term) extends Term 定义∙作为I上的方法 sealed abstract class Term case class II(x: Term, y: Ter

有人知道为什么下面的代码无法识别吗∙ 作为有效的中缀运算符

object Main extends App {
  val c = (I() ∙ I())
}

sealed abstract class Term 
case class I() extends Term
case class ∙(x: Term, y: Term) extends Term

定义
作为
I
上的方法

sealed abstract class Term 
case class II(x: Term, y: Term) extends Term
case class I() extends Term {
    def ∙(o: Term) = II(this, o)
}
现在
I()∙ I()
将工作,返回
II



不过,我不确定你想要实现什么。

简单地说,因为事实并非如此。它是
对象
,但不是方法,只有方法可以是运算符(中缀或非中缀)

作为对象,您可以在模式匹配上使用它:

case a ∙ b =>
作为一个类,如果它有两个类型参数,它将在类型声明中使用它:

type X = Int ∙ String

这不是因为使用了unicode符号。我知道没有中缀构造函数语法。因此,如果你想用中缀语法创建一个对象,你可以按照Emil的建议(添加
方法转换为
术语
I
)或使用隐式转换:

sealed abstract class Term 
case class I() extends Term 
case class ∙(x: Term, y: Term) extends Term

class Ctor_∙(x: Term) {
  def ∙(y: Term): ∙ = new ∙(x, y)
}  

object Term {
  implicit def to_∙(x: Term): Ctor_∙ = new Ctor_∙(x)
}
  • 隐式版本更像是创建元组的
    Predef.any2ArrowAssoc
    1->2
  • 添加方法更像是
    List.: