Live data from Hacker News

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

github.com

11–20 of 71 posts

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

#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 to Zig, where if you have a regular runtime factorial function, you can just execute it at compile time like:

    const x = comptime factorial(n);
Another limitation of the C# package is it only works with primitive types and collections. Zig comptime works on any arbitrary types.

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

#13
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…

You don't. The way it works is that it intercepts the call site when the input args are constant. If they're not then it won't be replaced and it will call the original method. C# source generators can't replace method definitions, however a call site can be changed to another method via source generators.

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

#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 to System.Type, adding some basic pattern matching for structures -- but haven't found a way to do it that's general enough while being very useful.

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

#15
post #11

Earlier quoted context omitted.

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…

You don't. The way it works is that it intercepts the call site when the input args are constant. If they're not then it won't be replaced and it will call the original method. C# source generators can't replace method definitions, however a call site can be changed to another method via source generators.

You don’t what?

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

#17

Earlier quoted context omitted.

You don't. The way it works is that it intercepts the call site when the input args are constant. If they're not then it won't be replaced and it will call the original method. C# source generators can't replace method definitions, however a call site can be changed to another method via source generators.

You don’t what?

You don't have to write the method twice. Source generators can only add new code, they cannot take away a method you declared, so it will still be there at runtime when called with non-constant arguments.

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

#18
post #16
post #8

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

D was already doing it in 2010 thereabouts, an then there is the whole reader macros in Lisp and Scheme.

And Nim, which has multiple levels of metaprogramming.

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

#19
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…

Yeah, it is kind of sad that it was the community that had to step up for some T4 like experience instead of string concatenation.

That isn't as cool as Aspire and AI features.

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

#20
post #6
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…

Also t4 templates before that for at least a decade

And much more developer friendly.
Post reply on HN