Skip to content

indented multiline strings using """ ... """ #70

Closed
@StefanKarpinski

Description

@StefanKarpinski

Stolen from Python but with some differences:

function jabberwock()
  """
  'Twas brillig, and the slithy toves
    Did gyre and gimble in the wabe;
  All mimsy were the borogoves,
    And the mome raths outgrabe.
  """
end

julia> print(jabberwock())
'Twas brillig, and the slithy toves
  Did gyre and gimble in the wabe;
All mimsy were the borogoves,
  And the mome raths outgrabe.

The point of the """ construct is to make it easy to embed snippets of text or code in a readable, nice way inside of Julia code. To that end, it is similar to "here documents" (shell, Perl, Ruby) as well as Python's multiline strings, which use the same delimiters. However, a couple of additional semantics make these more pleasant to use:

  • If the initial """ is on a line followed only by whitespace, that whitespace will be stripped, including the newline.
  • Lines following the opening """ token — up to and including the line on which the closing """ occurs — must begin with the same indentation sequence (identical whitespace characters) as the line on which the opening """ occurs.
  • This common indentation sequence will be stripped from each line of the multiline quote before the content is further processed — conceptually, it is part of the surrounding Julia code, rather than part of the quoted string.

After this whitespace stripping is applied, all normal string interpretation is performed as for a " string, including unescaping and interpolation. Moreover, you can prefix """ with an identifier as you can with " strings to invoke macro-based custom-string forms. Thus, a Q""" multiline string has no interpolation performed and r""" is a handy multiline regex literal (maybe the x extended regex format should always be on).

Metadata

Metadata

Assignees

No one assigned

    Labels

    speculativeWhether the change will be implemented is speculative

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions