C# Language Proposal for 'out' Parameters

  • Thread starter Thread starter C# Learner
  • Start date Start date
C

C# Learner

Note
----

Please use a fixed-width font to view this, such as Courier New.





Problem
-------

When passing a parameter with the modifier 'out', it is necessary to
write two statements:

- one for the declaration of the variable due to receive the value; and
- one for the method call, where the variable is passed as a parameter.

Example:

static void Main()
{
int i;
if (TryGetValue(out i)) {
Console.WriteLine("Value is: {0}.", i);
}
}

This is inconsistent with the traditional way of returning values from a
method.

Example:

static void Main()
{
int i = GetValue();
Console.WriteLine("Value is: {0}.", i);
}





Solution
--------

The solution to this problem would be to allow the declaration of the
variable to appear within the statment where it's being passed as an
'out' parameter.

Example:

static void Main()
{
if (TryGetValue(out int i)) {
Console.WriteLine("Value is: {0}.", i);
}
}





Benefits
--------

(a) Method bodies can benefit from one less statement for each time this
technique is used, promoting readability.

(b) C# becomes more consistent since returning values using both the
traditional way, and using 'out' parameters, is uniform. Again,
this promotes readability.

Example:

// traditional way
static void Main()
{
int i = GetANumber();
DoSomethingWith(i);
}

// using an 'out' parameter, with the aforementioned syntax
static void Main()
{
if (TryGetANumber(out int i)) {
DoSomethingWith(i);
}
}





End
---

Comments?

Regards,
C. S. Learner
 
I have a better proposal for ref and out:

instead of using "pass by reference" semantics, why not use
"copyin/copyout" semantics for "ref" and "copyout"semantics for "out".

This way, you would not need to declare a variable every time. In lots of
cases, you would be able to receive the value directly into an object
property. For example:

if (TryGetValue(myObj.MyProp))
{
// doSomething
}

This would be equivalent (almost) to the following that you have to write
today:

int i;
if (TryGetValue(out i))
{
myObj.MyProp = i;
}

For a "ref" (in/out) parameter, the gain is even more obvious. It would save
both the assignment from the property to a temp variable before the call,
and the reverse assignment after the call.

Also, I understand why "ref" and "out" are needed in the method declaration,
but it seems superfluous to have to specify them at every call.

The last refinement would be to have normal parameters (no keyword, "in"
semantics) be "readonly" inside the method body (you can do this in Java by
marking them with "final"). Of course this is probably too far fetched as it
goes against the C/C++/Java tradition, but IMO, this is so much cleaner and
would make the language easier to understand to beginners (for once,
mimicking Pascal or ADA rather than C++ would help).

Just some ideas...

Bruno.
 
C# Learner said:
[proposed new syntax]
if (TryGetValue(out int i))

I'd appreciate that. It seems no more problematic than being able to declare
a loop counter variable within a for statement.

P.
 
Also, I understand why "ref" and "out" are needed in the method
declaration,
but it seems superfluous to have to specify them at every call.
i think it makes the code more understandable, for example while debugging
you wont have to goto to see the method declaration to find out if the
parameter was a ref or out, instead you'll know right there and then from
the call what the methods accepts.
 
Bruno Jouhier said:
[...]
I understand why "ref" and "out" are needed in the
method declaration, but it seems superfluous to have
to specify them at every call.

I think it's good to be able to see which parameters might be changed
without having to examine the function being called.

P.
 
Bruno Jouhier said:
Also, I understand why "ref" and "out" are needed in the method declaration,
but it seems superfluous to have to specify them at every call.

Aargh no! It's absolutely *vital* to my mind that you should specify
them at every call - otherwise it's far from obvious, unless you look
at the declaration of what you're calling, that the parameter is being
passed by reference. I for one don't want to have to check the
declaration of every method which is called just in order to know
what's going on.

Ref/out are (or should be, IMO) rarely enough used that it's not like
it's going to take that many more keystrokes anyway, and the difference
in readability is huge.
 
