4 ms·
I'm not aware of a similar feature built into other languages, but most of it could be easily achieved with almost any type system. Just have two separate type
by realharo 7y ago
I'm not aware of a similar feature built into other languages, but most of it could be easily achieved with almost any type system.
Just have two separate types, e.g. UnsafeString and regular String, and some kind of `convert` function that takes a validation function as an argument. You'd get compile-time checking that way.
People don't tend to use such things in practice though, and you would also have to ban a portion of most language's standard libraries to enforce it in practice (because they already return regular strings for inputs).
- 13of40 7y agoHow about implementing it as an access modifier like "trusted" and enforce that only values from other trusted members can be assigned to a trusted member?
- staticassertion 7y agoI think this is just refinement types. You wouldn't so much ban the stdlib as need to wrap it.
- deleted 7y ago[deleted]
- aidos 7y agoThis can be a useful pattern - though I’ve never seen it used for this usecase. A similar one is templating languages (like Jinja) where you need to wrap strings if you want to send HTML to your templates with them being escaped on render. We use something similar where we have a BadNumber class in our code (python). Any operation with another number will also create a BadNumber. It allows us to make sure that these tainted numbers are always obvious.
- heavenlyblue 7y agoYou could use an import hook in python, then create a whitelist of APIs that will be mapped to UnsafeString and then will receive SafeString as an argument.