2011-11-14 42 views

回答

6

定义作为方法上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


不知道你想达到什么,但。

4

简而言之,因为它不是。它是一个object和一个class,但不是一个方法,只有方法可以是运算符(中缀或不)。

作为一个对象,你可以使用它在模式匹配:

case a ∙ b => 

为一类,如果它有两个类型参数,它会用它的类型声明:

type X = Int ∙ String 
3

这不是由于使用了unicode符号。没有我意识到的中缀构造函数语法。所以,如果你想创建一个中缀语法,你可以做什么埃米尔建议(添加方法TermI)的对象,或使用隐式转换:

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.::