r/ProgrammingLanguages • u/Low_Lawyer_5684 • 6h ago
A question about handling bad typecasts in dynamically-typed language
Hello everyone,
Im developing a small hobby scripting language, for embedded applications. It was started as a PHP8 clone, but now it is not fully compatible with Zend PHP.
I would like to get a feedback on language feature Im experimenting with.
It is about type conversions (type casts)
The basic idea is to allow any conversions, but track conversions that may indicate a programming mistake (allowing a bad input to reach calculations is considered a programmer mistake).
I.e.:
Instead of immideately throwing an error or silently accepting the conversion, we can mark the resulting
value as unreliable.
This metadata (a flag) belongs to the value itself (not to the variable). Copying a value preserves the flag, and operations consuming an unreliable value propagate it to their results. 'Consuming' means using the value.
Example of the unreliable flag propagation:
Values can be tested with built-in language construct is_reliable(...);
There are pseudo-types unreliable and reliable which allow this flag manipulation:
Normally program continues executing regardless unreliability of values used.
However, you can explicitely require reliable values in a particular code section:
Reading an UNRELIABLE value inside a reliable block throws an exception.
The idea is to combine the idea of dynamic typing with the ability to enforce stricter guarantees where needed.
I'm aware that propagating unreliability through subsequent calculations is not the best way to detect a programming error at the exact point where it originally occurred :).
By the time the value is consumed, the actual source of the problem may be far away, yes.
I'm interested in whether the benefits outweight this drawback.
A fast-fail mode is also allowed: we want exception but we do not want to catch it:
Examples above are all about strings and numbers, but this mechanism is suggested for all types (hashmaps aka php arrays, float, boolean values, class instances):
What do you think about this approach?
In particular:
- Dose propagating reliability flags through values and operations makes any sense?
- Are there any problems with this model?
- Have you seen similar approaches in other languages or runtimes?
Thank you!