4 ms·
Typing validation can only check the type, but there are a lot of facets of the type that most languages don't use. Types are a way of encoding your assumption
by curryst 6y ago
Typing validation can only check the type, but there are a lot of facets of the type that most languages don't use. Types are a way of encoding your assumptions about the data in a way that the language can understand.
Types can be defined so that only a subset of values is acceptable. I could do something like:
UserID = int where value is less than 1000
my_user_id: UserID = query_database(username=my_username)
And the type system could throw an error/warning that says "my_user_id could be greater than 1000" because I never check whether that value is actually valid for the type. Or if the language and the type system are integrated, it could transparently do the assertion for you and throw an error if the data doesn't match its type.
You could do likewise with regexes for strings, lengths of integers, etc, etc. You can also encode complex relationships among objects. If you change how user IDs are formatted and don't migrate legacy users, you could express something like "if creation_date is older than 1/1/2000, user_id is an integer less than 30,000. Else user_id is a string that matches this regex".