sumtype 0.10.0: multiple dispatch

Paul Backus snarwin at gmail.com
Thu Sep 24 02:28:11 UTC 2020


SumType is a generic sum type for modern D. It is designed to be 
an improved
alternative to `std.variant.Algebraic`.

Features:
   - Pattern matching, including:
     - Match-by-introspection ("if it compiles, it matches") (★)
     - Multiple dispatch (★)
   - Support for self-referential types (`This`).
   - Works with `pure`, `@safe`, `@nogc`, `nothrow`, and 
`immutable` (★)
   - Compatible with `-betterC` and `-dip1000` (★)
   - Zero runtime overhead compared to hand-written C
       - No heap allocation
       - Does not rely on runtime type information (`TypeInfo`) (★)

Starred features (★) are those that are missing from `Algebraic`.

The big new feature in this release is multiple dispatch: you can 
pass multiple
SumType arguments to a single `match` call, and it will pass each 
of those
SumTypes' values as a separate argument to the selected handler.

If you're used to calling `match` with UFCS, the syntax for 
passing multiple
arguments may take some getting used to. I recommend the 
following idiom:

     bool sameDimensions(Point p1, Point p2)
     {
         // Set up your handlers first
         alias doMatch = match!(
             (Point2D _1, Point2D _2) => true,
             (Point3D _1, Point3D _2) => true,
             (_1, _2) => false
         );

         // Now make the actual call
         return doMatch(p1, p2);
     }

Other improvements since 0.9.0, the last announced version:
   - SumTypes can be used as keys in associative arrays
   - isSumType!T is now true if T implicitly converts to a SumType
   - sumtype's license has been changed from MIT to Boost 1.0
   - Member types with non-const `opEquals` overloads finally work 
correctly
   - Various other bug fixes and documentation improvements

Links:
   - Documentation: https://pbackus.github.io/sumtype/sumtype.html
   - DUB: https://sumtype.dub.pm
   - Github: https://github.com/pbackus/sumtype


More information about the Digitalmars-d-announce mailing list