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

Already on GitHub? Sign in to your account

Add a match() function to std.variant for pattern matching on discriminated unions. #645

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
5 participants
Member

alexrp commented Jun 24, 2012

This is much nicer than the equivalent if/else forest people typically use to operate on Variant values.

Member

DmitryOlshansky commented Jun 24, 2012

Looks cool. Can it have a default clause? Like in std.concurency:
...
(Varaint v){
// unexpected type, do something sensible
}

Member

alexrp commented Jun 24, 2012

Currently you do that this way:

match(var,
      (int x) => ...,
      (float y) => ...,
      () => ... /* default */);

This function is heavily inspired by ML-style pattern matching, where, if you hit the default case, you usually don't need to work with the value anyway, so it isn't passed a parameter to the default case function. Of course, nothing stops you from using var in there.

Member

brad-anderson commented Jun 27, 2012

Just testing something. Ignore this unrelated comment and sorry about the noise.

Test Results

Contributor

jpf91 commented Jun 27, 2012

That's a very nice pull request!

A short example in the documentation would be cool though, just like the example you posted above.

Member

alexrp commented Jun 27, 2012

I'm really terrible at coming up with sensible examples, though. Could someone come up with something that would be useful to do an actual computation of sorts?

Owner

andralex commented Jul 9, 2012

How does this compare with visit()?

Member

alexrp commented Jul 9, 2012

Will reopen once I address some of the issues here.

@alexrp alexrp closed this Jul 9, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment