|
|
The purpose of the "is" contruct with multiple conditions is to avoid repeating the first operand (imagine it is a function or method, or attribute whose evaluation or dereference would have a side effect).
Here it allows writing it, without having to declare and initialize the value of an additional local temporary variable (that temporary variable or register will be allocated/defined intrisicly by the compiler, and only where it needs it). You get a shorter and more readable condition with "is" than with such additional explicit declaration.
Then when testing "c is" with constants or ranges, this becomes very clear, and the compiler can choose how to represent the ranges or individual values (notably when they are constant) and generate the best code (yes it means that it can then optimize "c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z')" itself into "(uint)((c | 0x20) - 'a') <= 'z' - 'a'", but without using ugly expressions with unsafe/unchecked typecasts.
So in my opinion this short notation is a good idea, and it remains very readable while being safe (and allowing easier optimization and inlining if needed by the compiler).
Doing the same thing in C++ is a real nightmare (you have to play with very ugly "move" semantics that are very hard to debug when there are also multiple overriden candidates for operators/convertors/constructors and so many implicit typecasts to play with). Doing that in C is simpler, but hard to manage as the compiler has no knowledge or control of the semantics, so it cannot make so many optimizations: you need to use explicit local "register" declarations, put multiple statements within braces, possibly surrounded by the "do{}while(0)" trick when using ugly macros... And still the C compiler is unable to perform optimizations you'd expect (and there's no warranty that it works when you also have to play with "volatile" semantics), plus the effect of "restricted" state concurrency used by modern processors using multiple cores and modern architectures using caches over shared buses/networks, where loads/stores may be reordered.
Cosidering all this, the "is" construct is very safe and the compiler will generate the correct code while preserving all the needed semantics, and without pollutiong the source code with ugly temp varaibles and extra local scopes.
|
|
|
|
|
Be more positive - learn these additions, but use only if fits...
After all - they do not force you!!!
"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid." ― Albert Einstein
|
|
|
|
|
Gone are the days when you could sit back a read a "Command" manual. You need at least a browser.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Kornfeld Eliyahu Peter wrote: Be more positive - learn these additions, but use only if fits...
After all - they do not force you!!! Nope, you're wrong. It's very easy to get in the position of supporting someone else's unreadable code.
Of the last 8 to 10 C# releases, I've found about 90% of the new syntax to either be useless or counter-productive, as it makes the code unreadable.
The C# team keeps making "additions", as it's their job, and for the most part they ran out of useful things to add years ago, but they don't let that stop them.
|
|
|
|
|
Some of them are pretty useful. For example:
private void DroppedURL_DoWork(object sender, DoWorkEventArgs e)
{
if (sender is BackgroundWorker worker)
{
...
Makes code much cleaner than the traditional way:
private void DroppedURL_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if (worker != null)
{
...
"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!
|
|
|
|
|
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!
|
|
|
|