問(wèn)題描述
宏很有用.
因此,我偶爾會(huì)感嘆 Java 和 C# 中沒(méi)有宏.宏允許我強(qiáng)制內(nèi)聯(lián),但允許我使用非宏代碼的代碼可管理性.
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# 的項(xiàng)目/產(chǎn)品有效地允許宏或指定內(nèi)聯(lián)擴(kuò)展.
Is there any Java- or C#-based project/product somewhere out there that effectively allow macros or specifying in-line expansion.
我正在考慮類(lèi)似的事情
@macro public void hello(int x){ ... }
@macro public void hello(int x){ ... }
或者當(dāng)我調(diào)用一個(gè)方法時(shí),調(diào)用前的@inline 注釋會(huì)影響被調(diào)用方法的內(nèi)聯(lián).
or when I call a method, an @inline annotation preceding the call would effect the called-method to be in-lined.
或者,我是否需要知道我應(yīng)該相信編譯器會(huì)為我做出最好的決定,即在最好的分析情況下,它可能會(huì)內(nèi)聯(lián)調(diào)用.
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.
我希望這個(gè)問(wèn)題不會(huì)導(dǎo)致?tīng)?zhēng)論宏的優(yōu)缺點(diǎn)/有用性.
I hope this question will not lead to debating the pro/cons/usefulness of macros.
推薦答案
宏不是標(biāo)準(zhǔn) Java 語(yǔ)言的一部分,我不知道主流 Java 工具、IDE 等是否支持任何宏預(yù)處理器.因此,如果您在 Java 代碼中使用宏,您應(yīng)該會(huì)體驗(yàn)到一些痛苦".例如,
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,
- 源代碼調(diào)試器不允許您相對(duì)于原始源代碼設(shè)置斷點(diǎn).
- 如果您與宏共享 Java 代碼,許多 Java 開(kāi)發(fā)人員可能會(huì)對(duì)此嗤之以鼻,和/或抱怨必須安裝/使用額外的工具.
Java 的第三方宏預(yù)處理器有很多例子;例如Jatha, OpenJava, PrintMacroJ、JavaMacros 等等...(但你有沒(méi)有遇到過(guò)使用它們的項(xiàng)目?)
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?)
宏允許我強(qiáng)制內(nèi)聯(lián),但允許我使用非宏代碼的代碼可管理性.
Macros allow me to force in-line but allow me the code-manageability of non-macro code.
沒(méi)錯(cuò).但是 JIT 編譯器在確定 應(yīng)該 內(nèi)聯(lián)的內(nèi)容方面可能比您做得更好.它會(huì)(肯定地)知道這些塊有多大,并且它會(huì)有關(guān)于執(zhí)行頻率、分支預(yù)測(cè)等的運(yùn)行時(shí)統(tǒng)計(jì)信息,這些對(duì)你來(lái)說(shuō)是不可用的.
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.
請(qǐng)注意,有一些 Hotspot JVM 調(diào)優(yōu)選項(xiàng)會(huì)影響優(yōu)化器的內(nèi)聯(lián)決策;請(qǐng)參閱 此頁(yè)面,并掃描inlin".例如,有一個(gè)似乎允許您增加內(nèi)聯(lián)方法體的大小上限.
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.
這篇關(guān)于是否有 Java 或 C# 的宏工具?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!