rescript-association / rescript-lang.org

Official documentation website for the ReScript programming language
https://rescript-lang.org
MIT License
1.86k stars 246 forks source link

Document objects, row polymorphism and constraints #394

Open amiralies opened 3 years ago

amiralies commented 3 years ago

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)
bobzhang commented 3 years ago

Note this is not subtyping. It is row polymorphism

amiralies commented 3 years ago

@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?

bobzhang commented 3 years ago

we will keep this

ryyppy commented 3 years ago

This should probably go in an advanced section