-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Req] Public api for compile-time codegen? #2205
Comments
I think it should be easier to express code, rather than building the node syntax tree. |
@AdamSpeight2008 As for me it is too big and controversial feature to be easily fitted into c#.
All you have to do is replace stubs with actual nodes. And if you're too lazy, it can done using string interpolation,
thats all. Now, here is what's wrong with macros:
As a summary: code rewriters offer almost ideal balance between developer qualification, scenarios supported and cost of support. As for me there's no place for macros-driven metaprogramming in c#. However I would love to hear if I'm wrong. |
I would like to upvote this proposal, seems like a natural addition to Roslyn and could allow a broader set of feature without chenging the language itself. There are a tons of possible use cases, perf improvement of serialization and aop just to name a few |
You're right you can do it using a string, then converting it at runtime. In which lies an issue, you don't get compile-time type-checking of that section of code. My proposal / idea is to have compile-time checked templates. (like C++) |
@AdamSpeight2008
So, the code above is meant to be executed at compile time as additional compilation step. No runtime magic at all. If you want to prove that rewriter/macro is working correctly you had to write tests anyway. There always are some errors that cannot be caught by compiler. |
@ig-sinicyn The example from the first link, would look something like this use a template function. template syntaxnode FormWithTicker( string ns , string fn )
{
namespace %{ns}%
{
public class %{fn}%() : System.Windows.Forms.Form
{
public System.Windows.Forms.Timer Ticker
{
get; set;
}
[STAThread]
public void Main
{
}
}
} The compiler would / should check at compile-time that the structure of the code is correct. |
Well, from my point of view it is useless to check template itself. As example, template code can derive from type not referenced by the template library. It could be derived from another template. It could use mixin-style template combinations. Or use members from type being passed as template argument. And so on and so on. So, the only way to check template is to apply it to the real code. And at this moment there will be no difference between macros, code template and classical code rewriter. Also, did you notice that your template could be easily replaced with usual base class? It seems that the discussion begins to look like "macro vs aop" holywar, so I'll stop for mow. |
I've written a working implementation of Roslyn compiler plugins similar to this, along with attribute macros as described. Take a look at #3248. |
In progress in #5561. |
Hi!
The metaprogramming feature was discussed multiple times already (e.g. #98 and #2136) but all of discussions as so far ended with "too big to fit" resolution.
At the same time there are a lot of real-world codegen tasks, such as
Each of these uses its own code generation approach, but all of them work fine, nothing fails and no one hurts. So, lets start with assumption that adding one more way to apply some build-time-magic will not break anything.
Now, there are a lot of issues not related to the c# syntax and too specific to be supported directly by the c# compiler. To name a few: #1677 (support for INPC), dependency properties, #105 (easy Equals() implementation) and so on. It seems that allowing to add custom code rewriters is the only solution that can be safely done at c# side.
All of these issues perfectly fits into following restrictions:
for
if you do not like standard one" approach, no template-based magic to imitate algebraic types, no need for metalanguage and so on. Just write the code that automates writing some more code for you, thats all.In summary:
So, what I've missed?:)
The text was updated successfully, but these errors were encountered: