Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Type of a microstate? #273

Open
taras opened this issue Nov 11, 2018 · 7 comments
Open

How to get Type of a microstate? #273

taras opened this issue Nov 11, 2018 · 7 comments
Labels

Comments

@taras
Copy link
Member

taras commented Nov 11, 2018

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.

@cowboyd
Copy link
Member

cowboyd commented Nov 11, 2018

microstate.constructor.Type

@taras
Copy link
Member Author

taras commented Nov 11, 2018

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

@cowboyd
Copy link
Member

cowboyd commented Nov 12, 2018

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

@taras
Copy link
Member Author

taras commented Nov 12, 2018

How do we do that?

@cowboyd
Copy link
Member

cowboyd commented Nov 12, 2018

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

@cowboyd
Copy link
Member

cowboyd commented Nov 26, 2018

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
Copy link
Member

cowboyd commented Dec 4, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants