www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - sumtype 0.10.0: multiple dispatch

reply Paul Backus <snarwin gmail.com> writes:
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
Sep 23 2020
next sibling parent Bruce Carneal <bcarneal gmail.com> writes:
On Thursday, 24 September 2020 at 02:28:11 UTC, Paul Backus wrote:
 SumType is a generic sum type for modern D. It is designed to 
 be an improved
 alternative to `std.variant.Algebraic`.

 [...]
Sure looks like a strong advance. Hope it sees a lot of use.
Sep 23 2020
prev sibling next sibling parent WebFreak001 <d.forum webfreak.org> writes:
On Thursday, 24 September 2020 at 02:28:11 UTC, Paul Backus wrote:
 [...]
   - DUB: https://sumtype.dub.pm
   - Github: https://github.com/pbackus/sumtype
Seems like dub.pm is still down, has been broken since like 6 months now :/ But your library looks really powerful, would love to see sumtype be included in phobos!
Sep 24 2020
prev sibling parent reply vitamin <vit vit.vit> writes:
On Thursday, 24 September 2020 at 02:28:11 UTC, Paul Backus wrote:
 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
Hello, Older version of sumtype accept this code: void main(){ import sumtype; alias Val = SumType!(bool); const bool b = true; Val val = b; //fail in newest version val = b; //fail in newest version } but new version need exact type: void main(){ import sumtype; alias Val = SumType!(bool); bool b = true; //cannot be const Val val = b; val = b; } is it bug in new version or old version?
Oct 27 2020
parent Paul Backus <snarwin gmail.com> writes:
On Tuesday, 27 October 2020 at 16:26:10 UTC, vitamin wrote:
 Hello,

 Older version of sumtype accept this code:

 void main(){
     import sumtype;
     alias Val = SumType!(bool);

     const bool b = true;
     Val val = b;  //fail in newest version
     val = b;      //fail in newest version
 }

 but new version need exact type:


 void main(){
     import sumtype;
     alias Val = SumType!(bool);

     bool b = true;   //cannot be const
     Val val = b;
     val = b;
 }

 is it bug in new version or old version?
It's a bug in the new version. Thanks for reporting. I've opened an issue on Github for this: https://github.com/pbackus/sumtype/issues/47
Oct 27 2020