method Bridge
1 | role Real |
1.1 | (Real) method Bridge |
Documentation for method Bridge
assembled from the following types:
role Real
From Real
(Real) method Bridge
Defined as:
method Bridge(Real:)
Default implementation coerces the invocant to Num and that's the behavior of this method in core Real types. This method primarily exist to make it easy to implement custom Real types by users, with the Bridge
method returning one of the core Real
types (NOT necessarily a Num) that best represent the custom Real
type. In turn, this lets all the core operators and methods obtain a usable value they can work with.
As an example, we can implement a custom Temperature
type. It has a unit of measure and the value, which are given during instantiation. We can implement custom operators or conversion methods that work with this type. When it comes to regular mathematical operators, however, we can simply use the .Bridge
method to convert the Temperature
to Kelvin expressed in one of the core numeric types:
is Realsub postfix:<℃>sub postfix:<℉>sub postfix:<K>my := 36.6℃;my := 451℉;my := 5778K;say ; # OUTPUT: «36.6 degrees C»say + + ; # OUTPUT: «6593.677777777778»say 123K + 456K; # OUTPUT: «579»
As we can see from the last two lines of the output, the type of the bridged result is not forced to be any particular core type. It is a Rat, when we instantiated Temperature
with a Rat
or when conversion was involved, and it is an Int when we instantiated Temperature
with an Int.