【问题标题】:Elixir heredocs closing triple quotes (""") behaviorElixir heredocs 关闭三引号 (""") 行为
【发布时间】:2019-03-25 14:18:50
【问题描述】:

我注意到以下两个调用产生了不同的结果字符串:

# Closing quotes not indented
"""
 a
"""

# Closing quotes indented
"""
 a
 """

第一个调用将返回" a\n",而第二个调用将返回a\n

似乎右引号的缩进级别表示一个点,直到heredoc中每一行的前导空格被截断。如果您有 8 个前导空格和 4 个结束引号缩进,则结果字符串中将有 4 个前导空格。字符和第一个实际字符之后的所有内容都不会被截断。

我在 Elixir 文档中没有找到任何关于该行为的文档。是bug吗?

【问题讨论】:

    标签: elixir


    【解决方案1】:

    我没有找到任何关于此的文档,但这绝对是有意作为主题提交

    允许heredoc根据heredoc结束的位置对齐。

    以前是committed by José Valim on 20 Feb 2012,它在elixir_tokenizer 中包含一个新函数,并带有注释:

    %% Remove spaces from heredoc based on the position of the final quotes.
    

    还有一个类似于你在问题中写的测试用例:

    test :double_quoted_aligned_heredoc do
      assert_equal "foo\nbar\nbar\n", """ <> "bar\n"
      foo
      bar
      """
    end
    

    【讨论】:

    • 很高兴知道,这让我有点困惑 ;-)
    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2014-03-31
    • 2011-03-07
    相关资源
    最近更新 更多