Live data from Hacker News

What's New in C# 14: Null-Conditional Assignments

blog.ivankahl.com

101–110 of 174 posts

Re: What's New in C# 14: Null-Conditional Assignments

#101

Cute, but is this actually needed? It's one more thing to remember, one more thing to know the subtleties of, and for what? To save writing a very readable and unambiguous line of code? It feels like the C# designers have a hard time saying "no" to ideas coming their way. It's one of my biggest annoyances with this otherwise nice language. At this point, C# has over 120 keywords (incl. contextual ones) [0]. This is a…

Yes, this doesn't actually add anything to the "size" of the language, if anything it actually shrinks it. It's existing syntax (the ? and ?? operators) and existing semantics. The only thing was that it worked in half the cases, only reads but not writes. Now this completes the functionality so it works everywhere.

You can argue that C# gets a lot of new features that are hard to keep up with, but I wouldn't agree this is one of them. This actually _reduces_ the "mental size" of C#.

Re: What's New in C# 14: Null-Conditional Assignments

#102
post #98

Earlier quoted context omitted.

Without it there's some silly inconsistency. For example I could call `person?.SetName(name)`, but if you wanted to refactor that into `person?.Name = name` you can't.

That's a great point I didn't think about. From that perspective, it does make sense. Maybe my feeling is just rooted in the fact I've never used a language which allowed ?. on assignment

My take is that it’s pretty minor. Modern C# has across the board null checking and for the most part you’re not designing things where this even comes up. You are, however, correct, in that I have 100% seen the ?SetName thing used by devs who just wanted to make the null checker go away and didn’t actually think about what the correct behaviour was.

Re: What's New in C# 14: Null-Conditional Assignments

#103

Cute, but is this actually needed? It's one more thing to remember, one more thing to know the subtleties of, and for what? To save writing a very readable and unambiguous line of code? It feels like the C# designers have a hard time saying "no" to ideas coming their way. It's one of my biggest annoyances with this otherwise nice language. At this point, C# has over 120 keywords (incl. contextual ones) [0]. This is a…

Yes, this doesn't actually add anything to the "size" of the language, if anything it actually shrinks it. It's existing syntax (the ? and ?? operators) and existing semantics. The only thing was that it worked in half the cases, only reads but not writes. Now this completes the functionality so it works everywhere. You can argue that C# gets a lot of new features that are hard to keep up with, but I wouldn't agree t…

> his actually _reduces_ the "mental size" of C#

IDK, if you read

  Settings?.RetryPolicy = new ExponentialBackoffRetryPolicy();

as "there is a now a ExponentialBackoffRetryPolicy" then you could be caught out when there isn't. That one ? char can be ignored .. unless it can't. It's another case where "just because it compiles and runs doesn't mean that it does the thing".

This to me is another thing to keep track of. i.e. an increase in the size of the mental map needed to understand the code.

Re: What's New in C# 14: Null-Conditional Assignments

#104
post #65

never using nulls is liberating. this is syntactic sugar for dealing with nulls. definitely welcome though, and definitely will be abused (which cant be done when nulls are actually banished)

When you don't, you'd likely have an Maybe for a username that would be a "string?" in C#. It's the same thing (more or less). For your optional type you still need some form of pattern matching to figure out whether you have it or not.

You can't banish the "absence of value" from any programming language. That wouldn't be a useful language. You can stop confusing "a string but perhaps not" as a single type "string" as C# did in the past though.

Re: What's New in C# 14: Null-Conditional Assignments

#105

Cute, but is this actually needed? It's one more thing to remember, one more thing to know the subtleties of, and for what? To save writing a very readable and unambiguous line of code? It feels like the C# designers have a hard time saying "no" to ideas coming their way. It's one of my biggest annoyances with this otherwise nice language. At this point, C# has over 120 keywords (incl. contextual ones) [0]. This is a…

> Cute, but is this actually needed? It's one more thing to remember, one more thing to know the subtleties of, and for what?

Hi there! C# language designer here :-)

In this case, it's more that this feature made the language more uniform. We've had `?.` for more than 10 years now, and it worked properly for most expressions except assignment.

During that time we got a large amount of feedback from users asking for this, and we commonly ran into it ourselves. At a language and impl level, these were both very easy to add in, so this was a low cost Qol feature that just made things nicer and more consistent.

