Adamovskiy
Adamovskiy

Reputation: 1583

This generic type

We have some classes hierarchy, and need to implement in base class method which return type is this.runtimeType. In Java is done by using generic parameter class Base extends <This extends Base<This>>. In Dart it works fine too:

class A<This extends A<This>> {
  This copy() => //...
}
class B extends A<B> {}

A a = new A(); // ok
A ab = new B(); // ok
B b = new B(); // ok

but while you have no not abstract parent classes:

A a2 = new A().copy(); // Unsound implicit cast from A<dynamic> to A<A<dynamic>>

And of course, we can't specify generic parameter for A explicitly here - it is recursive. Is it a kind of bug, or is there another way to do it in Dart?

Upvotes: 2

Views: 306

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657148

I think this is just a missing feature in strong mode.

I only get the error with

analyzer:
  strong-mode:
    implicit-casts: false

and this was added not too long ago.

In DartPad there is no error, even with Strong mode enabled.

Upvotes: 4

Related Questions