New 'Source Generators' Stir Up the C# Crowd
Who knew there were so many C# library authors out there?
New C# Source Generators unveiled in preview by the .NET and Languages dev team immediately garnered a lot of interest in the .NET dev camp, with more than 100 comments tacked on to an introductory post.
"We're pleased to introduce the first preview of Source Generators, a new C# compiler feature that lets C# developers inspect user code and generate new C# source files that can be added to a compilation," said Phillip Carter, program manager, .NET and Languages. "This is done via a new kind of component that we're calling a Source Generator."
Much of that huge post is devoted to explain exactly what Source Generators are.
"A Source Generator is a piece of code that runs during compilation and can inspect your program to produce additional files that are compiled together with the rest of your code.
"A Source Generator is a new kind of component that C# developers can write that lets you do two major things:
- Retrieve a Compilation object that represents all user code that is being compiled. This object can be inspected and you can write code that works with the syntax and semantic models for the code being compiled, just like with analyzers today.
- Generate C# source files that can be added to a Compilation object during the course of compilation. In other words, you can provide additional source code as input to a compilation while the code is being compiled.
Source generators improve on three existing technologies used to inspect user code and generate information or code based on said analysis:
- Runtime reflection
- IL weaving
- Juggling MSBuild tasks
High-level design goals include:
- Generators produce one or more strings that represent C# source code to be added to the compilation.
- Explicitly additive only. Generators can add new source code to a compilation but may not modify existing user code.
- Can produce diagnostics. When unable to generate source, the generator can inform the user of the problem.
- May access additional files, that is, non-C# source texts.
- Run un-ordered, each generator will see the same input compilation, with no access to files created by other source generators.
- A user specifies the generators to run via list of assemblies, much like analyzers.
The new tech is being introduced in preview to let library authors put it through its paces before it becomes generally available, which is targeted for the release of C# 9.
As Microsoft is neglecting the development of Visual Basic, there are no plans to add Source Generators to that language, though F# is a possible option still on the table if F# aficionado care to request it.
Another possible option is Microsoft-generated Source Generators, but that has no timeline yet.
For now, coders can begin using the new tech by flipping the switch that turns on the ability to use preview language features for the C# compiler.
According to the unusually high number of comments to the introductory post, there might be a lot of .NET developers out there who will do just that. As of press time, there were 104 comments, with the latest posted yesterday on the April 29 post.
One coder was excited about one of the use cases mentioned above, obviating the need to mess around with MSBuild abstractions:
This is a very promising feature. I've been maintaining a code generator for years. We switched over to .Net Core and being a DotNetCliTool, then over to a local tool, and the amount of trial and error and refinement to reasonably well plug into building, cleaning, rebuilding, incremental building, etc. was very high. It will be great if this made all that MSBuild file work and experimentation a thing of the past.
Other laudatory comments included:
- Reminds me very much of supercharged string mixins from D, which I've been wishing for in C# forever. Can't wait to try it out!
- I was waiting for this feature very long! Keep working and ship it fast.
- As a regular abuser of Fody, I think this is a great feature to have!
- Awesome! I do love code generation, and I often use t4.
However, many developers decried the inability to change existing code, as can be done with other metaprogramming features such as macros or compiler plugins, something that Carter had explicity said was not allowed, on purpose:
Source Generators are a form of metaprogramming, so it's natural to compare them to similar features in other languages like macros. The key difference is that Source Generators don't allow you _rewrite_ user code. We view this limitation as a significant benefit, since it keeps user code predictable with respect to what it actually does at runtime. We recognize that rewriting user code is a very powerful feature, but we're unlikely to enable Source Generators to do that.
In response to a bunch of comments calling for this ability, Carter was adamant: "No. As mentioned earlier, Source Generators do not allow you to rewrite user source code. We do not intend on allowing them to this. They can only augment a compilation by adding C# source files to it."
For now, to help library authors try out Source Generators, Carter pointed to:
About the Author
David Ramel is an editor and writer for Converge360.