about summary refs log blame commit diff
path: root/users/Profpatsch/whatcd-resolver/src/Optional.hs
blob: 9791c8497097549287b7180422d2193ab6596f96 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

















                                                              
module Optional where

import GHC.Records (getField)
import MyPrelude

newtype Optional a = OptionalInternal (Maybe a)
  deriving newtype (Functor)

mkOptional :: a -> Optional a
mkOptional defaultValue = OptionalInternal $ Just defaultValue

defaults :: Optional a
defaults = OptionalInternal Nothing

instance HasField "withDefault" (Optional a) (a -> a) where
  getField (OptionalInternal m) defaultValue = case m of
    Nothing -> defaultValue
    Just a -> a