[Proposal] Simplify generic type parameters declaration #1549
Replies: 8 comments
-
IMO this doesn't make the declaration of the generic types any simpler. At consumption this becomes ambiguous as classes are different based on their generic arity and your second example would be indistinguishable from |
Beta Was this translation helpful? Give feedback.
-
You may consider this as syntatic-sugar compiler magic which just expands
into its "real" form:
The declaration would become more straghtforward, less repetitive, always twice as shorter in typing, and much better for visual perception and easier for understanding |
Beta Was this translation helpful? Give feedback.
-
That's my point. This looks like I'd rather have better generic type inference which would make it possible to omit the generic type arguments entirely.
I don't see much of an appreciable difference between the two: public class Class2<T<U<V>>>
where T : IInterface2B<U<V>>
where U : IInterface2A<V>
where V : unmanaged { }
public class Class1<T, U, V>
where T : unmanaged
where U : IInterface1A<T>
where V : IInterface1B<T, U> { } |
Beta Was this translation helpful? Give feedback.
-
In case of declaration - sure yes (seems like I was a little misleading with the issue title - my bad)!
|
Beta Was this translation helpful? Give feedback.
-
Oh! I am stupid! I just realized what you meant with:
What about just raise roslyn (code analyzer) ambiguity error in this case and let a dev to fix it choosing what he actually need? |
Beta Was this translation helpful? Give feedback.
-
Why would that be a compiler error? It's perfectly legal to have You also might want to take a look at #339 as I think there may be some conceptual overlap. |
Beta Was this translation helpful? Give feedback.
-
I mean trigger error in case we have declarations which may cause potential ambiguity:
or
so when we will try to instaniate them:
compiler whould raise error about ambigious usage (since it's unable to determine which class to use). PS: as of #339 - it seems like a different story to me |
Beta Was this translation helpful? Give feedback.
-
I think this is kind of what I was searching for, just a little bit different. What I would like to archive is the same (I think) but I would do it the other way around. public class MyList : List<int> { }
// Today:
public static void Foo<TList, TItem>(TList list)
where TList : List<TItem>
where TItem : struct {}
public static void Bar<TList, TItem>()
where TList : List<TItem> => typeof(TItem);
Foo<MyList, int>(new MyList());
Bar<MyList, int>();
// Future (I wish)
public static void Foo<TList, {TItem}>(TList list)
where TList : List<TItem>
where TItem : struct {}
public static Type Bar<TList, {TItem}>()
where TList : List<TItem> => typeof(TItem);
Foo(new MyList());
Bar<MyList>(); // returns the int type The curly braces around the generic type argument acutally mean those are not existent. So I'm not sure if this would provide the same benefit @hypeartist wanted. But if it would, it should not have the problem with the ambiguity. On the other hand it is probably more then syntactic sugar. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The proposal is to make it possible to declare generic type parameters more straightforward.
This is existing implementation:
Declaring some types and interfaces:
And use them:
And this how it would be according to the proposal:
Declaring some types and interfaces:
And use them:
Beta Was this translation helpful? Give feedback.
All reactions