Create upwards compatible APIs

This topic has been covered by Elm Patterns

type Movie =
        { title : String
        , rating : Int
        , -- adding a field will destroy the function below

new : String -> Int -> Movie
new title rating =
    { title = title
    , rating = clamp 1 5 rating


How can I define my type, such that I can add features without breaking the API?


First write a constructor fromTitle that only uses as few arguments as possible. Next add partial constructors for every feature of your type: withRating and withDirector.

Now creating a new Movie can be done like this:

Movie.fromTitle "Life of Brian"
|> Movie.withDirector "Terry Jones"
|> Movie.withRating 5

Further Reading

Last updated