在 Scala 中注释掉部分代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13631038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Commenting out portions of code in Scala
提问by akauppi
I am looking for a C(++) #if 0-like way of being able to comment out whole pieces of Scala source code, for keeping around experimental or expired code for a while.
我正在寻找一种#if 0类似于C(++)的方式来注释掉整个 Scala 源代码,以便将实验性或过期代码保留一段时间。
I tried out a couple of alternatives and would like to hear what you use, and if you have come up with something better?
我尝试了几种替代方案,想听听您使用的是什么,如果您想出了更好的方法?
// Simply block-marking N lines by '//' is one way...
// <tags> """ anything
My editor makes this easy, but it's not really The Thing. It gets easily mixed with actual one-line comments.
我的编辑器让这一切变得简单,但这并不是真正的事情。它很容易与实际的单行注释混合在一起。
Then I figured there's native XML support, so:
然后我认为有原生 XML 支持,所以:
<!--
... did not work
-->
Wrapping in XML works, unless you have <tags>within the block:
包装在 XML 中有效,除非您<tags>在块中:
class none { val a= <ignore>
...
cannot have //<tags> <here> (not even in end-of-line comments!)
</ignore> }
The same for multi-line strings seems kind of best, but there's an awful lot of boilerplate (not fashionable in Scala) to please the compiler (less if you're doing this within a class or an object):
多行字符串的情况似乎是最好的,但是有很多样板文件(在 Scala 中并不流行)来取悦编译器(如果您在类或对象中执行此操作,则更少):
object none { val ignore= """ This seems like
...
<truly> <anything goes> but three "'s of course
""" }
The 'right' way to do this might be:
做到这一点的“正确”方法可能是:
/***
/*
... works but not properly syntax highlighed in SubEthaEdit (or StackOverflow)
*/
***/
..but that matches the /*and */only, not i.e. /***to ***/. This means the comments within the block need to be balanced. And - the current Scala syntax highlighting mode for SubEthaEdit fails miserably on this.
..but that match the /*and */only, not ie /***to ..but that match the and only, not ie to ***/.. 这意味着需要平衡块内的注释。并且 - SubEthaEdit 当前的 Scala 语法高亮模式在这方面失败了。
As a comparison, Luahas --[==[matching ]==]and so forth. I think I'm spoilt?
作为比较,Lua有--[==[匹配]==]等等。我觉得我被宠坏了?
So - is there some useful trick I'm overseeing?
那么 - 我正在监督一些有用的技巧吗?
回答by Brian Agnew
Why not just make use of your source code control mechanism ? Keep the code separate, check it in as separate files and forget it. I wouldn't want my day-to-day code base cluttered up with this sort of stuff.
为什么不直接使用您的源代码控制机制?将代码分开,将其作为单独的文件签入并忘记它。我不希望我的日常代码库被这类东西弄得乱七八糟。
Note however that if you're not regularly using this code (e.g. in automated tests etc.) it'll suffer from code rot. As soon as you comment out or otherwise shelve this stuff, dependencies will move on and you'll find that before long it just won't link against the existing code base.
但是请注意,如果您不经常使用此代码(例如在自动化测试等中),它将遭受code rot。一旦您注释掉或以其他方式搁置这些内容,依赖项就会继续前进,您会发现不久之后它就不会链接到现有的代码库。
回答by akauppi
I modified the Scala mode's SyntaxDefinition.xmlto support /***...***/style comments.
我修改了 Scala 模式SyntaxDefinition.xml以支持/***...***/样式注释。
This is not the same as the Scala parser's support for nested /*...*/comments, but I didn't find a way to express that for my editor.
这与 Scala 解析器对嵌套/*...*/注释的支持不同,但我没有找到一种方法来为我的编辑器表达这一点。
In case someone wants to do the same, here goes:
如果有人想做同样的事情,这里是:
<!-- AK 30-Nov-2012
-
- The Scala parser handles nested '/*...*/' style comments, but the SEE
- syntax highlighting seems not.
-
- We introduce '/***...***/' style comments (starting with three asterisks
- since JavaDoc uses '/**..*/' style) and deeper levels, to be used for
- blocking out code blocks, even if they contain '/*..*/' comments within.
-
- Note: Original comment handling misses a 'type="comment"' field. Is that vital?
-
- Test: If this works right, the following will be highlighted as a single comment:
- <<
- /***
- */
- ***/ <- green, not black (note: Scala parses these differently; this is just to test the mode)
- <<
-->
<state id="Multilevel Comment AK" color="#236E25" type="comment" font-style="italic">
<begin><regex>/\*\*(?'commentCatch'\*+)</regex></begin>
<end><regex>(?#see-insert-start-group:commentCatch)\*\*/</regex></end>
<import mode="Base" state="EmailAndURLContainerState" keywords-only="yes"/>
</state>
You may also want to add type="comment"to the existing few comment highlight rules. I'm not sure if that is vital (other modes than Scala's do so).
您可能还想添加type="comment"到现有的少数评论突出显示规则中。我不确定这是否至关重要(除 Scala 之外的其他模式也是如此)。
Information on SubEthaEdit modes.
关于SubEthaEdit 模式的信息。
回答by Erik Kaplun
There's one more option you've left out. Commenting of any sort has the downside of disabling syntax highlighting as well as not being included in IDE refactorings (Emacs+Ensime, IDEA, Eclipse, etc) or other code intelligence tools, I therefore prefer the following approach instead:
您还遗漏了另一种选择。任何类型的评论都有禁用语法突出显示以及不包含在 IDE 重构(Emacs+Ensime、IDEA、Eclipse 等)或其他代码智能工具中的缺点,因此我更喜欢以下方法:
def ignore(block: => Any) = ()
def ignoreIf(cond: Boolean)(block: => Any): Unit = if (!cond) block
ignore {
// experimental and/or disabled code
syntaxHighlightingEnabled(true, 3, "foobar")
}
ignoreIf(SomeFeatureEnabled) {
// experimental and conditionally enabled code
syntaxHighlightingEnabled(true, 3, "foobar")
}
回答by william zeitler
I use 'degrees of delete'. (1) Comment out. (2) If it's code I don't need anymore but might find useful later/elsewhere, I have a '.boneyard' folderwhere I toss code fragments (just outside the production source tree) -- surprisingly handy. (3) Just delete it, and rely on source control if it turns out I need it after all.
我使用“删除程度”。(1) 注释掉。(2) 如果它是我不再需要但可能会在以后/其他地方有用的代码,我有一个“.boneyard”文件夹,我可以在其中抛出代码片段(就在生产源树之外)——非常方便。(3) 删除它,如果结果证明我最终需要它,则依靠源代码管理。
回答by Joe
Inspired by the Lua programming language I use:
受我使用的 Lua 编程语言的启发:
def */ = () // noop function, def or val
/*
println("Hello world!")
// more code
*/
to enable the whole code-block simply add one "/" to the "/*", i.e.
要启用整个代码块,只需在“/*”中添加一个“/”,即
def */ = () // noop function
//*
println("Hello world!")
// more code
*/
Now it prints "Hello world!".
现在它打印“Hello world!”。

