Skip to content

Purescript Package that has a type class which provides default value for types.

License

Notifications You must be signed in to change notification settings

imsaravana369/purescript-default

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

purescript-default

A type class which provides default value.

Why this library?

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

About

Purescript Package that has a type class which provides default value for types.

Resources

License

Stars

Watchers

Forks

Packages

No packages published