[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: cheerful static typing (was: Any and Every... (was: Eval))
Here's how we provide a choice of optimistic or pessimistic type
checking in Curl:
For a call to any operator (including the assignment and return
operators, and the usual function calls), the compiler checks that the
representation of each value matches that which is expected:
1. Various compiler directives may be used to reject the usage at
compile time. If a given directive is active, static type declarations
(e.g., signatures) are used to determine if a corresponding "information
losing" behavior or error during execution might occur, and the usage is
rejected. The default settings prohibit an enumerated list of
"narrowing" conversions. Compiler directives can be specified for a
lexical-scope or at top-level for a whole package of code.
2. If not rejected, the value is coerced to the correct static type in
exactly the same way as an explicit use of the casting operator.
3. Any coercion (explicit or implicit) will merely confirm the type of a
value if possible (e.g., changing if necessary, only the static type as
understood by the compiler, but nothing else). Otherwise, it arranges
to actually cause a new value with a different representation to be
constructed if necessary (a cross-cast).
A single compiler directive (safe?) can be used to have the compiler
pessimistically reject anything that might cause a runtime type error.
This can be broken down into just certain cases such as
allow-implicit-downcasts?, allow-implicit-any-casts?, etc. When all of
these are allowed, then all possible type checks are cheerfully deferred
to runtime. (Of course, even then, if the two types are disjoint and
have no cross-cast operations defined between them, we still reject the
usage at compile time.)