Sum types visitation and future continuation similarities

34 views
Skip to first unread message

Vicente J. Botet Escriba

unread,
May 18, 2015, 2:57:21 PM5/18/15
to std-pr...@isocpp.org
Hi,

In one of the variant threads, BS presented this language like example of pattern matching on sum types (union, variants)


    int area(variant<Circle,Square> v)
    {
        inspect(v) {    // look at the variant v
        match Circle c:     return pi*square(c.radius());
        match Square s:   return s.height()*s.width();
        }
    }

It easy to extend the inspection/matching to multiple sum types.

A library solution applied to multiple variants could be

inspect(v, b).match(
  [](Circle c, int j )
    {
    },
  [](auto i, autoj )
    {
      // ...
    }
  );
This is quite close to the when_all(f1, f2).then(); expressions. This let me think that we could have a match function for futures because a future has either a value or an exception_ptr, so a sum type

f.match(
  [](Circle c)
    {
    },
  [](auto)
    {
      // ...
    }
  );

and that when_all would be close to inspect but applied to futures
when_all(a, b).match(
  [](Circle, int j )
    {
    },
  [](auto i, auto j )
    {
      // ...
    }
  );

Just wanted to share this similarity.

Vicente


Reply all
Reply to author
Forward
0 new messages