A quick summary of how the author sees type coercion / casting / conversion in programming languages, followed by a somewhat novel proposal.
Type casting is a special form of functional programming, where we rewrite a function B(source:A, options...)
as a cast operation A -> B<options...>
, where B
is now a type template, and options...
is an optional list of auxiliary compile time constants that become attributes of the type template.
Type casting is effectively data conversion. You have source data in format A that you would like to transform to target data in format B. And as in actual data conversion, we have two levels of quality:
- Lossless conversion, which transforms the data without destroying information, which means the conversion is invertible, the mapping is bijective and time-symmetrical. Some examples of a lossless conversion are "big endian to little endian", "upcast type to tagged union", "sign-extend integer from 32-bit to 64-bit",