Overriding fields in strong mode

53 views
Skip to first unread message

Samuel Rawlins

unread,
Mar 10, 2017, 7:04:52 PM3/10/17
to General Dart Discussion
Hey Dart strong mode experts,

I am wondering why we cannot override fields in strong mode (without changing type). Take this example:

class A {
  int a;
}

class B extends A {
  @deprecated
  int a;
}


yields the error: "Field declaration A.a cannot be overridden in B."

I think there are probably many reasons to want to annotate a field from your super class (mark it @deprecated, or to observe it, or declare it non-null, etc), and the only way to annotate it is to re-declare it.

A) Why can we not re-declare a field, if we don't change it's type or access?
B) How else might you annotate a field in your super class?

Frank Pepermans

unread,
Mar 10, 2017, 7:16:43 PM3/10/17
to mi...@dartlang.org

Never used the annotation myself, but did you try @virtual? https://github.com/dart-lang/sdk/issues/27384


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Samuel Rawlins

unread,
Mar 10, 2017, 7:44:55 PM3/10/17
to General Dart Discussion
That is almost what I'm looking for, Frank. Thanks for the tip.

However, I'm looking at the issue where you don't control/own the super class. In particular if you are extending dart:html's Element, or something really distant from your code, in a separate package.

Vijay Menon

unread,
Mar 10, 2017, 8:00:51 PM3/10/17
to General Dart Discussion
FYI - this is slated to be supported again in 1.23 (overriding without virtual):


That said, in your example below, instances of type B will end up with two field slots for 'a', which might not be what you want.
Reply all
Reply to author
Forward
0 new messages