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 enumerate thru all type variants #34

Open
avesus opened this issue Mar 12, 2016 · 2 comments
Open

How to enumerate thru all type variants #34

avesus opened this issue Mar 12, 2016 · 2 comments

Comments

@avesus
Copy link

avesus commented Mar 12, 2016

There is a convenient case() function which allows to switch on precise type. But for library code it is very useful to enumerate thru all type keys.

const Action = Type({
  Walk: [],
  Run: []
});

Action.forEach(name => console.log(name));

should output

Walk
Run
@paldepind
Copy link
Owner

What is your use case for this?

@avesus
Copy link
Author

avesus commented Mar 14, 2016

As long as union-type can work as what in VisualBasic/Microsoft terminology called by word 'Variant', it should be possible to know every kind of types which it could have. For an example, in testing by passing all variant types instances. The case() function only allows us to switch on exact already passed variant, but not dynamically determine which other variants it may have.

My particular use case is in a framework, where I implement multilevel plugins inheritance of components, and it's necessary to know all parent's types to effectively match wich parts the child overrides/extends.

I can submit a patch, if you'll find that useful and confirm on the API. Sure, some sort of the Array operations should be supported, so, may be some variantKeys() function which work like Object's keys(). Or types(), which sound more concisely IMO.

The task is just to filter out the case and caseOn elements from the Type's keys() method call resulting array. My current solution is very ugly and depends on type variants names capitalization. Otherwise, excluding the method names in the client code is like a hell when you'll add new methods ;-)

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

No branches or pull requests

2 participants