News for hackers. Stuff that matters.
Re:What's new in C# 12: overview by ToruiDev on Friday October 20, 2023 @08:43AM (#37955283)
Well, this features is designed to be used by source generators and not humans.

So as it's intended for being used inside the compilation process, i don't think this is too valid of a concern.

Re:What's new in C# 12: overview by gizmo on Friday October 20, 2023 @09:08AM (#37955500)
Source code generators are used because the language itself is not expressive enough. Maybe fix that instead.
Re:What's new in C# 12: overview by jbluepolarbear on Friday October 20, 2023 @09:39AM (#37955817)
So how would you solve the AOT (ahead of time compiling) problem without code generation? An entire ecosystem (Unity) that uses C# requires that code must be AOT for supporting IL2CPP (a low level translation of IL to c++). Dynamics and Reflection of non-AOT types are unavailable at runtime. IL2CPP came from Apples requirement that no JIT be run in apps and to get more performance; especially, for features like burst that allows writing C# that directly translates to high performance multithreaded c++.
Re:What's new in C# 12: overview by pjc50 on Friday October 20, 2023 @09:47AM (#37955926)
Different usage, I think: if people are talking about "language not expressive enough", they're referring to mechanisms for generating C#, or IL, not things further down the toolchain for AOT.

System.Xml.Serialization, for example, relies on generating assemblies at runtime to work. That's "code generation", but of a kind that directly conflicts with the AOT meaning of "code generation".

Re:What's new in C# 12: overview by pjc50 on Friday October 20, 2023 @09:40AM (#37955828)
You can't make a language fully general without it turning into a mess; things like the protobuf compiler are reasonable use cases for source generation.
Re:What's new in C# 12: overview by jddj on Friday October 20, 2023 @09:47AM (#37955930)
That's not really true.

Source generation is used at some level to implement expressivity at minimal runtime cost. It's just operating at a different level of abstraction to make different tradeoffs.

Whether you do Foo.Serialize() and it uses reflection to enumerate the properties, carries around tags permanently or it uses some compiletime generated function has nothing much to do with expressiveness.

If you design around a Foo<bar> does it matter if behind the scenes it generates a FooOfBar?

Should a language inherently care specifically about protobufs, flatbuffers, capnproto, etc because expressiveness or should it just have capable source generators for building strongly typed interfaces without the legwork? Are you sure an alternative implementation which is more expressive would be better or would it just be different?

Re:What's new in C# 12: overview by gizmo on Friday October 20, 2023 @10:46AM (#37956840)
> Whether you do Foo.Serialize() and it uses reflection to enumerate the properties, carries around tags permanently or it uses some compiletime generated function has nothing much to do with expressiveness.

Thinking those are the only options is exactly why the issue is expressiveness.

Re:What's new in C# 12: overview by pjc50 on Friday October 20, 2023 @11:19AM (#37957353)
Is this a reference to some specific, better, technique or an optimistic belief that such a thing could be built if only ..?
Re:What's new in C# 12: overview by gizmo on Friday October 20, 2023 @11:43AM (#37957716)
Code generators are used because run-time introspection tends to be slow(er) and because debugging code generated at runtime is harder and because IDEs don't know how to deal with code generated at runtime. But there no reason why this should be so. No good reason to have a hard boundary between compiletime and runtime either. These are just historical artifacts.
Re:What's new in C# 12: overview by pjmlp on Friday October 20, 2023 @03:26PM (#37960400)
That is really true, it is a workaround to avoid designing a proper macro system instead.

Likewise the new interceptors infrastructure is a workaround to avoid implementing proper AOP support, like Microsoft Fakes.

Re:What's new in C# 12: overview by lozenge on Friday October 20, 2023 @09:58AM (#37956086)
One example would be a tool that you compile in and records some state after every LOC in a method which you are interested in.

If you wanted to use a normal debugger at the same time you would usually be shown your code with the tool's generated code mixed in.

With this you could step through just your code.

Re:What's new in C# 12: overview by Mountain_Skies on Friday October 20, 2023 @09:14AM (#37955543)
That's good to know. I wonder if VS and VS Code are set to at least produce warnings when the feature is used in human generated code. Any feature can be abused, will be abused. At least with warnings by default the programmer can know this isn't a desired use case and then decide if their particular need is a worthwhile exception.
Re:What's new in C# 12: overview by Rapzid on Friday October 20, 2023 @10:25AM (#37956508)
On the other hand if this oopsies its way into a codebase it could highlight a serious problem with the code review process..