Forums

Multiline strings

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Re: Multiline strings

Postby todd.a » Mon Sep 28, 2009 2:03 pm

I really like Chuck's idea for this one. I can't tell you how much I hate HEREDOCs in PHP, Ruby, and any language that have them. It breaks the look and feel of the code by requiring you to put text in the first column. I am also against adding extra cruft to the text like | (this is what they do in Scala and is used with .stripMargins).

Code: Select all
<?php
// this is fugly. :)
class Foo {
  public static function useUglyCode() {
    $email = <<<EMAIL
Dear Mr. Mister,
...
...
EMAIL;
  }
}


The question here is what is the most common use case?
Like others mentioned you often paste multiline strings into your code. I use them a lot for SQL and emails templates and here's what looks good to me. This is without keeping any parsing challenges in mind.
Code: Select all
class Foo
  def main
    email = """
    Dear Lovely String,
    How are y...
      Here's a new paragraph with an indent :)
    """
   
    # here's how nice you can write SQL so you don't have to use an unnecessary ORM :)
    # SQL is powerful, use it or lose it LOL
    sql = """
    SELECT a.foo, b.bar
    FROM alpha a
    LEFT JOIN beta b ON a.id = b.alpha_id
    WHERE a.cost > 20
    ORDER BY a.pro DESC
    """

    print email, sql


Given this, additional processing, like hopscc suggested, is also needed. Not sure if I like the +| syntax but then again we all got used to string formats in C# and Java right?

@hopscc is that JSON string from the JSON parser? LOL. That's actually a good use case.
Last edited by todd.a on Thu Oct 01, 2009 12:17 am, edited 1 time in total.
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Re: Multiline strings

Postby hopscc » Mon Sep 28, 2009 6:26 pm

Working backwards:
Yes the example is the JSON string from the JSON parser - (pathological) its what prodded me to finally do something around this

I think we all have some sort of agreement on allowing some form of indentation (of content within multiline strings) for indentation alignment with surrounding code
and automatic removal of such indentation up to a particular level.

The cut+paste of literal text for templates or commands is probably a major use (perhaps also various markup forms (XML/HTML)).
Its probably cut+paste+edit so far as the coding operations go so as to obtain pleasant appearing indentation (your editor may or may not do the indentation for you)

My preference (obviously) would be to indent the multiline content an extra level just to separate it ( and highlight its difference) from code
(hence thats also where I implemented the default auto-indentation removal)

Your examples then look like this which to my eye is cleaner and easier to read YMMV
Code: Select all
    class Foo
      def main
        email = """
            Dear Lovely String,
            How are y...
                Here's a new paragraph with an indent :)
        """
       
        # here's how nice you can write SQL so you don't have to use an unnecessary ORM :)
        # SQL is powerful, use it or lose it LOL
        sql = """
            SELECT a.foo, b.bar
            FROM alpha a
            LEFT JOIN beta b ON a.id = b.alpha_id
            WHERE a.cost > 20
            ORDER BY a.prostitute DESC
        """

        print email, sql

( mine would use R" instead of """ ( or r""" perhaps)).


This seems to be a point that keeps being missed so I'll repeat it again;
The :shock: 'additional cruft '('|') is optional - You can use it if you want to clarify exactly where your whitespace is going to be or you like the look,
you dont have to use it, It doesn't cost if you dont use it, it doesnt cost (much) if you do use it (some additional formatting).

being optional it needs enabling which is the '+|' as patchImplemented...
I dont really have much preference for what might be used to do this so long as its not particularly verbose and reasonable mnemonic

Apply the patch - experiment with it for real ( try the kool-aid - its real good ;) )
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Multiline strings

Postby todd.a » Mon Sep 28, 2009 6:37 pm

I don't mind the extra indentation level. It's along the same lines. Where did you submit the patch. I'd like to take it for a spin in real code to see how good the KoolAid really is :)

Regarding the JSON string: when transmitting it over the wire you don't really care about leaving it pretty as you can save some bytes by cramming everything together. It doesn't seem like much but when offering web services it adds up quickly.
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Re: Multiline strings

Postby hopscc » Mon Sep 28, 2009 6:52 pm

patch is on ticket:120

re the JSON string Probably so but I'd like to at least start with something humanly readable (and compress it down as/if it goes on the wire)
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Multiline strings

Postby todd.a » Tue Sep 29, 2009 11:02 pm

Seems like because it was 5 weeks ago I had 5 hunks fail. If you have it for the latest revision can you attach a patch file against it?
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Re: Multiline strings

Postby hopscc » Wed Sep 30, 2009 5:41 am

I got this when run bigraw.patch patchfile against my copy of latest source tree
Code: Select all
> patchTst ../wkspace/bigraw.patch
patching file Source/CobraTokenizer.cobra
patching file Source/CobraParser.cobra
Hunk #1 succeeded at 2808 (offset 26 lines).
Hunk #2 succeeded at 3302 (offset 26 lines).
patching file Tests/100-basics/064-2-string-bigraw-bounded.cobra
patching file Tests/100-basics/064-2-string-bigraw.cobra
patching file Developer/IntermediateReleaseNotes.text
Hunk #1 succeeded at 53 with fuzz 2 (offset 5 lines).
>


check that the patch file and your src tree files both have the same convention for EOL
( both \n or both \r\n depending on platform) - I suspect the patchfile is at variance with (at least) some of the files on whatever your platform is.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Multiline strings

Postby hopscc » Wed Sep 30, 2009 5:43 am

Heres the patchfile again anyway
Attachments
bigraw.patch
(15.28 KiB) Downloaded 633 times
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Multiline strings

Postby todd.a » Wed Sep 30, 2009 2:32 pm

I used
Code: Select all
$ patch -p 0 -i bigraw.patch


I'm not familiar with applying patches.
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Re: Multiline strings

Postby hopscc » Thu Oct 01, 2009 12:40 am

My apologies, 'patchTst' is an alias I use for patch and a set of options
Code: Select all
alias patchTst='patch --dry-run -p0 -i'


You cmdline should have worked fine...
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Multiline strings

Postby todd.a » Fri Oct 02, 2009 1:51 pm

The output I get is
Code: Select all
$ patch -p0 -i bigraw.patch
patching file Source/CobraTokenizer.cobra
Hunk #1 FAILED at 54.
Hunk #2 FAILED at 73.
Hunk #3 FAILED at 140.
Hunk #4 FAILED at 808.
Hunk #5 FAILED at 853.
5 out of 5 hunks FAILED -- saving rejects to file Source/CobraTokenizer.cobra.rej
patching file Source/CobraParser.cobra
Hunk #1 FAILED at 2782.
Hunk #2 FAILED at 3276.
2 out of 2 hunks FAILED -- saving rejects to file Source/CobraParser.cobra.rej
patching file Tests/100-basics/064-2-string-bigraw-bounded.cobra
patching file Tests/100-basics/064-2-string-bigraw.cobra
patching file Developer/IntermediateReleaseNotes.text
Hunk #1 FAILED at 48.
1 out of 1 hunk FAILED -- saving rejects to file Developer/IntermediateReleaseNotes.text.rej


I am running this against the head revision. Any ideas on why it is doing this?
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

PreviousNext

Return to Discussion

Who is online

Users browsing this forum: No registered users and 24 guests