4 ms·
You've gotten some great answers but lemme add another thing: The zero type is useful for things that should not happen that you can prevent at compile time, wh
by 1-more 2y ago
You've gotten some great answers but lemme add another thing: The zero type is useful for things that should not happen that you can prevent at compile time, while the unit type is nice for things that can only happen one way.
In Elm (a functional language for making web UIs), you might use the unit type for a lazy view function. If and only if some condition is true, call a view function to render some stuff into your DOM tree as in the function viewIfLazy [0]. There's only one way to call that view function, so let's call it with the unit type.
Now, what about using the Never type (a type with no members that can be instantiated at runtime [1]) to do accessibility? In Elm, your DOM elements have type `Html msg` where msg is a type _parameter_ for your actual message type that makes your app update itself. Usually you create that type like `type Message = UpdatedTheFooCounter Int | UpdatedTheNameInput String | ClickedTheBlahButton` something like that. Then the type on your inputs and buttons will be `Html Message` (read as "Html of Message" or even "Html parameterized by Message").
Well, it's bad a11y practice to have onclick events attached to divs and spans and things that aren't buttons, generally. So you can give your divs type `Html Never` and then if you try to attach an onclick event to them, you find that you can't at compile time, because the members of the type Message are not members of the type `Never`. tesk9/accessible-html is a package that enforces that [2] (I kind of simplified how it does that because I'm deep enough in the weeds already).
[0] https://github.com/elm-community/html-extra/blob/0b8fc70752c9120b6ef997976855f3fde502de26/src/Html/Extra.elm#L65-L84 https://github.com/elm-community/html-extra/blob/0b8fc70752c...
[1] https://package.elm-lang.org/packages/elm/core/latest/Basics#Never https://package.elm-lang.org/packages/elm/core/latest/Basics... also has a simpler example now that I think of it.
[2] https://github.com/tesk9/accessible-html/blob/master/src/Accessibility.elm#L603-L608 https://github.com/tesk9/accessible-html/blob/master/src/Acc...