Live data from Hacker News

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

blog.ivankahl.com

171–174 of 174 posts

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

#171
post #168

Earlier quoted context omitted.

I don't understand what you are talking about. The syntactic sugar already exits before this change. That is to say, in a version f C# without the feature, you can write this: a.b?.c = foo It's not not well-formed semantically. What the change does is allow the above to be well-formed semantically. If b isn't null, then it behaves like a.b.c = foo. Otherwise, the value of foo is discarded. (Perhaps foo isn't even eva…

> The idea that there is no change in semantics, but only syntactic sugar is exactly backwards. > The syntactic sugar already exits before this change. That is to say, in a version f C# without the feature, you can write this: a.b?.c = foo You cannot. The semantics is conditional assignment, which already exists in the language. Hence the article repeatedly bludgeoning you with pairs of code snippets - whose semantic…

Using older C#, a minimal program in which I have this expression:

  a.b?.c = foo
produces:

  Main.cs(19,9): error CS0131: The left-hand side of an assignment must be a variable, property or indexer
Note that it is not saying "syntax error". C# has parsed it. We have an assignment with a left hand side. The syntax already exists. There is just a semantic constraint error.

This error got replaced with code which allows that type of expression, and generates code for it rather than a diagnostic.

I can't say for sure that they did zero parser work for the feature, but it sure looks like in principle, you do not have to, for this case, given that it already parses.

(If they have a parser which handles semantic attributes in teh grammar like "assignable expression", then of course that gets adjusted.)

For reference:

  using System;

  public class A
  {
    public B b { get; set; }
  }

  public class B
  {
    public int? c { get; set; }
  }

  public class Program. 
  {
    public static void Main()
    {
      A a = new A { };
      int foo = 42;
      a.b?.c = foo;
    }
  }

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

#172

The thing I enjoy most about C# is the depth/levels of progressive enhancement you can do. Let's say in the first instance, you write a proof of concept algorithm using basic concepts like List , foreach, stream writing. Accessible to a beginner, safe code, but it'll churn memory (which is GC'd) and run using scalar CPU instructions. Depending on your requirements you can then progressively enhance the memory churn,…

I have occasionally, just for fun, written benchmarks for some algorithm in C++ and an equivalent C# implementation, them tried to bring the managed performance in line with native using the methods you mention and others. I'm always surprised by how often I can match the performance of the unmanaged code (even when I'm trying to optimize my C++ to the limit) while still ending up with readable and maintainable C#.

Does this include the GC at the end of it all? Because if that happens after the end timestamp it's not an exact comparison. I read something once about speeding up a C/C++ compiler by simply turning free into a no-op. Such a compiler basically allocates more and more data and only frees it all at the end of execution, so then doing all the free calls is just wasted CPU cycles.

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

#173
post #93

Earlier quoted context omitted.

Thanks. I missed it. I really dislike that, because it hides the control flow too much. Perhaps I'm biased by Racket, where it's easy to define something weird using macros, but you should not do unexpected weird things. For example you can define vector-set/drop that writes a value to a position of a vector, but ignores the operation when the position is outside the vector. For example (vector-set/drop v -2 (print "…

I think in the context of C# this is not exactly unexpected since there are various operators that apply conditional execution (and evaluation) to parts of an expression. All of them start with ?, though (? :, ??, ?., ?[], ??=), so except for casts to nullable types, every question mark in an expression signals that control flow is about to happen and it's been that way in C# for quite a while.

[Too late, sorry for the delay, but I had to think about it.]

That ooks like a good reason. Looking at https://learn.microsoft.com/en-us/dotnet/csharp/language-ref... , the magic is in the LHS or the RHS, and in ??= the magic is explicitly in the connection. Here, IMHO, the problem is that the magic jumps from one side to the other. Anyway, I don't use C#.

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

#174

It's starting to feel like C# is going down the path of C++. Tons of features that introduce subtleties and everybody has their own set of pet features they know how to use. But the code gets really hard to understand when you encounter code that uses a subset you aren't familiar with. I remember staring at C++ codebases for days trying to figure out what is going on there. There was nothing wrong with the code. I ju…

That's why I keep using Golang instead of C#, even though C# is better in a lot of ways. But with Go, I normally can understand any sourcecode I'm control clicking into.

I've never gotten to this point with any other language, no matter how hard I tried.

Post reply on HN