-
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: out patterns #11293
Comments
If deconstruction and possibly recursive/active patterns are going to employ |
@HaloFour Swift uses |
So are you proposing the three following patterns?
public static int Weird((int, int, int) tuple, out int x) {
let (out x, var y, let z) = tuple; // assigns x
y += z; // can modify y, but not z
return y;
) |
@HaloFour
So you will have: F(out var x); // read/write
F(out let x); // readonly
G(out (var x, var y));
G(out (let x, let y)); There is some interactions between this and already proposed tuple declarators in G(out var (x, y)); // read/write Which I think can be still integrated to patterns via the identifier-pattern which I described before, G(out let (x, y)); // readonly This is applicable to all the places that we have patterns (like |
I see what you're doing, this is not related to your comment here: #9005 (comment) I can see some utility in this proposal but it feels weird given that it assumes that the return value from the method is related to the pattern. I honestly think that the first example (with |
@HaloFour The thing is, wherever I can deconstruct a tuple (e.g |
So, you'd expect the following to work? string s = "2";
if (int.TryParse(s, out 2)) {
Console.WriteLine("Yup, it's a two.");
} |
Yes, it might not be always useful, just like Alternatively, if we only allow complete patterns. all of these edge cases would just go away. |
Is this not equivalent to "var patterns" that were proposed before and almost made it into the language? One could write a function var patterns would be more general and more convenient. I'd like to have them but I don't remember the reason they did not make it. |
@GSPP Currently out vars don't use var patterns, therefore they do not support patterns at all, so the two are not the same thing, but could be. Tuple decomposition in out var is a special case which I proposed it to be not. In fact, I don't know why #10642 exists when we have patterns on the way.
They did. See #10866. |
I'm not speaking of pattern matching @alrz. I seem to miss the right term. I meant:
|
The advantage of this approach compared to the first example (using |
Proposal: out patterns
out var
would allow a variable be declared at the same time that is it passed as an out parameter:But if you want to also match the output, you will need to use a separate
is
expression (assuming array patterns),The idea is to be able to use a pattern in front of
out
keyword forbool
-returning methods, so that the simple form is a var-pattern,If the method returns
false
the pattern would not be evaluated and immediately fails. In case of a complete pattern, it always succeeds regardless of the return value,The advantage of this approach compared to the first example (using
is
and&&
) is that you can use recursive patterns right in font ofout
, while withis
and&&
it will become complicated very fast.EDIT: The pattern can be restricted to only complete patterns so that it doesn't depend on the method.
The text was updated successfully, but these errors were encountered: