News for hackers. Stuff that matters.
Re:Parse, Don't Validate (2019) by ollysb on Tuesday March 07, 2023 @07:28AM (#35054562)
You can use opaque types to encode constraints that the type system isn't able to express. That way you can have factory functions that apply any logic that's required before allowing construction of the opaque type. Now whenever that opaque type is referred to there's a guarantee that the data it contains satisfies your desired constraint.
Re:Parse, Don't Validate (2019) by crabbone on Tuesday March 07, 2023 @10:47AM (#35056632)
> You can use opaque types to encode constraints that the type system isn't able to express.

You just admitted in this sentence that the use of opaque types achieves nothing of value. Which was my point all along: why use them if they are useless? Just to feel smart because I pulled out an academia-flavored ninety-pound dictionary word to describe it?

Re:Parse, Don't Validate (2019) by chowells on Tuesday March 07, 2023 @01:04PM (#35058732)
Opaque types absolutely provide something of value. They're different types. You can't pass an Integer to a function that requires a PrimeNumber. It's a compile error.
Re:Parse, Don't Validate (2019) by crabbone on Tuesday March 07, 2023 @02:58PM (#35060355)
Not in this context they don't. They are useless if you want to ensure that a given number is a prime number.
Re:Parse, Don't Validate (2019) by ParetoOptimal on Tuesday March 07, 2023 @07:30PM (#35063518)
> Not in this context they don't.

What context is it exactly where they don't matter?

I can tell you in practice, in the real world, they very much do.

> They are useless if you want to ensure that a given number is a prime number.

It's not useless. The point is that once you have type `PrimeNumber` that can only be constructed after being validated, you then can write functions exist in a reality where only PrimeNumber exists.

Re:Parse, Don't Validate (2019) by ParetoOptimal on Tuesday March 07, 2023 @07:43PM (#35063646)
I wrote an example with prime numbers that you can run in the Haskell playground:

https://play.haskell.org/saved/gRsNcCGo

> They are useless if you want to ensure that a given number is a prime number.

This is wrong. In the example above `addPrimes` will only take prime numbers.

As such if I make a Jira story that says "add multiply/subtract functions using the PrimeNumber type" I'll know that implementation is simplified by only being able to concern itself with prime numbers.