Identity function expression #4412
Replies: 3 comments 5 replies
-
There have been a number of requests for non constant optional parameters. Whilst I could see the general proposal happening (given a suitable design), I dont think the identity function is special enough To warrant special treatment on is own |
Beta Was this translation helpful? Give feedback.
-
Also pretty easily solved with overloads: private static readonly Func<T, T> IDENTITY = f => f;
void Execute() => Execute(IDENTITY);
void Execute(Func<T, T> builder) { ... } |
Beta Was this translation helpful? Give feedback.
-
If it's part of your public API surface you should still null check it. Even with a default parameter someone could pass Obviously it depends on your consumers. If it's all internal (like for your company) and you can enforce nullable enabled, warnings as errors and maybe a few analyzers to boot, you might be safe without null checking. But if this is for public consumption (and likely even if it wasn't), I personally wouldn't elide the checks. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It is quite often I need to develop something a bit abstract than usual and need to use
Func<T, T>
to abstract instance building away. As I start to adopt nullable reference type,Func<T, T>
becomes non-nullable and I need to assignf => f
to it. Consider this methodThe problem is that for me the
builder
parameter exists only to support small use cases. If I would like to make it optional, I would need to write it asbut then the implementation will need to check against null value. The perfect solution would be
This would allow me to use
builder
without null-checking, but it is invalid becausef => f
is not a compile-time constant. Should C# provide something like identity function that works at compile-time to support use case like this? A StackOverflow user has discussed about this since 2009 so I guess I'm not alone.Beta Was this translation helpful? Give feedback.
All reactions