Erlang 中的字符串定界
Erlang 中是否有与 Perl 的 """
等效的方法?
我希望能够定义一个充满双引号的相当长的字符串,而无需转义每个单引号。这可能吗?
请随意如果我做错了,请告诉我,
谢谢!
Is there an equivalent to Perl's """
in Erlang?
I'd like to be able to define a pretty long string full of double-quotes without escaping every single one. Is that possible?
Feel free to let me know if I'm Doing It Wrong.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,
基本上没有其他办法可以做到这一点。这里提出的建议可行,但看起来比直接的解决方案更复杂,而且不太清晰。
No.
There is basically no other way of doing it. The suggestions presented here work but seem more complex than the straight forward solution, and less clear.
怎么样:
您可以定义一个宏来缩写atom_to_list以提高可读性。
How about this:
You can define a macro to abbreviate atom_to_list for improved readability.
首先我想说,对于 Erlang 的未来版本来说,拥有这样的字符串引用并不是一个坏主意。但我不会屏住呼吸等待它的到来。 Python 充分利用它们作为“unmunged”多行文档字符串。
如果我对需要转义引号和反斜杠感到非常恼火,我会考虑在编辑器中制定一个转换宏,以便在当前的文本选择上为我完成此操作。或者也许只是用 sed 中的正则表达式来实现它以进行剪切和粘贴。 :)
First I'd like to say that having such string quotation is not a bad idea for future versions of Erlang. But I'm not holding my breathe for it to arrive. Python makes good use of them as "unmunged" multi-line doc-strings.
If I was sufficiently annoyed by needing to escape quotes and backslashes I would look into working out a transforming macro in my editor to do it for me on the current text selection. Or maybe just implementing it with a regexp in sed to cut'n'paste. :)
我相信您最好的选择是将多行双引号完整字符串放入一个单独的文件中,然后使用新文件读取它:read_line,在应用程序启动时连接各行。
或者,如果您想要超级混乱,您可以将其与解析转换结合起来。您可以将字符串放入源代码中,注释掉,然后在调用解析转换时打开源文件,从注释中读出文本,连接并替换。示例:
在解析转换中,您查找以 MY_FAKE_STRING 开头的字符串。当你找到一个时,你打开模块的源代码,并读取行,直到到达完全相同的字符串。然后逐行阅读源代码,直到出现注释,然后将它们连接起来。到达第一个空(或非注释)行,您就得到了您的字符串,您可以返回该字符串而不是假字符串。
I believe your best choice is to put your multi-line doublequote-full string into a separate file, and then read it with the new file:read_line, concatenating the lines at boot-up of your app.
Or if you want to have an über-mess, you can combine this with parse-transforms. You can place your string(s) into the source code, commented out and when the parse transform is invoked, you open up the source file, read out the text from the comments, concatenate and replace. Example:
In your parse transform you look for strings starting with MY_FAKE_STRING. When you find one, you open up your module's source code, and read line's until you reach the very same string. Then read your source line-by-line until comments are coming, and concatenate them. Reaching the first empty (or non-comment) line, you have your string, which you can return instead of the fake string.
将文本保存在单独的文件中
Save your text in separate file