The new syntax is not intuitive, not more readable, just a bit more terse.
The new syntax is not intuitive, not more readable, just a bit more terse.
Once you start making switch expressions, the break keyword makes no sense. And with pattern matching the case keyword technically wouldn't be nessesary for normal switch statements already, might as well drop it for the new syntax.
I find this completely reasonable, and I think it's very readable, especially compared to the closest equivalents: putting a single switch in a function and calling that, or using nested tertiary operators:
const sqlOp = op switch {
"&&" => "AND",
"||" => "OR",
_ => throw new NotSupportedException()
}
vs function getSqlOp(string op) {
switch op {
case "&&": return "AND";
case "||": return "OR";
default: throw new NotSupportedException();
}
}
// ... far away
const sqlOp = getSqlOp(op);
vs const sqlOp = op=="&&" ? "AND" : op=="||" ? "OR" : throw new NotSupportedException();In a switch expression of course that is moot since break is a statement, and statements can't appear in expressions.
x: y: DoX()
but you can't have
x: DoX() y: DoY()
at all.
case x:
DoX(); goto case y;
case y:
DoY(); break; case "foo" { /* ... */ }
case "bar" /* ... */;But, it makes almost complete sense if you instead look at it as coping with Lisp envy. I'm still amazed that they managed to sneak in function literals and FEXPRs (Linq), a metaobject system (dynamic + DLR), a limited form of call/cc (aysnc/await), first-class metaprogramming (Roslyn -- albeit more in the style of Smalltalk rather than Lisp), and a REPL.
I just mean that C# looks messy from the point-of-view of an ML-wannabe, but pretty elegant as an aspiring Lisp. And I say this as someone that is fluent in F# and prefers functional programming in the style of ML rather than that of Lisp.
"Confessions of a Used Programming Language Salesman, Getting the Masses Hooked on Haskell"
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.72....
While I use them every day, my favorite is still “replacing” String.Format with string literals. Shorter, sure, but less intuitive particularly for people coming from another language. You can do roughly the same thing in PHP with backticks, but most people avoid them for the same reasons.
I’m not sure how I feel about the trend as a whole. As long as I can continue using the old methods as well I suppose I don’t have much room to complain, but sometimes it does feel like we’re just changing for the sake of change and that’s usually a bad indicator.
Many languages also have format strings with interpolation: JavaScript, Python, Ruby, Swift, PHP, etc.
Java, C/C++/Objective-C/Go (I'm lumping them up because they're basically C + C NextGens), Matlab, SQL, ASM don't have them.
Everything else, has them.
On the list of languages that don't have it, Java is planning to add it, I don't see the C family to change because it's very conservative, and the others are domain specific and don't care that much about string operations.
For example:
var h = "Hello ";
var w = "world";
var output = String.Format("{1} {2}", h, w);
output = $"{h} {w}";
Would compile fine but throw on the String.Format() line due to the typo.There are also instances where, such as with Console.Write you are now formatting a string in order to pass it into... a method that formats strings. Yes, the same benefit is still provided, but with a fairly obvious decrease in clarity and increase in redundancy.
You feel that "{0} {1} {2}" provides better clarity than $"{FirstName} {MiddleInitial} {LastName}"?
In what's ways specifically? Using String.Format you have to keep your indices correct and 'compiling' the string in your head requires jumping back and forth between the format string and the args.
Interpolation wins on every account as far as I'm concerned.
Maybe for people coming from C++, but string interpolation is a feature in Python, JavaScript, PHP, etc. It's not exactly an unusal concept (like it was back when PHP started doing it)
Also, there is an elegance to the functional style of pattern matching. And C# has been incorporating a lot of functional paradigms for more than a decade now which may initially feel "out of place" but ultimately benefited the language greatly.
This is a half-assed, syntactically ugly implementation of case statements in functional languages like Elm or Haskell - the point of which is to cover all possible executions of a branch explicitly, to prevent runtime errors.