dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.02k stars 4.03k forks source link

New features proposal for generics and other #24366

Closed DenisKudelin closed 6 years ago

DenisKudelin commented 6 years ago
  1. Casting to a generic class/interface/struct without specifying generic types + ability to call its non-generic methods without using interfaces:
    
    class Foo<T, U>
    {
    public bool Test() => true;
    public T GenericFoo() => default(T);
    public void GenericFoo(T value) { }
    }

object obj = new Foo(); ((Foo<,>)obj).Test(); //GenericFoo methods of class Foo<,> should not be available in this case. Foo<,> foo = (Foo<,>)obj; ((Foo<int,byte>)foo).GenericFoo()


2. Support of Delegate and Enum generic constraints:

`class Foo<T> where T: Delegate`

3. Support of generics with generic parameter, at least one level:

`class Foo<T<U>>`
But I very much suspect that it's not possible or too hard to implement.

4. Support of 'var' for field declarations:

`public static var Foo = 0;
`

5. Try/Catch/Finally without explicit block declaration:

try { Foo(); return true; } catch return false;


6. Rethrow operator for saving the original stacktrace:

try {

} catch(Exception ex) { rethrow ex; // bad example, but it's the same as just "throw;" in this case. }


7. memberof operator for accessing Type members metadata without using reflection:

class Foo { public int Bar => this.bar; private int bar; public int this[string key] => 0;

public MemberInfo[] GetMembers<T>(bool someValue)
{
    MethodInfo mi = memberof(this.ToString); // no need to cast
    return new []
    { 
         memberof(this.Bar), 
         memberof(this.bar),
         memberof(this.GetMembers<T>(bool)), // specifying parameter types in case of overloading.
         memberof(string.GetEnumerator), // It works like nameof operator
         memberof(this[string]) // even indexers..
    };
}

}


Also implementation must not use the search by function name using strings or something like, because it won't work after code obfuscation.
HaloFour commented 6 years ago

C# language change requests are taken at the http://github.com/dotnet/csharplang/ repository now. You'd want to make separate proposals rather than one mega-proposal. Many of these requests have already been made in one form or another, some championed and some already explained why they're not practical.

jcouv commented 6 years ago

@DenisKudelin I'll go ahead and close the issue and let you re-open on csharplang. Thanks