Replies: 2 comments
-
Answered here: #403 (comment) , but wanted to re-iterate that technically, extension methods could target interfaces, but they would still need to return specific types even then. And this just doesn't look good symmetry-wise. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Performance is not an issue here, that's true. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Extension methods declared on
Result
types accept the struct as an extension point, and not the interface. Thus, we havepublic static Result<T, E> Ensure<T, E>(this Result<T, E> result, Func<T, bool> predicate, E error)
, and notpublic static IResult<T, E> Ensure<T, E>(this IResult<T, E> result, Func<T, bool> predicate, E error)
. The same is true forResult<T>
vsIResult<T>
andUnitResult<E>
vsIUnitResult<E>
.In comments to #403 (#403 (comment)) you said that
Why is that? And is it a general rule, or a rule specific to
CSharpFunctionalExtensions
?Targeting interfaces with extension methods seems to be pretty common, i.e. LINQ using it all the time to extend
IEnumerable<T>
, and various fluent interfaces from multiple libraries making heavy use of it. It also would seem that targeting interface instead of concrete type makes the code more flexible.The
Result
type is a struct. This may or may not be relevant to the targeting question. One can find an argument, that targeting structs with extension methods is not recommended, because they're tricky in regards to modifying them inside the extension method, as they're passed by reference. However,Result
is immutable by design, so this argument is irrelevant here.I don't think performance arguments also hold water, as there doesn't seem to be any boxing/unboxing involved when calling extension method on a struct.
So, why aren't extension methods targeting
Result<T, E>
and notIResult<T, E>
? Is it for some historic reasons (consistency and/or backwards compatibility), or something else entirely?Beta Was this translation helpful? Give feedback.
All reactions