A class may be declared with more than one as implicit and they will be searched in their declaration order for the first one which can be applied to the argument.
There are a few restrictions but they are not too subtle and they are detailed in the docs.
The important reminder from the docs might be this:
The choice of which implicit constructor or factory to call depends on the runtime type of the value [of the argument passed] (my emphasis and parenthetical addition)An interesting example can be found by looking into the Curl constants named curl-version and curl-version-number. The latter is an instance of a VersionNumber.
The implicit factory for VersionNumber is documented as
public implicit
{VersionNumber.from-string
string:StringInterface
}:VersionNumber
The Curl docs provide a live-code working example. Here is my code from my simplified version of the example:
{define-proc {make-version-table ...:VersionNumber}:Table
and here is how to execute the procedure:
{make-version-table "7.0.1", "7.0.3+"}
Looking at the last the two REST arguments you can see how smart this is.
The code in the Curl example uses a loop:
{for version-num key i in ... do
}
and in the loop that local var version-num is sent a VersionNumber clone method named
n-clone
as in
{ version.n-clone n = j } || j is a loop counter in my simplified version
to clone selected portions of the version number object.
All we have are strings but the code also includes no asa operator doing any type casts.
NOTE: (a quote from the Curl docs)
You can also use asa to convert a value to a class type, providing there is a corresponding implicit constructor or factory in the class.
No comments:
Post a Comment