問題描述
宏很有用.
因此,我偶爾會感嘆 Java 和 C# 中沒有宏.宏允許我強制內聯,但允許我使用非宏代碼的代碼可管理性.
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.
是否有任何基于 Java 或 C# 的項目/產品有效地允許宏或指定內聯擴展.
Is there any Java- or C#-based project/product somewhere out there that effectively allow macros or specifying in-line expansion.
我正在考慮類似的事情
@macro public void hello(int x){ ... }
@macro public void hello(int x){ ... }
或者當我調用一個方法時,調用前的@inline 注釋會影響被調用方法的內聯.
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.
推薦答案
宏不是標準 Java 語言的一部分,我不知道主流 Java 工具、IDE 等是否支持任何宏預處理器.因此,如果您在 Java 代碼中使用宏,您應該會體驗到一些痛苦".例如,
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,
- 源代碼調試器不允許您相對于原始源代碼設置斷點.
- 如果您與宏共享 Java 代碼,許多 Java 開發人員可能會對此嗤之以鼻,和/或抱怨必須安裝/使用額外的工具.
Java 的第三方宏預處理器有很多例子;例如Jatha, OpenJava, PrintMacroJ、JavaMacros 等等...(但你有沒有遇到過使用它們的項目?)
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.
沒錯.但是 JIT 編譯器在確定 應該 內聯的內容方面可能比您做得更好.它會(肯定地)知道這些塊有多大,并且它會有關于執行頻率、分支預測等的運行時統計信息,這些對你來說是不可用的.
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.
請注意,有一些 Hotspot JVM 調優選項會影響優化器的內聯決策;請參閱 此頁面,并掃描inlin".例如,有一個似乎允許您增加內聯方法體的大小上限.
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.
這篇關于是否有 Java 或 C# 的宏工具?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!