top | item 37343839

(no title)

blkhp19 | 2 years ago

In Swift, you shouldn't use Optional like this. You should instead make a new type to represent exactly what parameter this function expects:

  enum AllowList {
    case all
    case some([ClientIdentifier])
  }

discuss

order

thelopa|2 years ago

I would argue that you should do something similar in Haskell, too. It would ensure that practically everything accessing the allow list fails to compile unless it had been adapted to the new model. Haskell’s strength is the compiler and its ability to pedantically enforce type errors. Use it! Let it help you!

diarrhea|2 years ago

So far, in Rust, that has always felt unidiomatic and I haven't seen this pattern around a lot. It always felt like reimplementing Option/Result but worse: "Why use this when something conceptually similar exists in the stdlib?"

But that is exactly the point: it's only conceptually similar, but in this case deceivingly so. In Rust, we make abundant use of newtypes; this enum is also a new type. Only once it is treated as such does actual type safety kick in. I take it the same principle applies to Haskell. Good to know there's some merit to it.