Skip to content

Infer refinement types for val members of traits and abstract classes #3964

Open
@liufengyun

Description

@liufengyun

Currently abstract member of trait or class will get a refinement type:

trait Animal
class Dog extends Animal
class Cat extends Animal

abstract class Bar { val x: Animal }
val bar: Bar { val x: Cat } = new Bar { val x = new Cat }

trait Foo { val x: Animal }
val foo: Foo { val x: Cat } = new Foo { val x = new Cat }

As argued by @julienrf , there should be a symmetry if the members are in the constructor positions:

abstract class Bar(val x: Animal)
val bar: Bar { val x: Cat } = new Bar(new Cat)

trait Foo(val x: Animal)
val foo: Foo { val x: Cat } = new Foo(new Cat)

Hopefully, this feature should also make the following work:

trait Vec(val size: Int)
class Vec8 extends Vec(8)

abstract class Lst(val size: Int)
class Lst8 extends Lst(8)

class Test {
  val v8a: Vec { val size: 8 } = new Vec8        // ok
  val v8b: Vec { val size: 8 } = new Vec(8)      // ok

  val l8a: Lst { val size: 8 } = new Lst8        // ok
  val l8b: Lst { val size: 8 } = new Lst(8)      // ok
}

Related #3920 .

Metadata

Metadata

Assignees

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