Open Bug 1257909 Opened 8 years ago Updated 2 years ago

Make Variant matching syntactically prettier and w/ less boilerplate

Categories

(Core :: MFBT, defect)

defect

Tracking

()

People

(Reporter: fitzgen, Unassigned)

References

Details

Defining a matcher struct that needs to "close over" some local sucks: you have to spell out the structure that the C++ compiler would automatically generate if you were using a lambda *and* you have to define a constructor to put the references in the right places.

Even when not closing over any locals, the matcher struct is defined separately from where it's used.

This stuff is not ergonomic.

How can we use lambdas instead?

The only way I can think of is to require a lambda for each variant type, in order. For example:

  Variant<Foo, Bar, Baz> v(...);
  v.match(
    [&](Foo& foo) { ... },
    [&](Bar& bar) { ... },
    [&](Baz& baz) { ... }
  );

It looks a lot nicer, and doesn't require spelling out all the closed over things explicitly, but it does require remembering the proper order of types. Maybe that's ok?

Maybe there is a better way?
Is there any way to do defaults with this approach? There are no template lambdas, right?

It seems like, as long as everything is well-typed, we could always provide both, and see what people choose.
See Also: → 1232686
This method isn't possible while we still support MSVC 2013, because using lambdas would require auto returns with decltype, which MSVC 2013 ICEs on.

Unassigning for now.
Assignee: nfitzgerald → nobody
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.