Type arguments cannot be inferred from usage
This is the code I have now:
And
CreateFailedResult
is implemented as:
I don't really understand why it can't figure out that A1 is going to be string, since that's the type argument for ReadString's return type?6 Replies
The return type of ReadString and the method you call there are entirely unrelated
C# only does inference from arguments to a method. That's it
Surely it should check the type arguments though? Because if I change
CreateFailedResult
to return OperationResult<string>
and remove <A1> it works fineI'm not sure what you mean there. Of course that works, you're providing all the type arguments
I mean when I remove the generic parameter from
CreateFailedResult
and make it return OperationResult<string>
, ReadString
compiles fineAgain, of course it does. You're providing all the type information there
As I said C# only infers type arguments based on parameters. CreateFailedResult has no parameters, therefore no type argument inference can happen
There's no magical "oh, this local is used later in the function to return, therefore I'll go back and do inference"
That sucks