rescript-lang.org
rescript-lang.org copied to clipboard
Document objects, row polymorphism and constraints
For example a document explaining these would be useful
type userLike<'a> = {.."name": string} as 'a
let extractName: userLike<_> => string = user => user["name"]
let name = extractName({"name": "John", "age": 20})
let name' = extractName({"name": "John", "age": 20, "username": "siever"})
let name'' = extractName({"age": 20, "username": "siever"}) // type error shuold have "name" field
type user = {"name": string}
let user = {"name": "John"}
type userWithAge = {"name": string, "age": int}
let userWithAge = {"name": "John", "age": 21}
// Printing name for a type with subtypes
[user, (userWithAge :> user)]
->Array.map(user => user["name"])
->Array.forEach(Js.log)
Note this is not subtyping. It is row polymorphism
@bobzhang Do you think we should document these features? are we going to preserve these object related features in language or they are likely to be removed in the future?
we will keep this
This should probably go in an advanced section