What’s the reason records cannot be assigned to a record that has a subset fields?
type A = {a :: Int}
type B = {a :: Int, b :: Int}
bx :: B
bx = {a: 1, b: 2}
ax :: A
ax = (\{a} -> {a}) bx -- Works
ax = bx -- Compile error: Type of expression contains additional label b.
``
Hi,
the types are different:
-
bx
is B
- you claim
a
to be A
but assign an B
- there is no automatic casting/coercing and no subtype casting in PureScript
the middle one works as:
\{a} -> \{a}
has basically
\forall t r. { a :: t | r } -> { a :: t }
Is there a way to do it manually without introducing the additional \{a} -> {a}
function?
Well you can use the forall ... r. ... { a :: t | r } -> ...
type everywhere you want to use it like that. Or give the function some name and use it.
There is probably something like this already defined in some library and unsafeCoerce
should work too.
1 Like
record-studio
has a shrink
function that you can use:
import Prelude
import Effect (Effect)
import Effect.Console (log)
import Record.Studio (shrink)
type A = { a :: Int }
type B = { a :: Int, b :: Int }
bx :: B
bx = { a: 1, b: 2 }
ax :: A
ax = shrink bx
3 Likes