-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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: Inline type parameter constraints #7671
Comments
Is this a no go? |
Yes, no one commented in an hour, so probably a bad idea :D |
@leppie lol, that was because I am myself a little uncertain about this. I just wanted to know if it is indeed a bad idea, then why. I know that every decision in language design had been carefully taken, so in C# 2.0 this probably had been considered and for some reason eventually dropped. on the other hand, this is like anonymous method / lambda expression all over again, changes of requirements cause to need for more concise syntaxes. My first motivation for this, is for nullable types, that said, it is more common to use |
@alrz Was just kidding :) But it will probably make parsing a lot harder. If I understand correctly it would allow:
|
@leppie I think you meant |
@alrz |
@leppie type-parameter is just an identifier, constraints are not part of that. (I was mentioning type-parameters actually, with a |
It's not that it's a bad idea, it's that it doesn't offer anything really new or novel. The existing syntax isn't appreciably more verbose and while it is slightly removed from the type parameter declaration itself it remains a part of the type or member declaration. |
@HaloFour Take this example, void F<struct T>(T? arg) {}
// versus
void F<T>(T? arg) where T : struct {} When I'm declaring this method, I know that |
@alrz I don't disagree, I just don't think that said benefit warrants a second form of the syntax. |
One more thing, with nullable reference types, it actually affects the method signature,
The first one accepts a |
That would be true regardless of this syntax. void F<T>(T? arg) where T : struct { }
void F<T>(T? arg) where T : class { } |
@alrz They aren't a part of the method signature, that proposal deals strictly with overload resolution. You still won't be able to define the following: void F<T>(T arg1) where T : class { }
void F<T>(T arg1) where T : struct { } And even if they were that doesn't change any of the rest of the argument which is that a syntax already exists which is not appreciably more verbose, is not removed from the declaration and is more capable of describing the breadth of functionality offered by constraints. |
I was specifically talking about situations when nullables are involved, but as you said, due to the restrictions of inline constraints mixing these two syntaxes doesn't seem like a good idea, and ultimately it doesn't prevent duplication of type parameters. |
@HaloFour Nevertheless, Rust does allow both syntaxes. and uses |
@alrz Is "Rust does it" worth 200 points? I still don't see what there is to gain by having two syntaxes where neither provides particular functionality, clarity or verbosity benefits over the other. |
@HaloFour Even in the aforementioned language |
Not sure if this is a matter of habit, but I find the suggested syntax harder to read. |
Also (feel free to dismiss as irrelevant mental experiment): --Something I understand, maybe because of habit.
SELECT Name, Value
FROM Sample
WHERE Name = 'Abc' AND Value > 50 vs. --More concise version I just came up with.
SELECT Name(='Abc'), Value (>50)
FROM Sample |
@dsaf "sometimes there is just too much stuff going on one line." I think features like expression-bodied methods/lambdas,
And it doesn't even look like C# anymore! I emphasized, in simple cases, I would prefer inline constraints. |
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. You seem to have moved this to dotnet/csharplang#279 |
I think in simple cases, inline constraints are far consite than
where
clauses, which cause to duplicate every type parameter.The text was updated successfully, but these errors were encountered: