An Id type that leverages the type system (thanks to phantom types)
to ensure that it refers to the right resource.
For example, if your application handles users with integer-based IDs, and articles with string-based IDs, you could define them as such:
type User
= User
{ id : UserID
, name : String
}
type Article
= Article
{ id : ArticleID
, title : String
}
type alias UserID =
Id String User
type alias ArticleID =
Id Int ArticleMake an Id from its representation.
userID : Id String User
userID =
Id.from "e4edf8a"
articleID : Id Int Article
articleID =
Id.from 5Extract the raw representation from an Id.
Id.to userID == "e4edf8a"
Id.to articleID == 5Encode an Id.
encodeUserID : Id String User -> Value
encodeUserID =
Id.encode Json.Encode.string
encodeArticleID : Id Int Article -> Value
encodeArticleID =
Id.encode Json.Encode.int
The
Id a resourcetype handles IDs whose representation are of typeathat refers to resources of typeresource.