> It feels like the C# designers have a hard time saying "no" to ideas coming their way.

We say no to more than 99% of requests.

> We're trading brevity for complexity

There's no new keyword here. And this makes usage and processing of `?.` more uniform and consistent. Imo, that is a good thing. You have less complexity that way.

Re: What's New in C# 14: Null-Conditional Assignments

#106
post #2

Isn't this more confusing? Because it skip the code if the value is null and I don't think it is normal to follow the flow assuming nothing has happened.

It kind of is more confusing because I always imagined the RHS to be evaluated first in an assignment, before the target is evaluated.

The motivation is that you don't want the side effects in some cases like GetNextId() but I think it's still strange. I hacven't thought deeply about it but i _think_ I'd rather keep the intuitive right-hand-first evaluation and explicitly have to use if (..) in case I have a RHS whose side effects I need to avoid when discarded.

Re: What's New in C# 14: Null-Conditional Assignments

#107

While this is nice, there are some long requested features like Discriminated Unions that got delayed a lot.

Discriminated unions are a hard feature to put into a mature language that already has other features in a similar space - I mean enums and class hierarchies. (.e.g. if your union is "Cat or Dog", then in OO terms they have a common base class "Animal" ). How does it play with records, structs, generics, etc etc.

That is why although they are much requested, none of the proposals that I have seen are simple to understand or easy to implement, and thus are proceeding slowly.

I don't really see Discriminated union as being in "competition" with "a?.b = c" as that's a "quick win" extension to previous ?. and ?? syntax. It's not even close to being of the same magnitude.

I would settle for a good built-in Result type, so that people don't roll their own crappy ones, or use various opinionated but incompatible libraries.

Re: What's New in C# 14: Null-Conditional Assignments

#108

Is .NET entering its twilight years as a tech people build new things with? I just can't imagine Gen Z wanting to start a project in C#. I realise there are still .NET shops, and I still talk to people who do it daily, but ours is a field driven by fashion whether we care to admit or not - and C# just does not feel as fashionable as it once did (I'm a former C# dev, up until 2020)

I'd say it's never been better tbh. I can't speak for Gen Z but .NET (for some reason) was never the choice of startups. Possibly because there is still a cost associated with the best developer experiences, such as for the best IDE's in editions that allow any size of for-profit org.

Re: What's New in C# 14: Null-Conditional Assignments

#109

Cute, but is this actually needed? It's one more thing to remember, one more thing to know the subtleties of, and for what? To save writing a very readable and unambiguous line of code? It feels like the C# designers have a hard time saying "no" to ideas coming their way. It's one of my biggest annoyances with this otherwise nice language. At this point, C# has over 120 keywords (incl. contextual ones) [0]. This is a…

Agreed, it appears that since they changed to early release their have become pressured to add new language features every year.

As polyglot I have the advantage that I don't have to sell myself as XYZ Developer, and increasingly I don't think C# (the language itself) is going into the direction that I would like, for that complexity I rather keep using C++.

Just wait for when extension everything, plus whatever design union types/ADT end up having, and then what, are they going to add on top to justify the team size, and yearly releases?

Despite my opinion on Go's design, I think the .NET team should take a lesson out of them, and focus on improving the AOT story, runtime performance, and leave the language alone, other than when needed to support those points.

Also bring VB, F# and C++/CLI along, this doesn't not have to be C# Language Runtime, where it gets all the features of what designed as a polyglot VM.

Re: What's New in C# 14: Null-Conditional Assignments

#110
post #90

Cute, but is this actually needed? It's one more thing to remember, one more thing to know the subtleties of, and for what? To save writing a very readable and unambiguous line of code? It feels like the C# designers have a hard time saying "no" to ideas coming their way. It's one of my biggest annoyances with this otherwise nice language. At this point, C# has over 120 keywords (incl. contextual ones) [0]. This is a…

Nothing is stopping you from constraining the language version you want to be used in your projects: https://learn.microsoft.com/en-us/dotnet/csharp/language-ref... You can force it all the way down to ISO-1/2. If this is still insufficient, then I question what your goals actually are. Other people using newer versions of C# on their projects shouldn't be a concern of yours.

Easily said, applies to complaints in other languages as well, this is only doable for people that work alone.
Post reply on HN