News for hackers. Stuff that matters.
Re:Parse, Don't Validate (2019) by munk-a on Tuesday February 10, 2026 @11:38AM (#46962439)
Without any other context? Nothing - it's just a type alias...

But the context this type of an alias should exist in is one where a string isn't turned into a PhoneNumber until you've validated it. All the functions taking a string that might end up being a PhoneNumber need to be highly defensive - but all the functions taking a PhoneNumber can lean on the assumptions that go into that type.

It's nice to have tight control over the string -> PhoneNumber parsing that guarantees all those assumptions are checked. Ideally that'd be done through domain based type restrictions, but it might just be code - either way, if you're diligent, you can stop being defensive in downstream functions.

Re:Parse, Don't Validate (2019) by thfuran on Tuesday February 10, 2026 @12:04PM (#46962988)
>But the context this type of an alias should exist in is one where a string isn't turned into a PhoneNumber until you've validated it.

Even if you don't do any validation as part of the construction (and yeah, having a separate type for validated vs unvalidated is extremely helpful), universally using type aliases like that pretty much entirely prevents the class of bugs from accidentally passing a string/int typed value into a variable of the wrong stringy/inty type, e.g. mixing up different categories of id or name or whatever.

Re:Parse, Don't Validate (2019) by seanwilson on Tuesday February 10, 2026 @11:48AM (#46962664)
> All the functions taking a string that might end up being a PhoneNumber need to be highly defensive

Yeah, I can't relate at all with not using a type for this after having to write gross defensive code a couple of times e.g. if it's not a phone number, return -1...throw an exception? The typed approach is shorter, cleaner, self-documenting, reduces bugs and makes refactoring easier.

Re:Parse, Don't Validate (2019) by barmic12 on Tuesday February 10, 2026 @12:17PM (#46963236)
one issue is it’s not a type alias but a type encapsulation. This have a cost at runtime, it’s not like in some functionnals languages a non cost abstraction.
Re:Parse, Don't Validate (2019) by vips7L on Tuesday February 10, 2026 @01:24PM (#46964422)
Correctness is more important than runtime costs.
Re:Parse, Don't Validate (2019) by LelouBil on Wednesday February 11, 2026 @12:29PM (#46977932)
In languages like kotlin and rust you can have a type encapsulation like this that does not exist at runtime