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 Int User
type alias ArticleID =
Id String ArticleMake an Id from its representation.
userID : Id Int User
userID =
Id.from 5
articleID : Id String Article
articleID =
Id.from "e4edf8a"Extract the raw representation from an Id.
Id.to userID == 5
Id.to articleID == "e4edf8a"Encode an Id.
encodeUserID : Id Int User -> Value
encodeUserID =
Id.encode Json.Encode.int
encodeArticleID : Id String Article -> Value
encodeArticleID =
Id.encode Json.Encode.string
The
Id a resourcetype handles IDs whose representation are of typeathat refers to resources of typeresource.