-
Notifications
You must be signed in to change notification settings - Fork 1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Stronger type Inference for generics calls #151
Comments
Yes, and if classes |
Another thing that I've always found mildly irritating is that if any one of the type arguments cannot be inferred by the compiler then you must manually supply all type arguments. Example: void MyFunc<T1>(T1 item) => ...
// I can call the above function without supplying the type argument:
MyFunc("Hello world"); However, if I make no other change than to simply add another type argument: void MyFunc<T1, T2>(T1 item) => ...
// I must now supply all type arguments because T2 cannot be inferred by the compiler:
MyFunc<string, object>("Hello world"); Now that C# will introduce some kind of wildcard into the language, can we use that to do F#-style partial type inference? void MyFunc<T1, T2>(T1 item) => ...
// I can now supply *some* of the type arguments
MyFunc<_, object>("Hello world"); |
@Richiban I'd prefer this progression: |
Ah, yes definitely! Although you'd still need |
You are likely right. I've only ever used the Edit it occurs to me that "I've only ever used the |
Reminds me of the "diamond" syntax in Java which is used when the generic type argument may be inferred: // T is inferred to be Integer as Java generic inference is based on assignment/use
List<Integer> numbers = new ArrayList<>(); However, C# already uses similar syntax to denote open generic types, e.g. MyFunc<_, object>();
MyFunc<string, _>(); |
@HaloFour Places where open generic types may be specified and places where generic types may be inferred are mutually exclusive. I really don't like reading or writing the extra underscore and space. |
Yes, but in my opinion the meaning of the syntax really shouldn't change based on where it's used. And that space is optional. 😁 |
@HaloFour So you want the meaning of syntax like |
Sure. Could help with reflection tests like |
Still needed. Nice QoL enhancement. What's blocking/controversial about it? |
Quick example which illustrates an example IRL which causes issues: `class Program
Which can be worked around by ` internal class ObjectResult
|
Interesting, everyone's is different. Here's mine: https://gist.github.com/jnm2/a0031a31d04d6ed4d44d8dfc9265b8cf |
It would also be awesome to infer generics from the initializers.
|
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Problem:
C# can infer the type from the context for types like this:
Now, when it comes to generics, say we have the following class:
When we are creating
Foo
we must satisfy the generic argument and provide the type explicitly like this:Solution:
I propose to have stronger type inference for generics calls this means you no longer need to satisfy the type argument manually because it would be inferred by the compiler:
Example 1:
Example 2:
Known Issues:
When it comes to overload resolution in cases like this:
I thought that it should pick the generic constructor over anything else but as noted by @HaloFour in my previous proposal it's a breaking change so for the sake of compatibility it would have to pick the non-generic version.
Another thing is sometimes we may need to disambiguate so in this case like @aluanhaddad I think that we should satisfy some or all of the generic type arguments to the point that it's no longer ambiguous.
Related Topics:
The text was updated successfully, but these errors were encountered: