I have an IResult<TResult>
generic class and subclasses Success<TResult>
and Failure<TResult>
used for representing successful/failed results and associated information. To cut down on the cumbersome repetition of generic parameters all over the place (it's easy enough when TResult
is just a class, but gets very ugly if there's nested generics), I have an extension method to help with building:
public static class SuccessExtensionsClass
{
public static BuildSuccess<TResult> Success<TResult>(this TResult result)
{
return new Success<TResult>(result);
}
}
Now I can do something like:
TResult blah = new TResult();
then
return blah.BuildSuccess();
So far so good. I don't need to supply the generic type to the extension method, as desired.
However, if I were to use the syntax:
BuildSuccess(blah)
I get a compiler error about the missing generic type parameter. Now, on the other hand, if I change it to:
BuildSuccess<TResult>(blah)
It can't find the method until I specify the class as with a normal static method:
SuccessExtensionClass.BuildSuccess<TResult>(blah)
works fine, as does:
SuccessExtensionClass.BuildSuccess(blah)
Why? Or, more specifically, what motivated this choice? The compiler has all the same information available in both cases, as the error about a missing generic type when called as BuildSuccess(blah)
makes very clear. Is it just a design choice that extension methods can only be called without being qualified with their class name when in extension syntax? Is this for purposes of code clarity alone or is there another reason I'm not thinking of?
As a follow-up, why wouldn't the static syntax be subject to the same rule as instance method syntax, where the compiler will (only) resolve the name if you have explicitly imported the namespace?
edit for hopefully more clarity:
When in instance syntax, I don't have to call the extension method as:
blah.SuccessExtensionsClass.BuildSuccess()
but in static syntax I do need to call it as:
SuccessExtensionsClass.BuildSuccess(blah)
Is this simply to enforce clarity?