if ([double val, bool ok] = double.TryParse("5.x5"); ok) {
Console.WriteLine(val);
} else {
Console.WriteLine("err");
}
Of course, having "var" there would be even nicer. if ([double val, bool ok] = double.TryParse("5.x5"); ok) {
Console.WriteLine(val);
} else {
Console.WriteLine("err");
}
Of course, having "var" there would be even nicer.Lolwut? It's basically the same syntax that "for" has, which is why C2Y is finally getting it; besides, C++ already got it in 2017 — or even in 1998, if you count the "if (int x = f()) { ... }" shape.
if (double.TryParse("5.x5", out double val)) { ...
which would be the C# equivalent double.TryParse("5.x5", out double val, out bool ok);
if (ok) { ... }
in case of a hypothetical variant of C# that doesn't have functions, only procedures, so you'd have to return the data from subroutine calls by ref/out/globals exclusively. It's still would be the same semantics — but quite different ergonomics.So out of those three choices: return all results by assigning to ref/out parameters; return one result directly, all other by assigning to ref/out parameters; return all results directly — I personally prefer the last one. Almost nobody prefers the first one, yet people seem to be strangely attached to the second variant for some reason.
Anyway C# has this:
var (_, _, population1960, _, population2010) = QueryPopulationDataForYears("New York City", 1960, 2010);
My original comment was about "the historical inability of commonly used programming languages (from FORTRAN all the way to, say, Java/C#)..." — sure, by the 2010s (when the FP fad got pretty noticeable) the popular programming languages started getting syntactic sugar for handling products/sums, among other things. But in the seventies, the eighties, the nineties, even the early 2000s, the most popular programming languages didn't really have destructuring. Yes, the fringe ones did have it, e.g. Mesa (1979) — in exactly the "list of variables in square brackets" form — but who the hell used them? The mainstream, until very recently, was the single-value assignment only.