Skip to content

Instantly share code, notes, and snippets.

@arowM
Last active November 29, 2017 08:14
Show Gist options
  • Save arowM/836d478bda8cac261e1e5cab200a5ab0 to your computer and use it in GitHub Desktop.
Save arowM/836d478bda8cac261e1e5cab200a5ab0 to your computer and use it in GitHub Desktop.
Default.elm
module Default
exposing
( Default
, default
, int
, string
, maybe
, stubTask
)
import Task exposing (Task)
{-| Phantom type to define default value of type `a`.
```
class Default a where
default :: a
```
-}
type Default a
= Default a
default : Default a -> a
default (Default a) =
a
{-|
```
instance Default Int where
default :: Int
default = 0
```
-}
int : Default Int
int =
Default 0
string : Default String
string =
Default ""
maybe : Default a -> Default (Maybe a)
maybe (Default a) =
Default <| Just a
{-| A Stub.
stubTask int (Task.succeed 10)
--> 0
stubTask string (Task.succeed "foo")
--> ""
stubTask (maybe string) (Task.succeed <| Nothing)
--> Just ""
stubTask (maybe (maybe int)) (Task.succeed <| Just (Just 3))
--> Just (Just 0)
-}
stubTask : Default a -> Task error a -> a
stubTask def _ =
default def
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment