Skip to content

Refine types according to their constructor val’s singleton types #1262

Closed
@julienrf

Description

@julienrf

Being able to have constructor parameters for traits is great, however using constructor parameters in the following situation is not very useful because the type Quux will not be refined according to the value’s singleton type of the foo constructor parameter:

trait Foo {
  type Bar
}

trait Quux(val foo: Foo)

And then:

object FooInt extends Foo {
  type Bar = Int
}

val quux: Quux { val foo: FooInt.type } = new Quux(FooInt)
// error: type mismatch:
//  found   : Quux
//  required: Quux{foo: FooInt.type}

It works if I replace the constructor’s val parameter foo with an abstract member and refine its type in the instantiated anonymous class:

trait Quux2 {
  val foo: Foo
}

val quux2: Quux2 { val foo: FooInt.type } =
  new Quux2 { val foo: FooInt.type = FooInt } // OK

A workaround proposed in SI-5700 consists of the following:

val quux: Quux { val foo: FooInt.type } = {
  class RefinedQuux(override val foo: FooInt.type) extends Quux(foo)
  new RefinedQuux(FooInt)
}

But I find it a bit cumbersome to use, and anyway this workaround is limited because we can not anymore mix a refined Quux in some other class or trait (ie. we can not write new Something with newQuux(…)).

Do you think it would be possible to refine traits type that have val constructor parameters according to the singleton type of the actual value being passed during the constructor invocation? That would make dependently-typed programming and existential types a lot more convenient.

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