Proposal: allow contravariant out parameters #1760
Replies: 12 comments
-
Are you suggesting this for both regular // given
void Method(out List<string> l) {
l = new List<string>();
}
// regular out parameter
IReadOnlyList<string> l1;
Method(out l1);
// out declaration
Method(out IReadOnlyList<string> l2); |
Beta Was this translation helpful? Give feedback.
-
Yes, by creating a temporary variable of the correct type
…On Wed, 1 Aug 2018, 21:16 HaloFour, ***@***.***> wrote:
Are you suggesting this for both regular out parameters and out
declarations?
// givenvoid Method(out List<string> l) {
l = new List<string>();
}
// regular out parameterIReadOnlyList<string> l1;Method(out l1);
// out declarationMethod(out IReadOnlyList<string> l2);
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://github.com/dotnet/csharplang/issues/1760#issuecomment-409707372>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/Ab0rANi4HlF5aCqXIvEP3lqlOTpOIbGZks5uMgyBgaJpZM4Vq_Ax>
.
|
Beta Was this translation helpful? Give feedback.
-
That would change the behavior of For example: public class Foo {
private IReadOnlyList<string> r;
private List<string> l;
public void M(out List<string> list) {
list = new List<string>();
throw new Exception("oops!");
}
public void Test1() {
try {
M(out l);
}
catch { }
Debug.Assert(l != null); // succeeds
}
public void Test2() {
try {
M(out r);
}
catch { }
Debug.Assert(r != null); // fails
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Not today. This proposal would seek to change that. |
Beta Was this translation helpful? Give feedback.
-
I presume the original semantics could be preserved by the compiler wrapping it in a try finally block. So given your example
|
Beta Was this translation helpful? Give feedback.
-
That's only one of the scenarios in which it's observable. Here is another: public class C {
private IReadOnlyList<string> r;
public void M(out List<string> list) {
list = new List<string>();
M2();
}
public void M2() {
Debug.Assert(list != null);
}
public void Test() {
M(out r);
}
} These may be minor points. But at the same time the amount of syntax to workaround this intentional limitation is not onerous. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
Still I think I'll leave this open in case someone comes up with a better way of achieving something similar? |
Beta Was this translation helpful? Give feedback.
-
Sure. I'm not necessarily arguing against the idea, just noting that it would behave differently. The same would be true if C# were to move to support |
Beta Was this translation helpful? Give feedback.
-
I want this outcome regardless of the method used to achieve it. To be fair, if your program depends on an out parameter being observable before the method returns, you probably have bigger issues. :D |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Given types A and B where A : B
I suggest the following code should be valid
A use of this might be passing an IReadOnlyList as an out parameter to something which accepts a List.
Beta Was this translation helpful? Give feedback.
All reactions