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

Proposal: q:thru/DELIM/ heredocs #282

Open
Xliff opened this issue Jun 1, 2021 · 11 comments
Open

Proposal: q:thru/DELIM/ heredocs #282

Xliff opened this issue Jun 1, 2021 · 11 comments
Labels
language Changes to the Raku Programming Language

Comments

@Xliff
Copy link

Xliff commented Jun 1, 2021

Given that blocks of text with incusive ending delimeters are becoming more polular, why not allow Raku to use these natural ending delimiters in a heredoc?

Consider the VCard...

     BEGIN:VCARD
      LABEL;TYPE=dom;TYPE=home:V1
      LABEL;TYPE=postal,work:V2
      LABEL;TYPE=postal,intl;TYPE=parcel,other:V3
      END:VCARD

...and XML....

```

...HTML and others, it seems only natural that a Raku coder would do:

my $vcard = q:thru/END:VCARD/;
     BEGIN:VCARD
     LABEL;TYPE=dom;TYPE=home:V1
     LABEL;TYPE=postal,work:V2
     LABEL;TYPE=postal,intl;TYPE=parcel,other:V3
     END:VCARD

...and...

 my $xml = q:thru!</root>!;
<root>
  <section>
    <item />
  </section>
</root>

Would this be worth a consideration to add to the language spec?

Thanks to codesections on #raku for a better suggestion for the syntax.

@Xliff Xliff added the language Changes to the Raku Programming Language label Jun 1, 2021
@jnthn
Copy link
Contributor

jnthn commented Jun 1, 2021

In the VCard example, is it an accident (or significant) that the END is less indented than the BEGIN? (Asking because with :to the indent of the delimiter is the amount to remove from preceding lines, and it warns if there is less).

@Xliff
Copy link
Author

Xliff commented Jun 1, 2021

@jnthn,

Actually, that's a bug I hadn't noticed, until now. Will update example. Thanks

@Xliff
Copy link
Author

Xliff commented Jun 1, 2021

Since it was mentioned on IRC, this type of XML would NOT qualify for this heredoc.

<root><section><item ./></section></root>

Since the terminator is not on its own line.

@jnthn
Copy link
Contributor

jnthn commented Jun 1, 2021

... Since the terminator is not on its own line.

Indeed. Also, even in the case where it is, we have to repeat the terminator in two places, which doesn't feel all that ideal. If we want to change the XML tag we're using, we have to remember to update the terminator too, which is not the best bit of ergonomics. Syntax that looks good at rest doesn't always feel good in motion.

One thing I don't see in the proposal is how this addition makes things better. In the negative column:

  • It's not saving on typing, because :to/XML/ + \nXML is 12 chars to type, while :thru!</root>! is 14
  • It isn't making a mistake less likely because it's introducing repetition

In the positive column:

  • It saves a line. It's worth something (especially to folks like me who can't have many of them on screen at once), but is it worth it?

For such naturally terminated languages, when we reach the point where it's easy to add slangs, an alternative would be to parse the VCard or XML as part of the main parse (that is, enter a grammar parsing XML up until the closing tag, then hand back to the main language). That would mean we're rid of any need for terminators and get to check it's valid XML or VCard or whatever along the way.

@Xliff
Copy link
Author

Xliff commented Jun 1, 2021

@jnthn, to address your points:

How does this not save on typing. I hate to say show your math, so let me show mine. Consider:

To address your first point, consider:
a) Current Raku:

q:to/VCARD/;
BEGIN:VCARD
EMAIL;TYPE=home:V1
END:VCARD
VCARD

b) With proposal:

q:thru/END:VCARD/;
BEGIN:VCARD
EMAIL;TYPE=home:V1
END:VCARD

I end up with difference of 0, without the redundant "VCARD".

And the second: It is less likely to make mistakes, because in the event of cut and paste, you are copying text with the delimiter included. No possibility of mistakes, then.

I do understand your willingness to prefer Slangs rather than a core addition, but how many Slangs will Raku need before a core addition is warranted?

Thanks for your comments.

@codesections
Copy link
Contributor

How does this not save on typing. I hate to say show your math, but by mine, 1) adds 2, not 14.

I had that thought at first, too, but then realized @jnthn is right for the XML case:

my $xml = q:thru!</root>!;
<root>
 <section>
   <item />
 </section>
</root>

vs

 my $xml = q:to!XML!;
<root>
  <section>
    <item />
  </section>
</root>
XML

The only difference is that the first adds :thru!</root>! (14 chars) and saves both of :to!XML! (8 chars) and \nXML (4 chars). So it doesn't end up saving any. (Here, it actually cost a couple, but that wouldn't always be the case with, say HTML vs </html>)

@Xliff
Copy link
Author

Xliff commented Jun 1, 2021

Ah! Thanks for the clarification, @codesections.

@jnthn
Copy link
Contributor

jnthn commented Jun 1, 2021

show your math

> ":to/XML/".chars + "\nXML".chars # the starter and the terminator
12
> ":thru!</root>!".chars # the starter-only form so no terminator
14

That is, even if I pick a reasonable terminator rather than being deliberately brief. I still type less characters by using the terminator form. Similarly:

> ":to/VCARD/".chars + "\nVCARD".chars
16
> "q:thru/END:VCARD/".chars
17

in the event of cut and paste, you are copying text with the delimiter included

This is a fair point.

No possibility of mistakes, then.

Well, not that mistake. But if I paste a decent amount and the opener is off-screen I might forget that I need to copy-paste the final line of the data into :thru/.../, or think I can type if faster than I can copy-paste it, or figure I'll pre-type it because it will be off screen or I don't want to jump back to do it... I assure you I can find plenty of ways to screw things up. :-)

how many Slangs will Raku need before a core addition is warranted?

I guess in a sense my comment that slangs could be "an alternative" was not the best choice of words, in so far as this needn't be either-or; what you're proposing is a generic language feature, whereas slangs are inherently a specialized solution that, if available, would happen to also address what is wanted here.

@Xliff
Copy link
Author

Xliff commented Jun 1, 2021

@jnthn,

Thanks for your thorough explanation.

My final reason for this discussion is to bring up the fact that since there is a q:to// in core, a q:thru// would be logically consitent and an alternative through with what already exists.

I will still consider working on a PR for final consideration.

Still trying to parse through Perl6/Grammar.nqp though. The rule is clearly marked, however I have still yet to understand it's complete functionality.

Thanks for your time

@niner
Copy link

niner commented Jun 2, 2021 via email

@Xliff
Copy link
Author

Xliff commented Jun 2, 2021

This seems like the proper entry point for a PR, however I don't see much in the way of a regex. Looks like I would have to trace where $*W.herestub_queue is sourced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language Changes to the Raku Programming Language
Projects
None yet
Development

No branches or pull requests

4 participants