One thing I like about F# is the ability to use readonly objects in a way that does not require a lot of code. I’ve done a small lib inspired by this ability of F#: with
The syntax is a bit special since I assume that the properties have a private set.
myobject.With(m => m.Property1 == 3 && m.Property2 == "3")
using With;
...
public class SomeClass
{
private static readonly IPreparedCopy<MyClass, int, string> PreparedCopy =
Prepare.Copy<MyClass, int, string>((m,v1,v2) => m.Property1 == v1 && m.Property2 == v2);
public async Task<MyClass> Handle()
{
// fetch instance of MyClass, say:
var myObject = await _storage.Load(someId);
// change the name of that customer:
var copy = PreparedCopy.Copy(myObject, 3, "3");
// ...
}
}
The reason for this style of syntax is due to the fact that compiling expressions are potentially expensive, why you’d want to have a similar pattern as compiled regex.
Do you want to send a comment or give me a hint about any issues with a blog post: Open up an issue on GitHub.
Do you want to fix an error or add a comment published on the blog? You can do a fork of this post and do a pull request on github.
Comments