Re:Idris: A Language for Type-Driven Development
by still_grokking on Tuesday January 24, 2023 @03:05AM
(#34500836)
"Funny" code.
I'm quite sure nobody would do something like that "in production"…
The problem starts with the signature of this function: There is—of course—no function which could take an arbitrary String and return a SubscriberName.
You need to return at least an Option, or better a Result.
But at this point the fun starts. You need to thread this wrapped value throughout your whole program.
Crashing your program (by a panic in the above function, or by a unwrap somewhere later) because someone sent you an invalid String is not an option in real life.
Re:Idris: A Language for Type-Driven Development
by satvikpendem on Tuesday January 24, 2023 @03:11AM
(#34500877)
That's a simplified example that I grabbed in the middle of the book before the author refactors it, of course you shouldn't `panic` if you don't need to.
Re:Idris: A Language for Type-Driven Development
by still_grokking on Tuesday January 24, 2023 @03:17AM
(#34500918)
I've pointed this out as I think the follow-up problem is more difficult actually.
It's easy to validate something "and just throw an exception".
The fun starts when you can't do that (or don't want to do it like that).
This example comes from the excellent book Zero To Production In Rust, which has a chapter on type-driven development [0].
[0] https://zero2prod.com