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

Declaration of ref/out parameters in lambdas without typename #303

Closed
ViIvanov opened this issue Feb 8, 2015 · 13 comments
Closed

Declaration of ref/out parameters in lambdas without typename #303

ViIvanov opened this issue Feb 8, 2015 · 13 comments

Comments

@ViIvanov
Copy link

ViIvanov commented Feb 8, 2015

Hello!

I have a little suggestion for C# language.
Let us have delegate like this:

delegate T Parse<T>(string text);

and we want to create an instance:

Parse<int> parse = text => Int32.Parse(text);

All is OK. What about ref/out parameters in a delegate?

delegate bool TryParse<T>(string text, out T result);

when we want to create an instance…

TryParse<int> parse1 = (string text, out int result) => Int32.TryParse(text, out result);

…we shoud to specify types on parameters.

Why is this required? What about a syntax below:

TryParse<int> parse2 = (text, out result) => Int32.TryParse(text, out result);

?

@mikedn
Copy link

mikedn commented Feb 8, 2015

In this particular case you don't need to write any of the parameter stuff, it's just:

TryParse<int> parse1 = Int32.TryParse;

@alanfo
Copy link

alanfo commented Feb 8, 2015

This would be a worthwhile improvement to type inference, in my view.

As ref and out are full keywords, they can't possibly be type names and so there appears no reason why the compiler won't be able to infer the type from the delegate signature.

@paulomorgado
Copy link

I'm not sure I understand what you are proposing.

Would you care to elaborate a bit more?

@ViIvanov
Copy link
Author

ViIvanov commented Feb 9, 2015

@paulomorgado Of course! Let us see a code below:

delegate T Parse<T>(string text);
delegate bool TryParse<T>(string text, out T result);

static void Main() {
  // We can create an instance of Parse<int> like this:
  Parse<int> parseHex1 = (string text) => Int32.Parse(text, NumberStyles.HexNumber);
  // or like this (and, I think, this way is more simple and more readable):
  Parse<int> parseHex2 = text => Int32.Parse(text, NumberStyles.HexNumber);

  // To create an instance of TryParse<int> delegate
  // we must explicitly specify a types of arguments in a lambda expression:
  TryParse<int> tryParseHex1 = (string text, out int result) => Int32.TryParse(text, NumberStyles.HexNumber, null, out result);

  // And we can not now use a sintax like this:
  TryParse<int> tryParseHex2 = (text, out result) => Int32.TryParse(text, NumberStyles.HexNumber, null, out result);
}

Why this is meaningful:

  1. In some scenarios user can have a delegates with a few (three, four, …etc) parameters and when at least one of them has a ref or out modifier user must explicitly specify types of all "delegate parameters".
  2. Follows from previous - we can not use anonymous types as type-parameters in delegates with ref or out parameters.

@omariom
Copy link

omariom commented Feb 9, 2015

👍

@paulomorgado
Copy link

I ink you are missing the fact that, although you can't declare by reference type parameters in C#, when a parameter is expressed as of being of type ref T (or out T, which is the same for the CLR - just extra validation from the compiler), the type is, actually, &T, which is not the same as T.

@ViIvanov
Copy link
Author

ViIvanov commented Feb 9, 2015

@paulomorgado Excuse me, can you explain what do you mean? Why in you point of view

TryParse<int> tryParseHex1 = (string text, out int result) => Int32.TryParse(text, NumberStyles.HexNumber, null, out result);

is correct (it is valid C# code) and

TryParse<int> tryParseHex2 = (text, out result) => Int32.TryParse(text, NumberStyles.HexNumber, null, out result);

is not? In a both examples, types of arguments exactly the same. But, in the second line, it calculated by the compiler, not specified by user explicitly.

@paulomorgado
Copy link

I was trying to understand your issue, and I think I got it: the compiler should be able to infer the types from usage when there are out parameters. Is that it?

@ViIvanov
Copy link
Author

ViIvanov commented Feb 9, 2015

@paulomorgado Exactly! I'm sorry for my bad and poor English.

@alrz
Copy link
Contributor

alrz commented Nov 19, 2015

👍

@Thaina
Copy link

Thaina commented Jan 15, 2016

+1

thanks

@asvishnyakov
Copy link
Contributor

👍

@gafter
Copy link
Member

gafter commented Mar 24, 2017

Issue moved to dotnet/csharplang #338 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