Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the patch keyword in Dart do?

Tags:

dart

Can someone explain what the patch keyword does? For example, in math_patch.dart I see

patch num pow(num x, num exponent) => MathNatives.pow(x, exponent);
patch double atan2(num a, num b) => MathNatives.atan2(a, b);
patch double sin(num x) => MathNatives.sin(x);
patch double cos(num x) => MathNatives.cos(x);

What does this mean? What are _patch.dart files for?

like image 743
Seth Ladd Avatar asked Oct 12 '12 06:10

Seth Ladd


1 Answers

The patch mechanism is used internally (and is only available internally, not to end users) to provide different implementations of core library functionality.

For the math library that you have below, the platform independent library source in lib/math declares these methods as external. external methods get their implementation from a patch file. There is a patch file in the VM in runtime/lib/math_patch.dart, which supplies the implementation for the VM and there is a patch file in the dart2js compiler in lib/compiler/implementation/lib/math_patch.dart, which supplies the dart2js implementation.

The external keyword is understood by the analyzer and doing it this way allows only the shared part to be in the SDK and be understood by the tools. That means that the SDK can have lib/math instead of having lib/math/runtime and lib/math/dart2js, which makes the SDK cleaner and easier to understand.

like image 88
Seth Ladd Avatar answered Sep 19 '22 23:09

Seth Ladd