To me, invalid values are best expressed with optional error returns along with the value that are part of the function signature. Types are best used to only encode information about the hierarchy of structures composed of primitive types. They help define and navigate the representation of composite things as opposed to just having dynamic nested maps of arbitrary strings.
Parse, Don't Validate and Type-Driven Design in Rust
61–70 of 89 posts
Re: Parse, Don't Validate and Type-Driven Design in Rust
#62The `try_roots` example here is actually a _counterexample_ to the author's main argument. They explicitly ignore the "negative discriminant" case. What happens if we consider it? If we take their "parse" approach, then the types of the arguments a, b, and c have to somehow encode the constraint `b^2 - 4 a c >= 0`. This would be a total mess--I can't think of any clean way to do this in Rust. It makes _much_ more sen…
Sure, we can follow the advice of creating types that represent only valid states but then we end up with `fn(a: A, b: B, c: C) transformed into `fn(abc: ValidABC)`
Re: Parse, Don't Validate and Type-Driven Design in Rust
#63Every time you introduce a type for a "value invariant" you lose compatibility and force others to make cumbersome type conversions. To me, invalid values are best expressed with optional error returns along with the value that are part of the function signature. Types are best used to only encode information about the hierarchy of structures composed of primitive types. They help define and navigate the representati…
What would you say to someone who thinks that nested maps of arbitrary strings have maximum compatibility, and using types forces others to make cumbersome type conversions?
Re: Parse, Don't Validate and Type-Driven Design in Rust
#64Re: Parse, Don't Validate and Type-Driven Design in Rust
#65[flagged]
Re: Parse, Don't Validate and Type-Driven Design in Rust
#66Every time you introduce a type for a "value invariant" you lose compatibility and force others to make cumbersome type conversions. To me, invalid values are best expressed with optional error returns along with the value that are part of the function signature. Types are best used to only encode information about the hierarchy of structures composed of primitive types. They help define and navigate the representati…
> They help define and navigate the representation of composite things as opposed to just having dynamic nested maps of arbitrary strings. What would you say to someone who thinks that nested maps of arbitrary strings have maximum compatibility, and using types forces others to make cumbersome type conversions?
edit: To put it differently: To possibly be compatible with the nested "Circle" map, you need to know it is supposed to have a "Radius" key that is supposed to be a float. Type definitions just make this explicit. But just because your "Radius" can't be 0, you shouldn't make it incompatible with everything else operating on floats in general.
Re: Parse, Don't Validate and Type-Driven Design in Rust
#67Earlier quoted context omitted.
Coming from Haskell, I loved Agda 2 as a dependent type language. Is there any newer or more mainstream language that has added dependent types?
Typescript has something that can be used as dependent types, but it wasn't intended as a language feature, so the Syntax is not as ergonomic as Agda: https://www.hacklewayne.com/dependent-types-in-typescript-se...
Re: Parse, Don't Validate and Type-Driven Design in Rust
#68[dead]
Re: Parse, Don't Validate and Type-Driven Design in Rust
#69Re: Parse, Don't Validate and Type-Driven Design in Rust
#70Note that the division-by-zero example used in this article is not the best example to demonstrate "Parse, Don't Validate," because it relies on encapsulation. The principle of "Parse, Don't Validate" is best embodied by functions that transform untrusted data into some data type which is correct by construction . Alexis King, the author of the original "Parse, Don't Validate" article, also published a follow-up, "Na…