Conversion from loosely typed values to a strictly typed instances.
Example
Generic wrapper class
Consider the case of implementing a generic wrapper class. Although the
wrapper encapsulates a particular concrete type, the API for the wrapper
should accept similarly typed values--but not any value. Two generic types
are in play here, the strict type being wrapped, and a loose type that's
accepted as arguments and internally converted to the strict type. These
types are not independent of each other; each strict type has an associated
loose type from which it can be converted. To minimize boilerplate, the
wrapper class should only need to be parameterized with a single type.
These requirements can be cleanly met with a combination of [[Like]] types
and FromLike conversions. The generic wrapper class described above can
be implemented as follows:
Conversion from loosely typed values to a strictly typed instances.
Example
Generic wrapper class
Consider the case of implementing a generic wrapper class. Although the wrapper encapsulates a particular concrete type, the API for the wrapper should accept similarly typed values--but not any value. Two generic types are in play here, the strict type being wrapped, and a loose type that's accepted as arguments and internally converted to the strict type. These types are not independent of each other; each strict type has an associated loose type from which it can be converted. To minimize boilerplate, the wrapper class should only need to be parameterized with a single type.
These requirements can be cleanly met with a combination of [[Like]] types and
FromLike
conversions. The generic wrapper class described above can be implemented as follows:When no loose type conversions are needed,
GenericWrapper
can be instantiated with an identityFromLike
converter.A
GenericWrapper
parameterized with aLike
type and accompanyingFromLike
conversion can be set with any like-typed value.