elm-street-0.1.0.2: Crossing the road between Haskell and Elm

Safe HaskellNone
LanguageHaskell2010

Elm.Print.Types

Contents

Description

Pretty functions for elm module.

The generated module should contain:

  • Type definitions for all ADT
  • show* functions for Enum types
  • read* functions for Enum types
  • universe* functions for Enum types
  • un* functions for newtypes

Example

Expand

The example of Record, Newtype and Enum generated type and functions:

type alias User =
    { id : Id
    , name : String
    , age : Age
    , status : RequestStatus
    }

type RequestStatus
    = Approved
    | Rejected
    | Reviewing

showRequestStatus : RequestStatus -> String
showRequestStatus x = case x of
    Approved -> "Approved"
    Rejected -> "Rejected"
    Reviewing -> "Reviewing"

readRequestStatus : String -> Maybe RequestStatus
readRequestStatus x = case x of
    "Approved" -> Just Approved
    "Rejected" -> Just Rejected
    "Reviewing" -> Just Reviewing
    _ -> Nothing

universeRequestStatus : List RequestStatus
universeRequestStatus = [Approved, Rejected, Reviewing]

type Id
    = Id String

unId : Id -> String
unId (Id x) = x
Synopsis

Documentation

prettyShowDefinition :: ElmDefinition -> Text Source #

Pretty shows Elm types.

Internal functions

elmAliasDoc :: ElmAlias -> Doc ann Source #

Pretty printer for Elm aliases:

type alias User =
    { userHeh : String
    , userMeh : Int
    }

elmTypeDoc :: ElmType -> Doc ann Source #

Pretty printer for Elm types with one or more constructors:

type Status a
    = Foo String Int
    | Bar a
    | Baz

If the type is a newtype then additionally unTYPENAME function is generated:

type Id a
    = Id String

unId : Id a -> String
unId (Id x) = x

If the type is Enum this function will add enum specific functions:

type Status
    = Approved
    | Yoyoyo
    | Wow

showStatus : Status -> String
showStatus x = case x of
    Approved -> "Approved"
    Yoyoyo -> "Yoyoyo"
    Wow -> "Wow"

readStatus : String -> Maybe Status
readStatus x = case x of
    "Approved" -> Just Approved
    "Yoyoyo" -> Just Yoyoyo
    "Wow" -> Just Wow
    _ -> Nothing

universeStatus : List Status
universeStatus = [Approved, Yoyoyo, Wow]