5 comments

  • smcnc 5 hours ago
    • pjmlp 1 hour ago
      D was already doing it in 2010 thereabouts, an then there is the whole reader macros in Lisp and Scheme.
    • Zambyte 4 hours 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 3 hours 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.
        • NuclearPM 2 hours ago
          You don’t what?
          • ygra 1 hour ago
            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.
    • Tiberium 4 hours ago
      There's also Nim, it shines even more in compile time and has proper compile time macros :)
  • CharlieDigital 7 hours ago
    C# meta programming game is strong. Source generators are :chefs_kiss:
    • daeken 3 hours ago
      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.

  • mfro 8 hours ago
    This seems like the kind of feature that should be built into MSBuild.
    • eterm 7 hours 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 6 hours 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 7 hours ago
        Also t4 templates before that for at least a decade
  • mgaunard 7 hours ago
    So it's like C++ consteval?
  • jauntywundrkind 4 hours 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 4 hours 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.)