Sdk: Add a hint `@nonVirtual` for class members

Created on 5 Sep 2018  路  9Comments  路  Source: dart-lang/sdk

... or, to _help_ developers when defining the intended use of a class, and to _help_ users use a class properly (i.e. as intended by the class). I'd find this extremely _useful_ when working on fairly complex classes with lots and lots of member fields and methods.

Similar to the spirit of @sealed (https://github.com/dart-lang/sdk/issues/27372):

import 'package:meta/meta.dart';

class X {
  @nonVirtual
  void alwaysPrintFoo() {
    print('Foo');
  }
}

class Y extends B {
  // HINT: X.alwaysPrintFoo is marked @nonVirtual and should not be overridden.
  void alwaysPrintFoo() {
    print('Bar');
  }
}

General concept:

  • Issue a _hint_ when a member annotated with @nonVirtual is overriden.

Because of how this annotation would need to work, I imagine it would _also_ need to:

  • Issue a _hint_ when a class with one or more @nonVirtual members is _implemented_ (implements - not extends/with), as by definition a class with @nonVirtual member requires the _original_ implementation:

    import 'package:meta/meta.dart';
    
    class X {
    @nonVirtual
    void alwaysPrintFoo() {
      print('Foo');
    }
    }
    
    // HINT: Cannot implement a class with one or more `@nonVirtual` members.
    abstract class Y implements X {}
    
  • Issue a _hint_ when an _abstract_ member is annotated with @nonVirtual. Also impossible:

    import 'package:meta/meta.dart';
    
    abstract class X {
    // HINT: Cannot define a `@nonVirtual` abstract member.
    @nonVirtual
    void alwaysPrintFoo();
    }
    

/cc @davidmorgan @srawlins for thoughts.

analyzer-hint area-analyzer customer-google3 type-enhancement

Most helpful comment

It's now October 2019, and this issue's been open for over a year. can someone on the Dart team let us know what they think about this?

All 9 comments

I do like it, would be definitely helpful in many APIs. Why not call it @final to align with Java?

I believe that is a reserved word/keyword in Dart. It could be @finalMember, but meh.

(I don't care about the name, to be clear, it can be whatever folks agree on)

An alternative is just to support @sealed on member methods/fields, of course!

/cc @srawlins

I keep getting into situations where I'd like to have this functionality.

For reference, an old school issue from @danrubel asking for language-level support of this is here: https://github.com/dart-lang/sdk/issues/3928. I'll update that issue to link here. I do think it's better (and more practical) to implement this via pkg:meta rather than having it in the language.

Also just ran into a situation where this would be useful. Any updates on the priority? I'm willing to contribute the change, just not sure where to begin.

It's now October 2019, and this issue's been open for over a year. can someone on the Dart team let us know what they think about this?

I'm all for revisiting this. Specifically, it could potentially be used to disallow overriding Widget equality in Flutter (which otherwise we'll have to analyze specifically for). See https://github.com/dart-lang/linter/issues/1783 and it's inspiration: https://github.com/flutter/flutter/pull/41220#discussion_r332737769.

Implemented in server and landed in package:meta:

image

Looking forward to seeing how folks take advantage!

Was this page helpful?
0 / 5 - 0 ratings