19

With Dart 2.6, I can use extension methods like this :

extension on int {
  int giveMeFive() {
    return 5;
  }
}


main(List<String> arguments) async {
  int x;
  x.giveMeFive();
}

Everything works perfectly ! :D

But now if I want to put my extension method in another file like this :

main.dart

import 'package:untitled5/Classes/ExtendInt.dart';

main(List<String> arguments) async {
  int x;
  x.giveMeFive();
}

ExtendInt.dart

extension on int {
  int giveMeFive() {return 5;}
}

It fails with a depressing error ..

bin/main.dart:9:5: Error: The method 'giveMeFive' isn't defined for the class 'int'.
Try correcting the name to the name of an existing method, or defining a method named 'giveMeFive'.
  x.giveMeFive();
    ^^^^^^^^^^

Is it not allowed to do that or am I doing something wrong ? Thanks for reading ! :)

Johnny
  • 245
  • 5
  • 8

1 Answers1

31

This is working as intended. An extension with no name is implicitly given a fresh name, but it is private. So if you want to use an extension outside the library where it is declared you need to give it a name.

This is also helpful for users, because they may need to use its name in order to be able to resolve conflicts and explicitly ask for the extension method giveMeFive from your extension when there are multiple extensions offering a giveMeFive on the given receiver type.

So you need to do something like

extension MyExtension on int {
  int giveMeFive() => 5;
}
Erik Ernst
  • 804
  • 7
  • 3
  • Hello @Erik, thanks for the answer - and also for the great job on improving the dart language. What do you think about declaring extensions globally - without the need to import the extension? I think dart doesn't support anything that isn't explicitly declared with "import", so I think this would be a really big awkward overall step for the language. Still, I was wondering, since I came from years of Swift background and was wondering about the implications of the usage of extensions. – Guilherme Matuella Jul 29 '20 at 11:46
  • Hi @GuilhermeMatuella, thanks! It would not fit well to introduce a notion of a global declaration. I believe it couldn't mean that Dart tools would implicitly add imports of libraries that aren't already "in the same program": that concept isn't well defined. For instance, the analyzer must work on a library without any notion of what "the program" is. So it would basically mean that certain declarations (extensions) are implicitly re-exported, such that they are available whenever they can be reached by a direct or indirect import. But even that would probably cause too many name clashes. – Erik Ernst Aug 03 '20 at 09:30
  • Thanks for your opinion on the subject. Recently, I got a closer view of the "extensions" concept (both in Swift and the one in Dart), and it indeed doesn't make much sense when we simply look at this "global concept" spectrum - also, we must assume that the wider goals of a language, like Dart, does come into play in these cases as well. – Guilherme Matuella Aug 03 '20 at 11:08