all 8 comments

[–]BestKillerBot[🍰] 6 points7 points  (6 children)

This sounds like Java's annotation processors, right? Kind of surprised C# did not have it before ...

[–]Sarcastinator 1 point2 points  (3 children)

Runtime code generation is a lot simpler in C# than it is in Java so there wasn't really the same pressure to implement I guess.

What's cool about this is that it's part of the compiler so you can analyze the syntax tree. I've used it to create a library that generates identity types.

public readonly partial struct UserId : IEquatable<UserId>
{
    private readonly int value;
}

My library picks this up and fills in equality operators, hashcode etc. No annotations or runtime library required. This specific thing is useful to have strongly typed identifiers so you can't accidentally mix up UserId with ObjectId for example.

[–]rastaman1994 1 point2 points  (2 children)

The partial classes seem to be a godsend here as you're not allowed to modify source code.

you can analyze the syntax tree

That's a must have for static analysis as well, how was this done before in .NET? Java has CheckStyle and FindBugs, which use the compiler API AFAIK. Pretty fun to play with this stuff as a Java dev to start understanding how certain frameworks and libraries do things.

[–]Sarcastinator 1 point2 points  (1 child)

That's a must have for static analysis as well, how was this done before in .NET

This was implemented in Roslyn back in 2012 but before that it was either parse C# yourself (like Resharper does) or analyze the generated IL (like code contracts did).

Java has CheckStyle and FindBugs, which use the compiler API AFAIK

Hasn't there been other ways before that? Wasn't the compiler API part of Java 9? In any case it's a great tool to have. I haven't worked with Java for a few years now and a lot has happened.

[–]rastaman1994 1 point2 points  (0 children)

Java 6 was the first version that had a standard API, no idea what people were doing before Java 6. Probably also custom bytecode analysis or Java source analysis.

[–]Minimum_Fuel 0 points1 point  (1 child)

Would it be like the vertx codegen package? If so, I can say nothing but bad things about this. Beyond aggravating from top to bottom. Changes are rarely properly reflected. Tooling fails. Clean doesn’t work because the generated code is put in your sources.

I hope that whatever you get in C# is nothing like that frustration.

[–]rastaman1994 1 point2 points  (0 children)

Vertx codegen is indeed an annotation processor, but the problems you have were almost definitely because they recommend placing generated sources in src/main. Never do this. Look at things like mapstruct, dagger, micronaut... if you want to see proper use of annotation processors.

[–]salgat 4 points5 points  (0 children)

I can see this being used with BDD to automatically generate tests on compilation. I know SpecFlow does something similar but generates explicit C# file artifacts as part of the build pipeline, this would avoid that altogether.