Live data from Hacker News

Comptime – C# meta-programming with compile-time code generation and evaluation

github.com

61–70 of 71 posts

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#61
post #5
post #2

This seems like the kind of feature that should be built into MSBuild.

It's a lot less ergonomic but there are source generators in C# : https://devblogs.microsoft.com/dotnet/introducing-c-source-g... That said, for more complex results, you'd typically load a serialization on start. I can see the value in this tool, but there must be a fairly limited niche which is too expensive to just have as static and run on start-up and cache, but not so large you'd prefer to just serialize, store…

> there are source generators

Last time I tried them discovered source generators in the current .NET 10 SDK are broken beyond repair, because Microsoft does not support dependencies between source generators.

Want to auto-generate COM proxies or similar? Impossible because library import and export are implemented with another source generators. Want to generate something JSON serializable? Impossible because in modern .NET JSON serializer is implemented with another source generator. Generate regular expressions? Another SDK provided source generator, as long as you want good runtime performance.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#62
post #14

Earlier quoted context omitted.

I love (and heavily use) source generators, but the development experience is godawful. Working with the raw Roslyn types is painful at best and this is compounded by them having to be written against .NET Standard, severely limiting the use of newer .NET functionality. Eventually I want to write a good baseline library to use for my source generators -- simplifying finding definitions with attributes, mapping types…

I agree, .NET Standard limitation unnecessarily complicates development experience. I think it's because some tools (Visual Studio) is still use legacy .NET Framework. I don't understand why they didn't integrate them via out of process architecture into these tools, since source generators didn't exist in the legacy framework anyway. I sometimes generate code from plain CLI projects (avoiding source generators altog…

Yeah, I went with that approach for most of the code generation in the emulator I'm currently working on. Source generators handle a few core things, but more advanced compilation tasks went to just ahead of time generation; couldn't get my parser combinator library to play nicely with .NET Standard, so that was just a dead-end.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#63
post #5

Earlier quoted context omitted.

It's a lot less ergonomic but there are source generators in C# : https://devblogs.microsoft.com/dotnet/introducing-c-source-g... That said, for more complex results, you'd typically load a serialization on start. I can see the value in this tool, but there must be a fairly limited niche which is too expensive to just have as static and run on start-up and cache, but not so large you'd prefer to just serialize, store…

> there are source generators Last time I tried them discovered source generators in the current .NET 10 SDK are broken beyond repair, because Microsoft does not support dependencies between source generators. Want to auto-generate COM proxies or similar? Impossible because library import and export are implemented with another source generators. Want to generate something JSON serializable? Impossible because in mod…

You can source generate JSON serdes from your own source generator but you do need to generate the jsontypeinfo metadata yourself.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#64
post #14

Earlier quoted context omitted.

I love (and heavily use) source generators, but the development experience is godawful. Working with the raw Roslyn types is painful at best and this is compounded by them having to be written against .NET Standard, severely limiting the use of newer .NET functionality. Eventually I want to write a good baseline library to use for my source generators -- simplifying finding definitions with attributes, mapping types…

I agree, .NET Standard limitation unnecessarily complicates development experience. I think it's because some tools (Visual Studio) is still use legacy .NET Framework. I don't understand why they didn't integrate them via out of process architecture into these tools, since source generators didn't exist in the legacy framework anyway. I sometimes generate code from plain CLI projects (avoiding source generators altog…

.NET standard isn’t the biggest issue with making source generators. You can’t add dependencies to your project, which is an absolutely huge oversight IMO.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#65
post #11
post #8

I think Zig really shines here: https://ziglang.org/documentation/master/#comptime

A key difference is that in this C# package, `[Comptime]` is an attribute (annotation? not sure on the C# term) applied to methods. In Zig, the `comptime` keyword can be applied to pretty much any expression. In the C# package, if you want to do factorial at runtime and at compile time, (I think, from reading the README) you need to define the same function twice, one with `[Comptime]` and once without. Contrast this…

Decorations in [square brackets] are Attributes in C#

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#66

I started using C# recently for a hobby project writing a game engine on top of Monogame, and I have been very surprised at how nice of a language C# is to use. It has a clean syntax, decent package management, and basically every language feature I regularly reach for except algebraic data types, which are probably coming eventually. I think the association of .NET to Microsoft tarnished my expectations.

Unions have a proposal: https://github.com/dotnet/csharplang/issues/9662

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#67
post #31

Earlier quoted context omitted.

Array also implements IReadOnlyList if I'm not mistaken. I think C# doesn't really have immutable collections, they just can be typecasted to IReadonly* to hide the mutable operations. But they can always be typecasted back to their mutable base implementation. The only real immutable collections I know of, are F#s linked lists.

Immutable collections exit, they were just added later. See System.Collections.Immutable: https://learn.microsoft.com/en-us/dotnet/api/system.collecti...

Those collections are more like copy-on-write than actual immutable. System.Collections.Frozen is the real thing.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#68
post #67

Earlier quoted context omitted.

Immutable collections exit, they were just added later. See System.Collections.Immutable: https://learn.microsoft.com/en-us/dotnet/api/system.collecti...

Those collections are more like copy-on-write than actual immutable. System.Collections.Frozen is the real thing.

Isn't Frozen something you do to a set or dictionary to say, I'm not going to add any more values, please give me a version of this which is optimized for lookup only?

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#69
post #64

Earlier quoted context omitted.

I agree, .NET Standard limitation unnecessarily complicates development experience. I think it's because some tools (Visual Studio) is still use legacy .NET Framework. I don't understand why they didn't integrate them via out of process architecture into these tools, since source generators didn't exist in the legacy framework anyway. I sometimes generate code from plain CLI projects (avoiding source generators altog…

.NET standard isn’t the biggest issue with making source generators. You can’t add dependencies to your project, which is an absolutely huge oversight IMO.

You can add dependencies, it's just important, that you also add the dependencies to the project where the source generator is used:

SourceGenerator.csproj:

  
FinalProject.csproj:

  
  
And I also recommend to add "latest" to SourceGenerator.csproj to use new features like raw string literals.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#70
post #14

C# meta programming game is strong. Source generators are :chefs_kiss:

I love (and heavily use) source generators, but the development experience is godawful. Working with the raw Roslyn types is painful at best and this is compounded by them having to be written against .NET Standard, severely limiting the use of newer .NET functionality. Eventually I want to write a good baseline library to use for my source generators -- simplifying finding definitions with attributes, mapping types…

Add "latest" to the csproj to get more useful features like pattern matching and raw string literals.
Post reply on HN