microstates
microstates copied to clipboard
How to get Type of a microstate?
We don't have a way to get the Type of a Microstate. This makes it difficult when I want to be able to serialize a Microstate where the microstate went through a few transitions. Especially when the microstate might have gone a type change via set
. We have metaOf
, valueOf
but not typeOf
.
microstate.constructor.Type
I will test it but I believe there were some weird edge cases. Should we make typeOf
that does microstate.constructor.Type
?
Actually, we probably need to return the type that create
was called with before any type changes happen.
let light = create(TrafficLight, 'green');
light.constructor.Type //=> class GreenLight {}'
typeOf(light) //=> TrafficLight
How do we do that?
I'm not exactly sure how aside from making union types first-class citizens.
Upon reflection, I think we need to have ways to access them all. We can start storing the Type
on the meta
of each microstate. Where Type
is the intended, and potentially abstract type of the microstate.
import { typeOf } from 'microstates';
let five = create(Maybe(Number), 5);
typeOf(five) //=> Maybe<Number>
five.constructor.Type //=> Just<Number>
let anulled = five.set(null);
typeOf(anulled) //=> Maybe<Number>
annulled.constructor.Type //=> Nothing
Worth noting that subatomic microstate rely on storing the original Type https://github.com/microstates/lab/blob/master/src/meta.js#L40-L42
So when / if this makes it into master, there will be a typeOf(microstate)
operation on every microstate.