polyadic / funcky

Funcky is a Functional Library for C#
https://polyadic.github.io/funcky
Apache License 2.0
19 stars 3 forks source link

Idea: the DU source-generator automatically writes the dispatching of the Match. #779

Open FreeApophis opened 8 months ago

FreeApophis commented 8 months ago

I often rely on polymorphic overloads of the same method to handle DU cases indivdually:

Ideally I would have some syntax like this:

shape.Dipatch(Canvas.Draw);

This is obviously not possible, usually I write code like this: I write a public function which is delegating to individual overloads of the same Methodgroup. This is similar to the accept method in the visitor pattern:

public static Task<Image> Draw(Shape shape)
        => shape.Match(triangle: Draw, rectangle: Draw, polygon: Draw);

Source Generator:

[DispatchToMethodGroup] // name to discuss...
public partial static Task<Image> Draw(Shape shape)

DispatchToMethodGroup could take a parameter name optionally.

[DispatchToMethodGroup(nameof(Canvas.DrawInternal))]
public partial static Task<Image> Draw(Shape shape)