55 ms·
> And defining a function that will only accept particular variant is nice This is possible to achieve (or hack your way through, if you will) by parameterizin
by bPspGiJT8Y 2y ago
> And defining a function that will only accept particular variant is nice
This is possible to achieve (or hack your way through, if you will) by parameterizing the type and using a nullary type (a type which is impossible to have) to exclude specific cases of a sum type. In Haskell this would look like this:
data Weather a b c = Sunny a | Rainy b | Snowy c
-- can't snow in the summer!
onlySummerWeather :: forall a b. Weather a b Void -> String
onlySummerWeather weather = case weather of
Sunny _ -> "Got sunny weather"
Rainy _ -> "Got rainy weather"
Snowy v -> absurd v
where `absurd :: forall a. Void -> a` "if you give me something you can't ever have, I will give you anything in return".
- mst 2y agoI would be so tempted to name the variable 'ity' instead of 'v'.
- Iceland_jack 2y agoBy floating the forall., we get another representation type for Void (forall a. a) and `absurd' is one half of that isomorphism :) absurd :: Void -> (forall a. a) drusba :: (forall a. a) -> Void drusba void = void @Void