How to define “type disjunction” (union types)?

前端 未结 15 2387
温柔的废话
温柔的废话 2020-11-22 05:52

One way that has been suggested to deal with double definitions of overloaded methods is to replace overloading with pattern matching:

object Bar {
   def fo         


        
15条回答
  •  自闭症患者
    2020-11-22 06:48

    From the docs, with the addition of sealed:

    sealed class Expr
    case class Var   (x: String)          extends Expr
    case class Apply (f: Expr, e: Expr)   extends Expr
    case class Lambda(x: String, e: Expr) extends Expr
    

    Regarding the sealed part:

    It is possible to define further case classes that extend type Expr in other parts of the program (...). This form of extensibility can be excluded by declaring the base class Expr sealed; in this case, all classes that directly extend Expr must be in the same source file as Expr.

提交回复
热议问题