Skip to content
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

Closed
controlflow opened this issue Apr 2, 2015 · 7 comments
Closed

[Proposal] "Default values" for out parameters #1757

controlflow opened this issue Apr 2, 2015 · 7 comments

Comments

@controlflow
Copy link

In methods with out-parameters it is very common to see first statements like:

string DoSomething(string key, out string value, out int id)
{
  value = null;
  id = 0;

  var a = F(key);
  if (a == null) return null;

  var b = G(a);
  if (b == null) return null;

  value = H(b, out id);
  return J(b);
}

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:

string DoSomething(string key, out string value = null, out int id = 0)
{
  var a = F(key);
  if (a == null) return null; // ok, 'value' and 'id' are assigned

  var b = G(a);
  if (b == null) return null;

  value = H(b, out id);
  return J(b);
}
@controlflow controlflow changed the title [Proposal] Default values for out parameters [Proposal] "Default values" for out parameters Apr 2, 2015
@ViIvanov
Copy link

ViIvanov commented Apr 2, 2015

Can I call this method like that:

var smth = DoSomething("key");

? Is this side effect?

@controlflow
Copy link
Author

@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 [DefaultValue] attribute.

@svick
Copy link
Contributor

svick commented Apr 2, 2015

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.

@MgSam
Copy link

MgSam commented Apr 2, 2015

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 gafter added this to the When Heck Freezes Over milestone Apr 3, 2015
@gafter gafter removed this from the When Heck Freezes Over milestone Apr 5, 2015
@alrz
Copy link
Contributor

alrz commented Dec 22, 2016

@gafter I think "caller-receiver parameters" from record spec draft can be applied here for a straightforward Deconstruct implementation.

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 With method.

@jcouv
Copy link
Member

jcouv commented Oct 22, 2017

Moving language design discussion over to csharplang. Thanks

@jcouv
Copy link
Member

jcouv commented Oct 22, 2017

Issue moved to dotnet/csharplang #1033 via ZenHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants