Replies: 3 comments
-
Take a look at my comment on this since I get around Default type Parameters. Making something like this automatic in C# would be really nice: |
Beta Was this translation helpful? Give feedback.
-
There's not a lot of details, so let me list some points to help spec this out. Correct me if anything doesn't sound right. For Default Type Parameters (DTPs): Given a type declaration
//in the end, the declaration would be the equivalent of this:
class X<[DefaultGenericArgument(typeof(C))] T> //attribute name is only for demonstration
where T : C
As far as I can tell, this is entirely syntactic sugar. Points 3 and 4 could potentially be subverted by IL rewriting, but if someone did that they would only shoot themselves in the foot and shouldn't be using a DTP in the first place. Question: What would be the interaction of a DTP relying on other type parameters? I have the following type (which is pretty convoluted, but it works the way it should): //not the actual type names
public class Foo<TBar, TBaz, TQux>
where TBar : Bar<TBaz, TQux>
where TBaz : Baz<TQux> //this one is abstract
where TQux : Qux Could DTPs be used here without needing to re-order the type parameters? public class Foo<TBar = Bar<TBaz, TQux>, TBaz, TQux = Qux>
where TBaz : Baz<TQux> Or would |
Beta Was this translation helpful? Give feedback.
-
For DTP keyword Given a type declaration
interface IX<[MustBeImplementingType] T> //again, attribute name is only demonstration
where T : IX<T>
As noted in the OP, point 5 cannot be enforced at runtime. Older compilers would also be able to put in any type as long as additional constraints match (if any), whereas with a DTP they would only require you to specify the appropriate type parameters explicitly and is not a behavioral change. The lowered constraint can alleviate some of this, but still wouldn't prevent the following: public class A : IX<A>
{
}
//by using an older compiler or IL rewriting, this is still legal:
public class B : IX<A>
{
} Question: Should it be a warning/error if more than one type parameter is marked with Overall, I'm personally not too thrilled about |
Beta Was this translation helpful? Give feedback.
-
default type parameters
Proposal: dotnet/roslyn#6248
Since types with identical names can be defined with different arities, it might be easier to require the diamond syntax (or missing type arguments) to disambiguate type arities. But not requiring it would provide source-compat such that you can add a type parameter with a default value, without changing use sites.
One possible restriction to simplify binding rules is to disallow identical type names where there exists a type with default type parameters.
However, even with this restriction there can be ambiguous type names, in those cases we prefer the non-generic type.
If we want to select the default parameter we can use
X<>
syntax or named type arguments.We can also disallow leading default type parameters just like method default parameters,
this
as default type parameterIn some cases it's useful to define a default type parameter of the inheriting type (if any).
It is also useful with shapes when we want the "self" type that is implementing the shape,
It'd be nice to also constrain such types to the inheriting type, in case that it's explicitly specified. (#169)
But this would be a C#-only restriction as CLR does not support such constraints. Note that this constraint only applies to the derived types so it might be preferable to restrict it to interfaces and abstract classes.
PS: One approach to resolve types is to do something like "overload resolution" on type arguments on top of #280, that would also cover some of #281 examples.
Beta Was this translation helpful? Give feedback.
All reactions