3 ms·
#1 You want to define a type with specific properties. #2 You don't want to write a validator. #3 Presumably you want to instantiate values of that type with
by hnfong 5y ago
#1 You want to define a type with specific properties.
#2 You don't want to write a validator.
#3 Presumably you want to instantiate values of that type with user given input.
I don't think the three requirements make sense at all.
If you're willing to drop #3 you can always use some sort of enum-ish thing.
- ragnese 5y agoI don't want to write a validator and litter calls to it throughout my code base. I want the "validator" to be the constructor of a type (or a refinement mechanism, etc). Then every function in my code base can have a clear contract via its type signature. What good is a function that says it accepts a string and returns a string if it actually just explodes when given some strings? That's not the same thing as returning a string... I want to do three-ish things: 1. Make my function's type signature be correct. It is NOT correct to say that you accept an "int" but actually crash if you're given `0`. 2. Push bug catching to compile time instead of runtime. Calling a validation function inside a function is a runtime check. In the meantime I can write 100 calls to a function that I know will cause a crash and the compiler will say nothing. 3. Push type requirements to the callers of functions. Everyone preaches "validate at the edges", but if your function signatures had very precise types, that would happen "automatically." Your code won't compile unless you pipe the correct types all the way from the top function call down to the bottom one.