|
Umm ... is predates as , yes? as is intended as an improvement over is .
|
|
|
|
|
is with a built-in as is new though
|
|
|
|
|
|
Maybe under other names?
- is with "declaration pattern"
- Three-operand is
- C#7-is
|
|
|
|
|
Seems kludgey.
We need a whole new language with everything we've learned over the past twenty years included, with cleaner syntax, rather than bits stuck on at odd angles.
|
|
|
|
|
The "best" part is that, like out -with-declaration, the declaration pollutes the scope surrounding the if . That's also annoying about the old pattern of using as and checking whether the result is null , but this new syntax syntactically suggests that it solves that long-standing annoyance and it doesn't.
|
|
|
|
|
I agree with a desire to not pollute the scope with rubbish. On the other hand, maybe defining a new scope is the better solution -- define a new Method.
In so many cases, when a scope becomes polluted, it's a side-effect of not splitting the logic into enough granularity.
It seems like maybe C# needs a with statement . Or maybe not, I've never liked the with statement in languages which include it. But if C# could get with right, maybe even I would use it.
|
|
|
|
|
|
Which is not a with statement.
|
|
|
|
|
And what is a with statement
|
|
|
|
|
Something C# doesn't have. And which I find useless in lesser languages.
|
|
|
|
|
I'd agree - the scope should logically be limited to the if block.
It seems strange that it wasn't really ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Needs to apply to an else if present?
|
|
|
|
|
Nope - because if it isn't that class, it should be at best null and thus unusable in the else
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Which maybe I want! I can use null better'n anybody! (OK, probably not.)
But what if the syntax allowed the use of the null-coalescing operator as well! Way hey!
|
|
|
|
|
We could call it something like B# and just copy and paste VB.NET, best language without the VB name stigma
Oh wait we don’t want something easy that can do the same job better do we
KISS principle
|
|
|
|
|
It does the null test, declares the new variable, assigns the value and completes the if in one statement. Think of it like the very old C way of doing a for loop:
int i;
...
for (i = 0; i < 10; i++) ... As opposed to the simpler version that was added in C99:
for (int i = 0; i < 10; i++) ... People complained that that was a kludge back then as well!
I was a sceptic, but once you are used to it you probably won't go back.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I agree, yes, that's good, though I don't use C99 either.
On the other hand, I notice that there is no similar syntax for while .
|
|
|
|
|
While is more of a "general purpose" loop construct, most commonly used with things that have been already constructed, like file pointers, linked lists, strings I think. It's less likely that you'd need the declarative part since a while loop doesn't have an initialisation phase like a for loop does.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
But it could! Except then it might just be roughly equivalent to a foreach anyway.
|
|
|
|
|
Which they added to C++ anyway ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You can't keep anything nice. (I've only ever dabbled in C++ .)
I may need to look at D again.
|
|
|
|
|
I really like ?? since they made it possible to throw.
something = myParameter ?? throw new ArgumentNullException(...)
also handy in if statements.
Optional arguments: better than 100 overloads, but as with everything use with care.
(): If indeed value tuples - better than what we had (no more Item1, Item2)
?[]: Never used it, but I use ?. all the time -and it would be strange if ?[] wasn't available as well.
Discards: Handy when needed - do not use them a lot, but when you do they make it clear right away.
Switch expressions: Why oh why.... where they not added earlier. No more crappy switch statements full of returns or variable assignments - and a compiler warning when I am missing a state. I need to figure out how to use them nicely with enums though - I want a catch all that throws if not a defined enum value, but still want a warning if a new enum value is added and I do not handle it
And nullable in general is of course the best thing since sliced bre... no, wait - I can just eat the bread without slicing it! Just too bad it is a bit crippled as we still need to call legacy code and the required keyword isn't coming before next version.
|
|
|
|
|
lmoelleb wrote: I need to figure out how to use them nicely with enums though - I want a catch all that throws if not a defined enum value, but still want a warning if a new enum value is added and I do not handle it Once you figure it out... write a tip
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
lmoelleb wrote: Switch expressions: Why oh why.... where they not added earlier. Yes, exactly!
|
|
|
|