-
Notifications
You must be signed in to change notification settings - Fork 4k
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] "Default values" for out parameters #1757
Comments
Can I call this method like that: var smth = DoSomething("key"); ? Is this side effect? |
@ViIvanov this is declaration-site feature, just to satisfy definite assignment rules. "Default" value has nothing to do with invocations, it's should not be stored in metadata with |
I don't understand, why does the current syntax look silly? Also, I think that using the same syntax as optional parameters without actually making the parameters optional would be confusing. |
Agree with @svick. The risk of confusion with this syntax is pretty great, as seen by @ViIvanov's question. I don't think syntax making it easier to add new methods that use out parameters should really be a priority, as the proposed tuples feature will be much nicer for the caller to interact with and serve the same purpose (returning multiple values). |
@gafter I think "caller-receiver parameters" from record spec draft can be applied here for a straightforward public void Deconstruct(out string name = this.Name, out int age = this.Age) {} However, as currently specified, this needs the method to be not static. So you could not use this in an extension Deconstruct method. Fortunately, with "extention everything" it wouldn't be much of an issue. extension class PersonExtensions : Person
{
public void Deconstruct(out string name = this.Name, out int age = this.Age) {}
} This could also be used in record expansion as it is used for |
Moving language design discussion over to csharplang. Thanks |
Issue moved to dotnet/csharplang #1033 via ZenHub |
In methods with
out
-parameters it is very common to see first statements like:While definite assignment rules are definitely good thing to have in C#, statements like this (to satisfy definite assignment rules) looks pretty silly in methods with "early" returns. It may be convenient to allow out parameter "initialization" with some constant default value in-place, using optional parameter syntax:
The text was updated successfully, but these errors were encountered: