question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I was thinking it might be quite nice to have an opt-in feature that makes you rely on factory methods to create the different types within a union. I was thinking this could be another attribute maybe [UnionWithFactory] or something like [Union(factory: true)].

I took the code example from the samples and modified it to show how the factory methods could be used:

using Dunet.Choices;
using Dunet.Shapes;

var rectangle = Shape.Rectangle(10, 10);
var triangle =  Shape.Triangle(10, 10);
var circle = Shape.Circle(10);

var getArea = (IShape shape) =>
    shape switch
    {
        Rectangle rect => rect.Length * rect.Width,
        Circle circle => 2.0 * Math.PI * circle.Radius,
        Triangle triangle => 1.0 / 2.0 * triangle.Base * triangle.Height,
        _ => 0d,
    };

var rectangleArea = getArea(rectangle);
var triangleArea = getArea(triangle);
var circleArea = getArea(circle);

Console.WriteLine($"Rectangle area: {rectangleArea}");
Console.WriteLine($"Triangle area: {triangleArea}");
Console.WriteLine($"Circle area: {circleArea}");

var choice = GetChoice();

if (choice is Yes)
{
    Console.WriteLine("YES!!!");
}

if (choice is No)
{
    Console.WriteLine("NO!!!");
}

static IChoice GetChoice() =>
    Console.ReadLine() switch
    {
        "yes" => Option.Yes(),
        _ => Option.No()
    };

What do you think?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
saulcommented, Jun 27, 2022

Preliminary PR for this here: https://github.com/domn1995/dunet/pull/33

0reactions
domn1995commented, Jul 6, 2022

After some more analysis, I’m going to defer this feature for the following reasons:

  • The ability of users to write Foo.NewThing() rather than new Foo.Thing() is not worth the increase in complexity and potential maintenance burden to this library.
  • Source generators already have a steep learning curve, due to a lot of hidden/non-explicit behavior. I believe a successful source generator should do as much as it can to minimize such behavior and take an explicit approach whenever possible. This feature runs counter to that philosophy.
  • With #28 on the docket, constructors and factory methods will not be necessary for most use cases. This is because users will be able to assign inner union values directly to the union type. This will bring Dunet unions in line with most other language’s DU implementations and effectively obsolete constructors and factory methods. For example:
// No need for constructor or factory methods to instantiate union values.
StringOrNumber str = "foo";
StringOrNumber num = 123;

[Union]
partial record StringOrNumber
{
    partial record Str(string Value);
    partial record Num(double Value);
}

If the community remains interested in this feature, I would whole-heartedly support a separate package containing this functionality. I’d also be glad to link to it from Dunet’s readme if that becomes the case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Factory Method
Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the...
Read more >
Factory method pattern
In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects ......
Read more >
Factory method for designing pattern
The factory method is a creational design pattern, i.e., related to object creation. The Factory Method pattern is used to create objects without...
Read more >
Factory Method Design Pattern
A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the...
Read more >
Factory Method Design Pattern
Factory Method is to creating objects as Template Method is to implementing an algorithm. A superclass specifies all standard and generic behavior (using ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found