rescript-lang.org icon indicating copy to clipboard operation
rescript-lang.org copied to clipboard

Document objects, row polymorphism and constraints

Open amiralies opened this issue 4 years ago • 4 comments

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)

amiralies avatar Jun 19 '21 10:06 amiralies

Note this is not subtyping. It is row polymorphism

bobzhang avatar Jun 20 '21 03:06 bobzhang

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

amiralies avatar Jun 20 '21 08:06 amiralies

we will keep this

bobzhang avatar Jun 20 '21 23:06 bobzhang

This should probably go in an advanced section

ryyppy avatar Jun 22 '21 08:06 ryyppy