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

Matching multiple with single case #17

Open
OliverJAsh opened this issue Mar 15, 2018 · 2 comments
Open

Matching multiple with single case #17

OliverJAsh opened this issue Mar 15, 2018 · 2 comments

Comments

@OliverJAsh
Copy link
Collaborator

Currently one has to duplicate cases:

export const getGridCountForDevice = Device.match({
  Phone: () => GridCount.Two({}),
  Tablet: () => GridCount.Two({}),
  Desktop: () => GridCount.Three({}),
});

Or use defaults:

export const getGridCountForDevice = Device.match({
  Desktop: () => GridCount.Three({}),
}, () => GridCount.Two({}));

It would be fantastic if unionize had a way of pattern matching multiple tags with a single case:

export const getGridCountForDevice = Device.match([
  [['Tablet', 'Phone'], () => GridCount.Two({})],
  ['Desktop', () => GridCount.Three({})],
]);

Although I imagine this would complicate the behind-the-scenes types of match quite drastically.

@pelotom
Copy link
Owner

pelotom commented Mar 15, 2018

It’s an interesting idea, I like it!

@mischkl
Copy link

mischkl commented Apr 17, 2018

This is something that's missing when using unionize for reducers as oppose to switch/case. e.g.

  switch (action.type) {
    case 'Tablet':
    case 'Phone': {
        return GridCount.Two({});
    }
    case 'Desktop':
        return GridCount.Three({});
}

I suppose the same thing could be achieved using match by declaring the callback externally, e.g.

export const getGridCountForDevice = Device.match({
  Phone: getGridCountTwo,
  Tablet: getGridCountTwo,
  Desktop: () => GridCount.Three({}),
});

function getGridCountTwo() {
  return GridCount.Two({});
}

though that makes it less explicit that multiple tags are being matched to the same function. So the syntax suggested by @OliverJAsh definitely has that benefit.

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

3 participants