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

前端 未结 15 2397
温柔的废话
温柔的废话 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: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")
    }
    

提交回复
热议问题