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?
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/mathdeclares these methods asexternal.externalmethods get their implementation from a patch file. Thereis a patch file in the VM in
runtime/lib/math_patch.dart, whichsupplies the implementation for the VM and there is a patch file in
the
dart2jscompiler inlib/compiler/implementation/lib/math_patch.dart, which supplies thedart2jsimplementation.The
externalkeyword is understood by the analyzer and doing it thisway allows only the shared part to be in the SDK and be understood by
the tools. That means that the SDK can have
lib/mathinstead of havinglib/math/runtimeandlib/math/dart2js, which makes the SDK cleaner andeasier to understand.