Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there macro facility for Java or C#?

Macros are useful.

Therefore, I occasionally bemoan the absence of macros in Java and C#. Macros allow me to force in-line but allow me the code-manageability of non-macro code.

Is there any Java- or C#-based project/product somewhere out there that effectively allow macros or specifying in-line expansion.

I am thinking of something like

@macro public void hello(int x){ ... }

or when I call a method, an @inline annotation preceding the call would effect the called-method to be in-lined.

or, should I need to know that I should just trust the compiler to make the best the decision for me that at the best of its analysis it might in-line a call.

I hope this question will not lead to debating the pro/cons/usefulness of macros.

like image 417
Blessed Geek Avatar asked Dec 10 '22 16:12

Blessed Geek


2 Answers

I would recommend trusting the JIT compiler to make the decision for you when it comes to inlining.

However, if you are just after macros for other purposes, in C#/.NET, there are other options. Much of what can be done with macros for utility purposes can be done via T4 (Text Template Transformation Toolkit). This is the basis for many ORM packages, for example.

like image 60
Reed Copsey Avatar answered Dec 12 '22 06:12

Reed Copsey


Macros are not part of the standard Java language, and I'm not aware of any macro preprocessor being supported by mainstream Java tools, IDEs and so on. So if you use macros in your Java code you should expect to experience some "pain". For example,

  • Source code debuggers won't let you set breakpoints relative to your original source code.
  • If you share your Java-with-macros code, many Java developers are likely to turn up their noses at it, and/or complain about having to install/use extra tools.

There are quite a few examples of third-party macro pre-processors for Java; e.g. Jatha, OpenJava, PrintMacroJ, JavaMacros, and so on ... (But have you ever come across a project that uses any of them?)


Macros allow me to force in-line but allow me the code-manageability of non-macro code.

True. But the JIT compiler can probably do a better job than you can in determining what should be inlined. It will know (for sure) how big the chunks are, and it will have runtime stats on execution frequency, branch prediction, etc that are not available to you.

Note that there are some Hotspot JVM tuning options that can influence the optimizer's decisions on inlining; see this page, and scan for "inlin". For instance, there is one that seems to allow you to increase the upper size threshold for an inlined method body.

like image 32
Stephen C Avatar answered Dec 12 '22 06:12

Stephen C