Newbie here. I'm trying to understand some of the machinery behind purescript-variant. Basically what I mean is how to understand these 2 function: inj and on.
When I look at the type of inj
, the author makes use of Cons typeclasses. From my understanding, Cons
place an assertion that there's a record r2
that can be made from some other record r1
by inserting a pair of tag/value. So in a sense:
r1 + tag/value = r2
And in this case Variant
is the r2
.
Citing from the readme:
foo :: forall v. Variant (foo :: Int | v)
foo = inj (SProxy :: SProxy "foo") 42
bar :: forall v. Variant (bar :: Boolean | v)
bar = inj (SProxy :: SProxy "bar") true
fooToString :: forall v. Variant (foo :: Int | v) -> String
fooToString = on (SProxy :: SProxy "foo") show (\_ -> "not foo")
This is where it gets confusing for me. I can feed the bar
as an argument to fooToString
while from the type of bar
there's no guarantee that it has a tag "foo" even if it's an open type. How is this possible?
fooToString
itself doesn't restrict that the variant has to have a tag of "foo" in it. And also becaue of the open types. Is this true? – autumn322