-
Notifications
You must be signed in to change notification settings - Fork 4k
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
(Idea) Templates for code. ("active" parts considered by some as macros) #174
Comments
Templates are better way to speed up coding than code snippets, because templates can be edit once and reflects in all usages, and you can have different templates in different project types. Templates can be defined in namespaces and classes, like
In template body, uses
compiles to
Another example for
compiles to
And in template body, a special object, Templates can be compiled as a special type into binaries, other project can reference this library to use these templates. |
If I understand this, it is a proposal for a macro system. |
Closing, as we do not want to add a macro system to C# of VB.net. |
@gafter It not entirely about macros. It's also about design-time code template (akin to VS snippets). It would allow "snippets" to be within a specific namespace, and not global. (In fact the current snippets ain't local, they are Visual Studio local. They are not tied to a specific project type, or even aware of the context they are to be pasted in (eg. #7858) Yeah you could implement them as a code fix, but to that seem like a lot of code. Just to paste in section of code. In principle the ide and compiler can conspire together and automatically do the work for you. We could also have usage attributes associated with the template, so that the IDE would be informed to when to suggest the snippet. |
If I understand correctly this proposal is a bit like defmacro in Common Lisp. Or is this more like inline T4? |
@Ytrog |
@gafter Is there any limited subset of macros you would be willing to support (e.g. variadic templates)? |
The use of this is a little specialised, mainly aimed at code that manipulates and analyses sourcecode.
Basic concept is to allow the coder to write the code they what to make as a Code Template and the compiler generates to code Roslyn's AST needed to make it.
How these would be expressed in code is up for discussion, as a base to start from I'm use the following. The type
Expr<T>
denotes an expression that evaluates to aT
, a statement would be anExpr<Void>
..Codeplex Thread: Template Functions & Template Masks
Template Function
Template Mask
This example will use template functions and template mask to replace If - Else structure to Inline if structure.
This template mask matches against assignments.
This template generates an assignment that utilises and inline if expression.
cond ? true : false
So let's utilise the previously construct templates and template masks.
Some of the functionality of Template Function can be implemented with the use of String Interpolation. Eg
The text was updated successfully, but these errors were encountered: