Skip to content
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 #11359

Closed
iam3yal opened this issue May 17, 2016 · 4 comments
Closed

Proposal: Stronger type Inference for generics calls #11359

iam3yal opened this issue May 17, 2016 · 4 comments
Labels
Area-Language Design Feature Request Resolution-Duplicate The described behavior is tracked in another issue

Comments

@iam3yal
Copy link

iam3yal commented May 17, 2016

So today, C# can infer the type from the context for types like this:

var x = 1; // int
var y = "one"; // string
var z = 1f; // float

Now, when it comes to generics, say we have the following class:

class Foo<T>
{
    public Foo(T value)
    {
    }
}

When we are creating Foo we must satisfy the generic argument and provide the type explicitly like this:

var x = new Foo<int>(1);
var y = new Foo<string>("one");
var z = new Foo<float>(1);

Now, in C# 7 there's a new feature to allow C# to infer the type from the argument or whatever context it might be when the type is known instead of specifying the the actual type explicitly like this (which is just beautiful imo):

Foo(new (1, "one"));

So what I'm proposing is the ability to have stronger type inference for generics calls this means you no longer need to satisfy the type argument because it would be inferred by the compiler so in the future we might have the ability to write the following code instead of the above:

var x = new Foo(1); // int
var y = new Foo("one"); // string
var z = new Foo(1f); // float

With my proposal the following will also be allowed so this is an addition to the C# 7.0 feature even though the C# 7.0 feature is more succinct:

Foo(new Bar(1, "one")); // Bar<T, U>

When it comes to overload resolution in cases like this:

class Foo<T>
{
    public Foo(T value)
    {
    }

    public Foo(object value)
    {
    }

    public Foo(int value)
    {
    }
}

I don't know what's the best approach to go with here but I'd assume it should pick the generic constructor over anything else.

The aim here is to make the code more readable and less explicit when it makes sense, in my opinion whether something is generic or not shouldn't concern the person that reads the code.

It's not like we handle things differently if something is generic, we just write it differently and I guess we can make it look like a normal code without all the verbosity and ceremony that generics brings, at least where we make the calls.

@HaloFour
Copy link

Dupe of #2319. You do have the following ambiguity issue:

public class Foo {
    public Foo(int value) { }
}

public class Foo<T> {
    public Foo(T value) { }
}

var foo = new Foo(123);  // Foo(int) or Foo<int>(int)  ?

For the sake of compatibility the result would have to be a Foo and not a Foo<int>, but that might not be what the developer expects.

@iam3yal
Copy link
Author

iam3yal commented May 17, 2016

@HaloFour Okay, what do you think about this then?

var foo = new Foo<>(123);

We can use this syntax instead when there's ambiguity? or just use it everywhere.

@iam3yal
Copy link
Author

iam3yal commented May 17, 2016

@HaloFour Okay, I see that @aluanhaddad already proposed something similar to disambiguate. :)

@aluanhaddad
Copy link

@HaloFour I actually think the developer would expect the non generic class to be instantiated because it is more specific.

@gafter gafter added Resolution-Duplicate The described behavior is tracked in another issue Feature Request Area-Language Design labels May 18, 2016
@gafter gafter closed this as completed May 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Feature Request Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

4 participants