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

前端 未结 15 2227
温柔的废话
温柔的废话 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.

    0 讨论(0)
  • 2020-11-22 06:50

    You might take a look at MetaScala, which has something called OneOf. I get the impression that this doesn't work well with match statements but that you can simulate matching using higher-order functions. Take a look at this snippet, for instance, but note that the "simulated matching" part is commented out, maybe because it doesn't quite work yet.

    Now for some editorializing: I don't think there's anything egregious about defining Either3, Either4, etc. as you describe. This is essentially dual to the standard 22 tuple types built in to Scala. It'd certainly be nice if Scala had built-in disjunctive types, and perhaps some nice syntax for them like {x, y, z}.

    0 讨论(0)
  • 2020-11-22 06:52

    Dotty, a new experimental Scala compiler, supports union types (written A | B), so you can do exactly what you wanted:

    def foo(xs: (String | Int)*) = xs foreach {
       case _: String => println("str")
       case _: Int => println("int")
    }
    
    0 讨论(0)
提交回复
热议问题