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

Allow ctor's with optional parameters to be used with generic type arguments that contain : new() qualifier #4505

Closed
kensykora opened this issue Aug 12, 2015 · 3 comments
Labels
0 - Backlog Area-Language Design Need More Info The issue needs more information to proceed.

Comments

@kensykora
Copy link

When trying to create a type argument that requires an object to have a public parameterless constructor, the compiler should allow types that, if no public parameterless constructor is defined but contain a single constructor that has all optional parameters, allows this type to be used with the constructor with all defaults used.

Example:

using System;

public class Program
{
    public static void Main()
    {
        Foo<Bar> foobar = new Foo<Bar>(); //Compilation error (line 7, col 7): 'Bar' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TSomeType' in the generic type or method 'Foo<TSomeType>'     
    }
}

public class Foo<TSomeType>
    where TSomeType : new ()
{
}

public class Bar
{
    public Bar(string optional1 = "", int optional2 = 0)
    {
    }
}

//Workaround
/*
public class Bar
{
    public Bar(): this (null)
    {
    }

    public Bar(string optional1 = null, int optional2 = 0)
    {
    }
}
*/
@HaloFour
Copy link

This is a limitation of the CLR. First, the CLR only recognizes the constructor constraint as having no parameters. While the parameters of the type are decorated to be optional they do exist in the signature which makes that constructor not parameterless. Attempting to set Bar as the generic type argument, even sidestepping the compiler via reflection, would cause a TypeLoadException.

Second, optional parameters aren't really optional, they just declare metadata that they have default values and supporting compilers can read those default values from the metadata in order to construct the actual argument values during compile time. The emitted IL to call the constructor actually does push "" and 0 onto the stack before calling Bar(string,int). Generic methods require that the IL always be exactly the same regardless of the generic type arguments, but since in this case the generic method would be required to emit additional opcodes to push the default arguments that couldn't be the case.

@gafter
Copy link
Member

gafter commented Aug 16, 2015

@kensykora How do you suggest this be mapped to the CLR?

@gafter gafter added the Need More Info The issue needs more information to proceed. label Oct 21, 2015
@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

See also #18002 for a general discussion of closing and/or moving language issues out of this repo.

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0 - Backlog Area-Language Design Need More Info The issue needs more information to proceed.
Projects
None yet
Development

No branches or pull requests

4 participants