Jon Skeet said:
Aargh no! It's absolutely *vital* to my mind that you should specify
them at every call - otherwise it's far from obvious, unless you look
at the declaration of what you're calling, that the parameter is being
passed by reference. I for one don't want to have to check the
declaration of every method which is called just in order to know
what's going on.

Ref/out are (or should be, IMO) rarely enough used that it's not like
it's going to take that many more keystrokes anyway, and the difference
in readability is huge.

I second that.
 
(a) Method bodies can benefit from one less statement for each time this
technique is used, promoting readability.

(b) C# becomes more consistent since returning values using both the
traditional way, and using 'out' parameters, is uniform. Again,
this promotes readability.

Example:

// traditional way
static void Main()
{
int i = GetANumber();
DoSomethingWith(i);
}

// using an 'out' parameter, with the aforementioned syntax
static void Main()
{
if (TryGetANumber(out int i)) {
DoSomethingWith(i);
}
}





End
The primary problem I have with this is in all othercases I can think of, a
variable declared within a () is scoped to the underlying block(method
parameters, for (int x) { //x is only valid here }, using (IDisposable x)
{ //x is only valid here }. By allowing declarations within a method call,
you remove that partciular consistence of the language.
 
Daniel said:
The primary problem I have with this is in all othercases I can think of, a
variable declared within a () is scoped to the underlying block(method
parameters, for (int x) { //x is only valid here }, using (IDisposable x)
{ //x is only valid here }. By allowing declarations within a method call,
you remove that partciular consistence of the language.

Hmm, good point. Back to the drawing board then.
 
C# Learner said:
Hmm, good point. Back to the drawing board then.

You'll find yourself doing that *alot* when trying to get syntax down. The
first forty ideas or so rarely seem to work, ;)
 
Ok. Maybe the few additional keystrokes are worth it. At least many people
seem to react strongly here.

If I remember well, languages like Pascal and ADA support in/out parameters
and you only need to specify this in the function/procedure declaration, you
don't repeat it every time you call. Also, seems to me that the method name
or the parameter name should give a good hint about the in/out nature of the
parameter. This is why I suggested that they are not that useful on the
caller side.

Also, I still do most of my developments in Java (or rather J#). So, I still
have to fight with the lack of goodies like in/out parameters :-(. So you
guys probably know better.

But I think that the debate around "by reference" semantics vs.
"copyin/copyout" semantics is interesting, and I don't really understand why
the C# (or rather .NET) designers did not choose copyin/copyout. In general
they made very good language design choices, but here, I think that they
made a rather poor choice.

Bruno.
 
Bruno Jouhier said:
Ok. Maybe the few additional keystrokes are worth it. At least many people
seem to react strongly here.

If I remember well, languages like Pascal and ADA support in/out parameters
and you only need to specify this in the function/procedure declaration, you
don't repeat it every time you call.

And C# has learned from the mistakes of the past :)
Also, seems to me that the method name
or the parameter name should give a good hint about the in/out nature of the
parameter. This is why I suggested that they are not that useful on the
caller side.

I don't believe it's always obvious, to be honest.
Also, I still do most of my developments in Java (or rather J#). So, I still
have to fight with the lack of goodies like in/out parameters :-(. So you
guys probably know better.

Personally I don't really regard ref and out parameters as "goodies".
They're necessary evils for interop, but I try to keep them out of my
code wherever possible. Then again, I have a Java background as well.
But I think that the debate around "by reference" semantics vs.
"copyin/copyout" semantics is interesting, and I don't really understand why
the C# (or rather .NET) designers did not choose copyin/copyout. In general
they made very good language design choices, but here, I think that they
made a rather poor choice.

I'm not sure I particularly mind that much - what are the benefits of
copyin/copyout, here?
 
But I think that the debate around "by reference" semantics vs.
I'm not sure I particularly mind that much - what are the benefits of
copyin/copyout, here?

The main benefit of copyin/copyout is that you can pass any valid
assignement LHS as in/out argument. In particular, you can pass an object
property and indexed expression, etc. For example, instead of having to
write:

int val = obj.Prop;
if (GetNewValue(ref val)) {
obj.Prop = val;
// more stuff
}

You just need to write:

if (GetNewValue(inout obj.Prop)) {
// more stuff
}

This is more concise, easier to read, and just as efficient.

Bruno.
 
Bruno Jouhier said:
The main benefit of copyin/copyout is that you can pass any valid
assignement LHS as in/out argument. In particular, you can pass an object
property and indexed expression, etc. For example, instead of having to
write:

int val = obj.Prop;
if (GetNewValue(ref val)) {
obj.Prop = val;
// more stuff
}

You just need to write:

if (GetNewValue(inout obj.Prop)) {
// more stuff
}

This is more concise, easier to read, and just as efficient.

Though, one may argue it basically provides the ability to do something that
you would probably never be able to justify doing. Passing a property(or a
field, even though its allowed) to a ref parameter is a very bad idea, IMHO.
If a non-essential method with a ref fails, the value of that ref is
undefined as far as I'm concerned. Allowing that to be extended to
properties is horrible. The syntactical changes that would be needed to
allow you to escape a method without performing the copy out is unpleasent.
Not providing any syntax in effect allows a failed method call to corrupt
the object's state and would make the exception\error handling code and the
code with the ref\out parameter have the nasty stuff instead of the standard
path. You can't just forget about the value of a property like you can with
a local, you have to take the time and back up the value if its at all
possible the method could fail.
You potentially end up with:
int val = obj.Prop
if (GetNewVal(inout obj.Prop))
{
//more stuff
}
else
{
obj.Prop = val;
}

or
int val = obj.Prop
try
{
GetNewVal(inout obj.Prop);
//more stuff
}
catch (WhateverException)
{
obj.Prop = val;
}
catch (WhateverOtherException)
{
obj.Prop = val;
}
catch (AnotherExceptionException)
{
//Again!
//makes me wish C# had fault handlers, I'd not have to write this so
much
obj.Prop = val;
}
finally
{
//do whatever
}

why is that better?
 
You got it wrong. There is no issue about corrupted state or anything like
that. Maybe you are confusing "copyin/copyout" with "passing by name".

The semantics of copyin/copyout are the following:

With a method declaration like:
MyMethod(inout MyType param)

A call like
MyMethod(inout expression);

is equivalent to:
MyType tmpVar = expression;
MyMethod(ref tmpVar);
expression = tmpVar;

This is actually cleaner than passing by reference because the inout
parameters behave exactly like return values (a ref parameter does not!). If
an exception is thrown by the method, the "copyout" assignment is **not**
performed and the expression is not assigned at all. So, I don't see where
there is a risk of corrupting the object state here. On the other hand, all
this seems very safe.

Passing "by name" is another story, and I would not advocate for it at all,
at least as a general passing mechanism. It has horrible side effects.

Bruno.
 
Bruno Jouhier said:
The main benefit of copyin/copyout is that you can pass any valid
assignement LHS as in/out argument. In particular, you can pass an object
property and indexed expression, etc. For example, instead of having to
write:

int val = obj.Prop;
if (GetNewValue(ref val)) {
obj.Prop = val;
// more stuff
}

You just need to write:

if (GetNewValue(inout obj.Prop)) {
// more stuff
}

This is more concise, easier to read, and just as efficient.

Right. That makes sense - and presumably you could also have just
output parameters where the property isn't read to start with, it's
only written out at the end?

Interestingly enough, VB.NET allows you to specify properties as ByRef
parameters, which does exactly the above. Now, I hate the fact that it
does it silently, but if a parameter could actually be *declared* that
way, it would make an awful lot of sense.
 
Right. That makes sense - and presumably you could also have just
output parameters where the property isn't read to start with, it's
only written out at the end?

Yes, of course, it also works for "out", this is simply "copyout" argument
passing.

BTW, copyout is what comes closest to multiple returns (the assignment is
done after the method returns), and, as my response to Daniel explains,
there is no special problem if the method throws an exception, the copyout
assignment is not performed, the same way the "normal" return value is not
assigned.

Bruno.
 
Bruno Jouhier said:
You got it wrong. There is no issue about corrupted state or anything like
that. Maybe you are confusing "copyin/copyout" with "passing by name".

The semantics of copyin/copyout are the following:

With a method declaration like:
MyMethod(inout MyType param)

A call like
MyMethod(inout expression);

is equivalent to:
MyType tmpVar = expression;
MyMethod(ref tmpVar);
expression = tmpVar;

This is actually cleaner than passing by reference because the inout
parameters behave exactly like return values (a ref parameter does not!).
If
an exception is thrown by the method, the "copyout" assignment is **not**
performed and the expression is not assigned at all. So, I don't see where
there is a risk of corrupting the object state here. On the other hand,
all
this seems very safe.

I hadn't considered that the code may be generated *around* the method, but
in, which helps mitigate much of the problem(not entirely sure why I was
thinking that way...3am responses aren't always well thought out, ;). The
problem still stands that you would have to throw an exception to stop that
copyout from happening. Your example itself uses an if block to determine
success, how do you escape without throwing an exception?
Passing "by name" is another story, and I would not advocate for it at
all,
at least as a general passing mechanism. It has horrible side effects.

Bruno.

"Daniel O'Connell [C# MVP]" <[email protected]> a écrit dans
le
message de news:[email protected]...
Though, one may argue it basically provides the ability to do something that
you would probably never be able to justify doing. Passing a property(or
a
field, even though its allowed) to a ref parameter is a very bad idea, IMHO.
If a non-essential method with a ref fails, the value of that ref is
undefined as far as I'm concerned. Allowing that to be extended to
properties is horrible. The syntactical changes that would be needed to
allow you to escape a method without performing the copy out is unpleasent.
Not providing any syntax in effect allows a failed method call to corrupt
the object's state and would make the exception\error handling code and the
code with the ref\out parameter have the nasty stuff instead of the standard
path. You can't just forget about the value of a property like you can with
a local, you have to take the time and back up the value if its at all
possible the method could fail.
You potentially end up with:
int val = obj.Prop
if (GetNewVal(inout obj.Prop))
{
//more stuff
}
else
{
obj.Prop = val;
}

or
int val = obj.Prop
try
{
GetNewVal(inout obj.Prop);
//more stuff
}
catch (WhateverException)
{
obj.Prop = val;
}
catch (WhateverOtherException)
{
obj.Prop = val;
}
catch (AnotherExceptionException)
{
//Again!
//makes me wish C# had fault handlers, I'd not have to write this so
much
obj.Prop = val;
}
finally
{
//do whatever
}

why is that better?
 
See inline..
Bruno said:
Ok. Maybe the few additional keystrokes are worth it. At least many people
seem to react strongly here.

If I remember well, languages like Pascal and ADA support in/out parameters
and you only need to specify this in the function/procedure declaration, you
don't repeat it every time you call. Also, seems to me that the method name
or the parameter name should give a good hint about the in/out nature of the
parameter. This is why I suggested that they are not that useful on the
caller side.

I would disagree here. IMHO, the ref and out in the calling
function, helps catch wrong usage of functions at compile time. In large
code bases it would potentially save someone a huge amount of debugging
time
Also, I still do most of my developments in Java (or rather J#). So, I still
have to fight with the lack of goodies like in/out parameters :-(. So you
guys probably know better.

But I think that the debate around "by reference" semantics vs.
"copyin/copyout" semantics is interesting, and I don't really understand why
the C# (or rather .NET) designers did not choose copyin/copyout. In general
they made very good language design choices, but here, I think that they
made a rather poor choice.

Bruno.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top