12 comments

  • smcnc 2 hours ago ago
    • Zambyte an hour ago ago

      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.
      • hahn-kev 21 minutes ago ago

        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.

    • Tiberium an hour ago ago

      There's also Nim, it shines even more in compile time and has proper compile time macros :)

  • CharlieDigital 5 hours ago ago

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

  • mfro 5 hours ago ago

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

    • eterm 4 hours ago ago

      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 and load.

      It also needs to be something that is dynamic at compile time but not at runtime.

      So it's very niche, but it's an interesting take on the concept, and it looks easier to use than the default source generators.

      • richardw 3 hours ago ago

        Not terribly niche. All config that isn’t environment-specific and is used in inner loops or at startup. It’s even got a test for serialised values so can be used to speed your case up:

        https://github.com/sebastienros/comptime/blob/main/test/Comp...

        But you need to be sure you won’t want to change without compiling.

      • piskov 4 hours ago ago

        Also t4 templates before that for at least a decade

  • mgaunard 5 hours ago ago

    So it's like C++ consteval?

  • jauntywundrkind an hour ago ago

    Makes me think of Boo language; Boo was so good at metaprogramming and multi-phasr programming! A very fine .NET language that was so far ahead of the curve, with having the tools of that language be usable at runtime.

    Alas many of the docs are offline now. But it had great quasiquotes, which let you write code that gets turned into AST that you can then process. Good macros. A programmable compiler pipeline. So much. Alas, obscured now. https://boo-language.github.io/

    • daeken an hour ago ago

      Boo and Nemerle both were really showing what was possible in .NET back in the early days. I still miss the metaprogramming they had, not to mention their pattern matching (which C# has closed the gap on, but is still way, way short.)