3 ms·
This is very exciting for a single feature: explicit type extensions. It allows domain-specific types. So instead of a function like getCustomer(int customerId
by throwaway13337 2y ago
This is very exciting for a single feature: explicit type extensions.
It allows domain-specific types. So instead of a function like getCustomer(int customerId), you could write getCustomer(CustomerId customerId) which is an integer (no extra memory allocated - it's a struct wrapper).
This is similar to the feature I use in typescript with zod z.brand('customerId') which adds a compile-time only hidden explicit type.
The really cool thing about this is that we can then enforce a rule that says CustomerId can only be, for example, a positive integer. Or that a string matches a given regex. We're validating a type and passing it around without wrapping it. You can still treat it as a primitive but you cannot treat the primitive as it without validation.
In other words, it can be thought of as ada's more strict types. e.g. type customerId range 1 .. 1000
Orientating validation around the type and not the function means that you only validate once yet are completely confident about it everywhere. We can essentially replace a lot of unit tests with a better type system.
Validated types are almost as a big of leap as types at all. They are descriptive, prevent a large class of bugs, while staying intuitive - not magical.
- noneeeed 2y agoI've always been disappointed by how few strongly typed languages have copied Ada's range subtype system. It was one of the things that made the language so nice to work in, and helped make a number of approaches to formal methods much more tractable. Being able to write a function/procedure and know that the parameters could only be within a certain set of legal values by that point let you avoid a lot of defensive coding, without having to rely on developers remembering to check their values. If you got your values into domain specific subtypes as early as possible your code was so much easier to reason about. It made a lot of the static analysis easier as well.