Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of how to link external function to its implementation

Tags:

dart

I notice that Dart supports the external keyword. Can someone point me to an example that demonstrates how to link an external function to its implementation?

like image 369
integragreg Avatar asked Oct 23 '25 12:10

integragreg


1 Answers

The external keyword is currently only used by the platform libraries. It allows dart2js and the Dart VM to use the same libraries, with only certain functions specialized for the platform. It is not something you can use for your own libraries, it's only used internally.

The external keyword was added to the language so that the platform libraries could do this, and still be valid Dart code.

The way it's used in the platform libraries is that a method can be marked as external. Then it doesn't have a body (like an abstract method), but the compiler or runtime must the provide an implementation. Example of an external method:

class Foo {
  external int bar(int x);
}

Both dartjs and the VM would then have their own individual patch files supplying the actual implementation. Example (dart2js format):

@patch class Foo {
   @patch int bar(int x) => somethingMagical(x); 
}

The magical part is free to use platform specific features (like pure JS code or native VM functions).

like image 166
lrn Avatar answered Oct 25 '25 12:10

lrn