Skip to content

[F] syntactic sugar for dynamic dispatch involving union types #6090

Closed
@robstoll

Description

@robstoll

I am not using dotty yet and merely read the doc about union types. Might be this is already supported. The idea is that the compiler does the dynamic dispatch if a method is called on a union type. For instance:

trait A { def foo(){} }
trait B { def foo(){} }
object Test {
  def test(fooable: A | B) {
    fooable.foo()
  }
}

Would expand to:

object Test {
  def test(fooable: A | B) {
    fooable match {
      case a: A => a.foo()
      case b: B => b.foo()
    }
  }
}

This would also go well in combination with structural types:

def test(fooable: A | B | { def foo(): Unit }) {
  fooable.foo()
}

Would expand to something like:

object Test {
  def test(fooable: A | B | { def foo(): Unit }): Unit = {
    fooable match {
      case a: A                   => a.foo()
      case b: B                   => b.foo()
      case x => x.asInstanceOf[{ def foo(): Unit }].foo() 
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions