A type class which provides default value.
Many times we need to pass some placeholder values as initial values to certain functions, e.g Passing empty string to Halogen Custom EdiText Component.
type PersonInfo = { name :: String, isMarried :: Boolean, salary :: Maybe Int }
To create empty placeholder value,
defaultPersonInfo :: PersonInfo
defaultPersonInfo = defaultValue
It also comes in handy when you want to provide a default value, like in case of fromMaybe
getPersonInfo :: Maybe PersonInfo -> PersonInfo
getPersonInfo mPersonInfo = fromMaybe defaultValue mPersonInfo
The DefaultValue type class is defined for handful of primitive types and also for some other types like Maybe, Either, Tuple etc.
But you can also use defaultValue
for your custom made type by just creating a DefaultValue
instance for that.
data Color = RED | YELLOW | GREEN
instance defaultColorInstance :: DefaultValue Color where
defaultValue = GREEN
--- >>> defaultValue :: Color
--- >>> GREEN