4 ms·
>[…] (simply using a list of tuples with two elements). Yes, but this is an approach based on thinking not on which values we want to exclude, but rather how t
by chrisoverzero 6y ago
>[…] (simply using a list of tuples with two elements).
Yes, but this is an approach based on thinking not on which values we want to exclude, but rather how to structure our data such that those illegal values aren’t even constructible.
You must have forgotten that you read about this approach in the article.
- jakear 6y agoSo instead of having a .push method that takes a list of two arguments, I have a Cons2 constructor that takes two arguments... groundbreaking. If the Haskell method gave you iterability for free I could see the benefit, but it doesn’t.
- lexi-lambda 6y agoIt actually does, though I didn’t mention it in the blog post. Here’s how you write it: data EvenList a = EvenNil | EvenCons a a (EvenList a) deriving (Functor, Foldable, Traversable) But that isn’t really the point of the example. As the blog post itself points out, it’s likely more useful to use a list of pairs in most situations, and that choice would still be a constructive representation. The example is not about exactly the way you choose to represent the property in question. The example is a demonstration that it’s possible to capture these properties using your type system even if it doesn’t have any first-class support for constraining list types this way. You can choose whichever representation you like the most/is most natural in your language of choice, of course, and you should! The blog post is just pointing out that you shouldn’t give up altogether on being able to capture these properties without changing the type system itself.
- valenterry 6y agoI think I'm just confused by the examples chosen. E.g.: > type NonEmptyArray<T> = [T, ...T[]]; I'm not very familiar with Typescript, but isn't that also a "constructive" approach, rather than a "constraining" one? It reads to me like "NonEmptyArray is an array that has one element and then more elements that come from pulling elements from another array". Typescript uses a language construct here for a special usecase, but the point still stands.