Skip to content

heredoc for CS? #81

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

Closed
jots opened this issue Jan 11, 2010 · 9 comments
Closed

heredoc for CS? #81

jots opened this issue Jan 11, 2010 · 9 comments

Comments

@jots
Copy link

jots commented Jan 11, 2010

what do you think of adding heredoc to CS? perhaps like ruby's heredoc
http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc-or.html
I would find it useful, for instance: setting a variable to a chunk of CS source code.

@jashkenas
Copy link
Owner

That's kinda why CS supports multiline-strings (take a look at the bottom of the docs). What would you like to do differently with a here-doc? The fancy left-hand-indentation-thing that you can do with Ruby's <<-EOS ?

@jots
Copy link
Author

jots commented Jan 11, 2010

The biggie is removing the need to escape quotes.

@jashkenas
Copy link
Owner

Ahh. Mind pasting in your use-case here? I think that doing Python-style triple quotes, with left-hand indentation suppressed, would be awfully nice. Combine that with interpolation, and you almost remove the need for JavaScript templates.

@jots
Copy link
Author

jots commented Jan 11, 2010

Oh yeah, add interpolation :-) #{...} syntax?

Use case:
code <<eos
code from http://github.com/creationix/coffeepot/raw/master/lib/lexer.coffee
eos
code from http://github.com/creationix/coffeepot/raw/master/lib/lexer.coffee
CoffeeScript.Lexer.tokenize(code)
I guess the benefit of heredoc vs triple quote is that triplequote would probably appear in the source.

@jashkenas
Copy link
Owner

Alright Jots -- heredocs are now on master. They're triple-quote (either single or double quotes will do) and keep track of their indentation from the left -- so you can use them from any level of code. This:

html: '''
      <strong>
        cup of coffeescript
      </strong>
      '''

Compiles to:

var html;
html = "<strong>\n  cup of coffeescript\n</strong>";

Closing the ticket...

@weepy
Copy link

weepy commented Jan 12, 2010

Here's a bug

bin/coffee -e "html: '''
      <strong>
    cup of coffeescript    
</strong>        
      '''"

gives

  var html;
  html = "<strong>
    cup of coffeescript
</strong>";

@jots
Copy link
Author

jots commented Jan 12, 2010

Thanks for adding this Jeremy! if you ever decide to change it to the normal heredoc syntax, you'll have my vote. I think it's more well known than triple quote (bash, perl, ruby etc.. use "<<") and it allows for triple quote to exist in the string. Still, I do appreciate the triple quote!

@jashkenas
Copy link
Owner

Thanks for the bug report, weepy -- I've changed the way heredocs work to use smallest left-hand indentation within the heredoc as the indentation level (it used to take the indentation level of the first line).

As for the <<EOS syntax, it's pretty unsightly, no?

@weepy
Copy link

weepy commented Jan 13, 2010

re: <<EOS, I don't see any advantage over """

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

No branches or pull requests

3 participants