microstates icon indicating copy to clipboard operation
microstates copied to clipboard

How to get Type of a microstate?

Open taras opened this issue 5 years ago • 7 comments

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.

taras avatar Nov 11 '18 19:11 taras

microstate.constructor.Type

cowboyd avatar Nov 11 '18 21:11 cowboyd

I will test it but I believe there were some weird edge cases. Should we make typeOf that does microstate.constructor.Type?

taras avatar Nov 11 '18 23:11 taras

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

cowboyd avatar Nov 12 '18 02:11 cowboyd

How do we do that?

taras avatar Nov 12 '18 14:11 taras

I'm not exactly sure how aside from making union types first-class citizens.

cowboyd avatar Nov 12 '18 17:11 cowboyd

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

cowboyd avatar Nov 26 '18 17:11 cowboyd

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.

cowboyd avatar Dec 04 '18 17:12 cowboyd