Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string for-loop interpolation with "separator" feature requested #2058

Open
jurgenvinju opened this issue Oct 22, 2024 · 1 comment
Open

Comments

@jurgenvinju
Copy link
Member

jurgenvinju commented Oct 22, 2024

It currently requires more unquoting and quoting than desirable to
generate source code that uses separators.

In the following code the newlines are separators. If they would not be,
there will be superfluous newlines generated which is not nice.

extends = [trim("<i>") | i <- h.imports, i is \extend];
    imports = [trim("<i>") | i <- h.imports, i is \default];
    grammar = [trim("<i>") | i <- h.imports, i is \syntax];

    newHeader = "<for (i <- sort(extends)) {><i>
                '<}>"[..-1] +
                "<if (extends != []) {>
                '
                '<}><for (i <- sort(imports)) {><i>
                '<}>"[..-1] +
                "<if (imports != [] && grammar != []) {>
                '
                '<}><for (i <- grammar) {><i>
                '<}>"[..-1];

If the for loop could know it was generating separated lists instead of a normal lists, then we would not have to remove the trailing newlines with [..-1] and unquote and quote again after every block, and we would not have to test for emptiness of predecessors:

    extends = [trim("<i>") | i <- h.imports, i is \extend];
    imports = [trim("<i>") | i <- h.imports, i is \default];
    grammar = [trim("<i>") | i <- h.imports, i is \syntax];

    newHeader = "<for-sep (i <- sort(extends)) {><i>
                '<}>
                '<for-sep (i <- sort(imports)) {><i>
                '<}>
                '<for-sep (i <- grammar) {><i>
                '<}>";

Where the literal characters after the last interpolated element would not be printed because that would not be a separator.
This code is much cleaner.

The name for-sep is up for debate of course. Alternatives welcome.

@jurgenvinju
Copy link
Member Author

another option would be to wrap the separator: <sep {> <}>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant