purescript / purescript-typelevel-prelude

Types and kinds for basic type-level programming
BSD 3-Clause "New" or "Revised" License
63 stars 21 forks source link

Adding RowList Limits #60

Closed mwalkerwells closed 4 years ago

mwalkerwells commented 4 years ago

This diff allows you to limit the number or rows in a type, most notably records. The API provides both kind Int instance matching and class aliases, such as LimitCount One.

-- Using: class LimitCount

oneRow1a :: forall r rl. RowToList r rl => LimitCount One rl => { | r } -> String
oneRow1a = ...

... oneRow1a { } -- Works
... oneRow1a { a: 1 } -- Works
... oneRow1a { a: 1, b: 2 } -- Error: "Could not match type"

-- Using Alias: class LimitCountThree

oneRow1b :: forall r rl. RowToList r rl => LimitCountThree rl => { | r } -> String
oneRow1b = ...

... oneRow1b { } works
... oneRow1b { a: 1 } -- Works
... oneRow1b { a: 1, b: 2 } -- Works
... oneRow1b { a: 1, b: 2, c: 3 } -- Works
... oneRow1b { a: 1, b: 2, c: 3, d: 4 } -- Error: "Could not match type"

-- Using: class ExactCount

oneRow1a :: forall r rl. RowToList r rl => ExactCount One rl => { | r } -> String
oneRow1a = ...

... oneRow1a { } -- Error: "Could not match type"
... oneRow1a { a: 1 } -- Works
... oneRow1a { a: 1, b: 2 } -- Error: "Could not match type"

-- Using Alias: class ExactCountOne

oneRow1b :: forall r rl. RowToList r rl => ExactCountOne rl => { | r } -> String
oneRow1b = ...

... oneRow1b { } -- Error: "Could not match type"
... oneRow1b { a: 1 } -- Works
... oneRow1b { a: 1, b: 2 } -- Error: "Could not match type"
hdgarrood commented 4 years ago

Thanks for the PR, but since this is library is very low-down in the ecosystem we need to be very conservative about adding things to it. This seems like it might be a better candidate for living in a separate library to me.

mwalkerwells commented 4 years ago

https://pursuit.purescript.org/packages/purescript-typelevel-rowlist-limits