DavidArno / SuccincT

Discriminated unions, pattern matching and partial applications for C#
MIT License
268 stars 15 forks source link

feat: add With() method based on with keywork from F# #44

Closed Odonno closed 4 years ago

Odonno commented 6 years ago

It would be interesting to add immutable syntax using the with keyword from F#. Here is a proposal :

var oldComputer = new Computer
{
    Os = "Windows 7",
    Cpu = "Intel Core i5"
};

var computerWithNewOs = oldComputer.With(new { Os = "Windows 10" });
var computerWithNewCpu = computerWithNewOs.With(new { Cpu = "Intel Core i7" });
DavidArno commented 6 years ago

Whilst this is in theory achievable, the only way I can think to do this would necessitate a lot of reflection use and so would be slow. I'd question whether it's worth it therefore when pattern-based with expressions are currently pencilled in for C# 8.

Odonno commented 6 years ago

@DavidArno Indeed, it would use reflection to create a new Immutable object and detect properties to overwrite. I can start with a PR until we have C# 8.

DavidArno commented 6 years ago

PR's are always welcome 😀

DavidArno commented 6 years ago

Bit slow on the uptake here, but I have just realised that because this feature requires System.Runtime it breaks the netstandard 1.0 version of SuccincT. I could update that to netstandard 1.5, but that would be a breaking change.

I am therefore pulling this feature from the v3.1 release and will reschedule it for the v4.0 release (which is allowed to make such breaking changes).

Apologies for the last minute change of plan here.

Odonno commented 6 years ago

Good to know. I will have some time to make some adjustments on the feature so.

Odonno commented 6 years ago

@DavidArno For information, I have made the wiki page for Copy/With functions https://github.com/DavidArno/SuccincT/wiki/Copy---With

DavidArno commented 4 years ago

Released with v4.