Proposal: Stronger type Inference for generics calls #4624
Replies: 15 comments
-
Yes, and if classes |
Beta Was this translation helpful? Give feedback.
-
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"); |
Beta Was this translation helpful? Give feedback.
-
@Richiban I'd prefer this progression: |
Beta Was this translation helpful? Give feedback.
-
Worth checking out #395, which is related. @jnm2, MyFunc() // T1 & T2 can be inferred, as current
MyFunc<object>() // T1 can be inferred, T2 = object
MyFunc<string,>() // T1 = string; T2 can be inferred
MyFunc<string, object>() // T1 and T2 both explicitly specified |
Beta Was this translation helpful? Give feedback.
-
Ah, yes definitely! Although you'd still need |
Beta Was this translation helpful? Give feedback.
-
You are likely right. I've only ever used the Edit it occurs to me that "I've only ever used the |
Beta Was this translation helpful? Give feedback.
-
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, _>(); |
Beta Was this translation helpful? Give feedback.
-
@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. |
Beta Was this translation helpful? Give feedback.
-
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. 😁 |
Beta Was this translation helpful? Give feedback.
-
@HaloFour So you want the meaning of syntax like |
Beta Was this translation helpful? Give feedback.
-
Sure. Could help with reflection tests like |
Beta Was this translation helpful? Give feedback.
-
Still needed. Nice QoL enhancement. What's blocking/controversial about it? |
Beta Was this translation helpful? Give feedback.
-
Quick example which illustrates an example IRL which causes issues: `class Program
Which can be worked around by ` internal class ObjectResult
|
Beta Was this translation helpful? Give feedback.
-
Interesting, everyone's is different. Here's mine: https://gist.github.com/jnm2/a0031a31d04d6ed4d44d8dfc9265b8cf |
Beta Was this translation helpful? Give feedback.
-
It would also be awesome to infer generics from the initializers.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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:
Beta Was this translation helpful? Give feedback.
All reactions