j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
175 stars 14 forks source link

multiline character literal constants #306

Open shahmoradi opened 12 months ago

shahmoradi commented 12 months ago

This feature request has resurfaced frequently, but I cannot readily find any related proposals/issues. So, I am requesting again to add specific syntax for multiline character literal constants in the language, much similar to the functionality of Python docstrings. Currently, writing a multiline text is a cumbersome process. For example,

character(:), allocatable :: str
str = &
"This "//new_line("a")//  &
"is "//new_line("a")//  &
"a "//new_line("a")//  &
"multiline "//new_line("a")//  &
"string."

could be written much more cleanly with a new syntax like,

str = `
This 
is 
a 
multiline 
string.`

where the backtick serves as a multiline delimiter that includes the new line characters in the literal constant. This new syntax (or similar) would also allow hassle-free repeated usage of single and double quotation marks ' and " in a string. My primary use case for this facility is the seamless incorporation and migration of hard-coded text and paragraphs in various source codes in multiple different programming languages. Currently, this is practically impossible without some preprocessing of the text. Given the extensive discussions of this feature among users, I hope this request makes it into future committee meetings for discussion and inclusion.

certik commented 12 months ago

Indeed, here is another such example from one of my recent codes: https://github.com/certik/fastGPT/blob/4e70c6a0e9f60a1b3c94f18b32c34a6012de1e7b/driver.f90#L225.

I wonder if we could use """ as in Python. It would not be strictly backwards compatible though.

klausler commented 12 months ago

I wonder if we could use """ as in Python. It would not be strictly backwards compatible though.

It wouldn't be backwards compatible at all -- it's the start of an unterminated character literal constant.

''" ... "'' and ""' ... '"" would work, and their terminators are unlikely to appear in the text. But I suspect that you need something that would allow anything to appear, no matter how unlikely.

wyphan commented 12 months ago

I'd suggest triple backticks ``` like the one used in Markdown for block code.