multiline strings and multiline comments ?

4,899 views
Skip to first unread message

faenvie

unread,
Aug 16, 2010, 3:34:03 AM8/16/10
to Clojure
hi clojure-users,

i wonder what the reason is, that clojure(-reader)
does not allow

1. multiline-strings like scala:

"""this is a
| multiline string"""

2. multiline comments like java

/* this is a
multiline comment */

someone who can explain or point
to relevant info ?

thanks

Michael Wood

unread,
Aug 16, 2010, 9:40:44 AM8/16/10
to clo...@googlegroups.com
On 16 August 2010 09:34, faenvie <fanny....@gmx.de> wrote:
> hi clojure-users,
>
> i wonder what the reason is, that clojure(-reader)
> does not allow
>
> 1. multiline-strings like scala:
>
>  """this is a
>   | multiline string"""

user=> (def s "This is a
multiline string")
#'user/s
user=> s
"This is a\nmultiline string"
user=>

> 2. multiline comments like java
>
> /* this is a
>   multiline comment */

I don't know.

> someone who can explain or point
> to relevant info ?

--
Michael Wood <esio...@gmail.com>

Rasmus Svensson

unread,
Aug 16, 2010, 10:33:25 AM8/16/10
to clo...@googlegroups.com
>> 2. multiline comments like java
>>
>> /* this is a
>>   multiline comment */
>
> I don't know.
>

Comment blocks are usually done by starting each line with ;;

;; this is
;; a comment
;; block
(some-code)

Anything after a ; is a comment, like python's #. There is a
convention for how many ;s to begin the line with. (Basically ; for
same line comments, ;; for comments above code and ;;; for top-level
comments not commenting on the form below)

There is also the (comment ...) form that disregards the containing
forms. Note that the contents has be well-formed clojure code
(matching parentheses, etc).

(comment ; Usage examples
(foo 1 2 3)
(bar :a :b :c))

// raek

Rasmus Svensson

unread,
Aug 16, 2010, 10:48:31 AM8/16/10
to clo...@googlegroups.com
2010/8/16 Rasmus Svensson <ra...@lysator.liu.se>:

I guess this does not answer the original "why?" question...

Multiline string literals, as showed above, do exist, but do not have
their own syntax. (Also, leading whitespace is not stripped from the
lines.)

Clojure follows other traditions for comments than, for example, Java.
Clojure uses Lisp-style comments, which has followed a separate and
parallel path, but also has the #_ "ignore next form" reader macro and
the comment macro I mentioned before.

// raek

B Smith-Mannschott

unread,
Aug 16, 2010, 10:49:18 AM8/16/10
to clo...@googlegroups.com

There's also the reader-macro #_, which causes the next form to be
ignored. If you were feeling especially perverse, you could use it to
simulate a mult-line comment:

#_"I have an editor which is too simple to help me
with comments and my right pinky is too tired to type
all those semicolons."

;-)

// Ben

B Smith-Mannschott

unread,
Aug 16, 2010, 12:09:49 PM8/16/10
to clo...@googlegroups.com
On Mon, Aug 16, 2010 at 16:48, Rasmus Svensson <ra...@lysator.liu.se> wrote:
> 2010/8/16 Rasmus Svensson <ra...@lysator.liu.se>:
>>>> 2. multiline comments like java
>>>>
>>>> /* this is a
>>>>   multiline comment */
>>>
>>> I don't know.
>>>
>>
>> Comment blocks are usually done by starting each line with ;;
>>
>> ;; this is
>> ;; a comment
>> ;; block
>> (some-code)
>>
>> Anything after a ; is a comment, like python's #. There is a
>> convention for how many ;s to begin the line with. (Basically ; for
>> same line comments, ;; for comments above code and ;;; for top-level
>> comments not commenting on the form below)
>>
>> There is also the (comment ...) form that disregards the containing
>> forms. Note that the contents has be well-formed clojure code
>> (matching parentheses, etc).
>>
>> (comment ; Usage examples
>>  (foo 1 2 3)
>>  (bar :a :b :c))
>>
>> // raek
>>
>
> I guess this does not answer the original "why?" question...

Tradition?

> Multiline string literals, as showed above, do exist, but do not have
> their own syntax. (Also, leading whitespace is not stripped from the
> lines.)

nor in python. a macro to do that on string literals would be neat.

> Clojure follows other traditions for comments than, for example, Java.
> Clojure uses Lisp-style comments, which has followed a separate and
> parallel path, but also has the #_ "ignore next form" reader macro and
> the comment macro I mentioned before.
>
> // raek
>

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Grayswx

unread,
Aug 16, 2010, 10:02:03 AM8/16/10
to Clojure
For comments, there's (comment This is a comment ...). I'd say that
eliminates the need for /* ... */.

Luka Stojanovic

unread,
Aug 16, 2010, 9:57:37 AM8/16/10
to clo...@googlegroups.com

>>
>> /* this is a
>> multiline comment */
>

(comment This is a
multiline comment?)

Andrew Gwozdziewycz

unread,
Aug 16, 2010, 12:26:17 PM8/16/10
to clo...@googlegroups.com
On Mon, Aug 16, 2010 at 3:34 AM, faenvie <fanny....@gmx.de> wrote:
> hi clojure-users,
>
> i wonder what the reason is, that clojure(-reader)
> does not allow
>
> 1. multiline-strings like scala:
>
>  """this is a
>   | multiline string"""

Yes. It does, if you plug this into your repl (

user=> (use '[clojure.contrib.str-utils2 :only [split-lines join]])
user=> (defn ml-str
"Scala style multiline string"
[s]
(let [l (split-lines s)]
(join "\n" (map (fn [b]
(let [i (.indexOf b "|")]
(if (> i 0)
(.substring b (inc i))
b))) l))))
#'user/ml-str
user=> (ml-str "Hello World
|the margin will
|be deleted as per
|the request of scala
|users")
"Hello World\nthe margin will\nbe deleted as per\nthe request of scala \nusers"
user=>

--
http://www.apgwoz.com

Lee Spector

unread,
Aug 16, 2010, 3:13:03 PM8/16/10
to clo...@googlegroups.com

On Aug 16, 2010, at 10:02 AM, Grayswx wrote:

> For comments, there's (comment This is a comment ...). I'd say that
> eliminates the need for /* ... */.

Except that with (comment ...) the stuff in ... has to have balanced parentheses (& possibly meet other constraints since it's being parsed?).

I'm finding that ;, #_, and (comment ...) generally suffice but every once in a while I do miss having block comments (like /* ... */, or Common Lisp's #| ... |#), e.g. when I want to comment out a big chunk of partially-written code that may not be balanced, or when I have a real comment that includes partial bits of code that may not be balanced.

-Lee

--
Lee Spector, Professor of Computer Science
School of Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspe...@hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438

Check out Genetic Programming and Evolvable Machines:
http://www.springer.com/10710 - http://gpemjournal.blogspot.com/

B Smith-Mannschott

unread,
Aug 16, 2010, 3:58:34 PM8/16/10
to clo...@googlegroups.com
On Mon, Aug 16, 2010 at 18:26, Andrew Gwozdziewycz <apg...@gmail.com> wrote:
> On Mon, Aug 16, 2010 at 3:34 AM, faenvie <fanny....@gmx.de> wrote:
>> hi clojure-users,
>>
>> i wonder what the reason is, that clojure(-reader)
>> does not allow
>>
>> 1. multiline-strings like scala:
>>
>>  """this is a
>>   | multiline string"""
>
> Yes. It does, if you plug this into your repl (
>
> user=> (use '[clojure.contrib.str-utils2 :only [split-lines join]])
> user=> (defn ml-str
>  "Scala style multiline string"
>  [s]
>  (let [l (split-lines s)]
>   (join "\n" (map (fn [b]
>                    (let [i (.indexOf b "|")]
>                     (if (> i 0)
>                      (.substring b (inc i))
>                      b))) l))))
> #'user/ml-str
> user=> (ml-str "Hello Wo
>   |the margin will
>   |be deleted as per
>   |the request of scala
>   |users")
> "Hello World\nthe margin will\nbe deleted as per\nthe request of scala \nusers"
> user=>

this, and my earlier rhetorical question about having this kind of
thing as a macro as produced: http://gist.github.com/527621

// Ben

Meikel Brandmeyer

unread,
Aug 16, 2010, 4:13:08 PM8/16/10
to clo...@googlegroups.com
Hi,

Am 16.08.2010 um 21:13 schrieb Lee Spector:

> I'm finding that ;, #_, and (comment ...) generally suffice but every once in a while I do miss having block comments (like /* ... */, or Common Lisp's #| ... |#), e.g. when I want to comment out a big chunk of partially-written code that may not be balanced, or when I have a real comment that includes partial bits of code that may not be balanced.

Every descent editor should provide a comment-selected-text functionality. So whether multiline comments or single comments are used should be an implementation detail. You also have to get (* *) (OCaml) nested and with /* */ (C) you are in trouble anyway because they can't nest. So I don't think that multiline comments are so much better without editor support. (I wrote a plugin for Vim which does the required escaping. Believe me: Single comments are much better!)

Sincerely
Meikel

Lee Spector

unread,
Aug 16, 2010, 5:14:58 PM8/16/10
to clo...@googlegroups.com

On Aug 16, 2010, at 4:13 PM, Meikel Brandmeyer wrote:
> Every descent editor should provide a comment-selected-text functionality. So whether multiline comments or single comments are used should be an implementation detail. You also have to get (* *) (OCaml) nested and with /* */ (C) you are in trouble anyway because they can't nest. So I don't think that multiline comments are so much better without editor support. (I wrote a plugin for Vim which does the required escaping. Believe me: Single comments are much better!)

Yeah, I realize there are problems with nesting and so in CL I generally use #|...|# only to comment out big blocks, and ; for everything else.

I've worked in several editors that have comment-selected-text, but I don't see it in Eclipse/Counterclockwise, which is what I'm using now for Clojure -- does anyone know if it's there and I'm just missing it?

Not that I think this is any kind of major issue, but as I said I do sometimes miss syntax-ignoring block comments (or comment-selected-text, which I agree is just as good and maybe better sometimes).

Zmitro Lapcjonak

unread,
Aug 18, 2010, 4:40:39 AM8/18/10
to Clojure
On Aug 17, 12:14 am, Lee Spector <lspec...@hampshire.edu> wrote:
> On Aug 16, 2010, at 4:13 PM, Meikel Brandmeyer wrote:
>
> > Every descent editor should provide a comment-selected-text functionality.

> I've worked in several editors that have comment-selected-text, but I don't see it in Eclipse/Counterclockwise

I've created issue: "comment multiple lines by pressing Ctrl+/"
http://code.google.com/p/counterclockwise/issues/detail?id=61

Still under development.

--
Zmi La

Daniel Kwiecinski

unread,
Jun 29, 2013, 7:14:49 AM6/29/13
to clo...@googlegroups.com
(clojure.string/join "\n" [
       "this"
       "is"
       "multiline"
       "sting"
])

;produces "this\nis\nmultiline\nsting"

;)

Niels van Klaveren

unread,
Jun 29, 2013, 7:48:19 AM6/29/13
to clo...@googlegroups.com
In my version of CCW CTRL-/ multiline (un)commenting just works (under Windows).
Perhaps it's a keyboard shortcut problem ?

Only problem there is when multiple lines are selected, and some are uncommented, uncommenting the whole block doesn't work.

Laurent PETIT

unread,
Jul 5, 2013, 8:17:42 AM7/5/13
to clo...@googlegroups.com
2013/6/29 Niels van Klaveren <niels.va...@gmail.com>:
> In my version of CCW CTRL-/ multiline (un)commenting just works (under
> Windows).
> Perhaps it's a keyboard shortcut problem ?
>
> Only problem there is when multiple lines are selected, and some are
> uncommented, uncommenting the whole block doesn't work.

Right, but what should be done, then? The command is a "toggle", but
there are both commented and uncommented lines: should it comment all
lines, or uncomment all lines?

>
>
> On Wednesday, August 18, 2010 10:40:39 AM UTC+2, Zmitro Lapcjonak wrote:
>>
>> On Aug 17, 12:14 am, Lee Spector <lspec...@hampshire.edu> wrote:
>> > On Aug 16, 2010, at 4:13 PM, Meikel Brandmeyer wrote:
>> >
>> > > Every descent editor should provide a comment-selected-text
>> > > functionality.
>>
>> > I've worked in several editors that have comment-selected-text, but I
>> > don't see it in Eclipse/Counterclockwise
>>
>> I've created issue: "comment multiple lines by pressing Ctrl+/"
>> http://code.google.com/p/counterclockwise/issues/detail?id=61
>>
>> Still under development.
>>
>> --
>> Zmi La
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Message has been deleted

Niels van Klaveren

unread,
Jul 5, 2013, 11:16:53 AM7/5/13
to clo...@googlegroups.com

Right, but what should be done, then? The command is a "toggle", but
there are both commented and uncommented lines: should it comment all
lines, or uncomment all lines?

I understand why the function works like it does, and there's no 'right' solution to this problem.
I just posted this to inform Zmitro that multiline commenting should work normally in CCW, except in this specific case.

Laurent PETIT

unread,
Jul 5, 2013, 11:27:42 AM7/5/13
to clo...@googlegroups.com
2013/7/5 Niels van Klaveren <niels.va...@gmail.com>:
Ok, sorry then.

In some configurations, one would maybe have to rebind the keyboard
shortcuts to make it work (e.g. keyboards where reaching the key would
imply also typing Shift, etc.)

Michael Wood

unread,
Jul 5, 2013, 12:01:43 PM7/5/13
to clo...@googlegroups.com
On 5 July 2013 14:17, Laurent PETIT <lauren...@gmail.com> wrote:
2013/6/29 Niels van Klaveren <niels.va...@gmail.com>:
> In my version of CCW CTRL-/ multiline (un)commenting just works (under
> Windows).
> Perhaps it's a keyboard shortcut problem ?
>
> Only problem there is when multiple lines are selected, and some are
> uncommented, uncommenting the whole block doesn't work.

Right, but what should be done, then? The command is a "toggle", but
there are both commented and uncommented lines: should it comment all
lines, or uncomment all lines?

Well, there are four reasonable-ish possibilities:

1.)  Do nothing, because it is ambiguous.

2.)  Uncomment all lines.

3.)  Comment all lines.

4.)  Uncomment all commented lines and comment all uncommented lines.

I think that doing either 2 or 3 would be more useful than 1 or 4, but since I don't use Eclipse it doesn't matter to me :)

--
Michael Wood <esio...@gmail.com>

Laurent PETIT

unread,
Jul 5, 2013, 1:03:48 PM7/5/13
to clo...@googlegroups.com
2013/7/5 Michael Wood <esio...@gmail.com>:
Well, maybe comment all lines then, because it's invertible. Ctrl + /
would add a ; in front on every line. And if it's not what I'd like to
do, no problem, Ctrl + / again would remove the ;

Nice google account, btw ;-)

>
> --
> Michael Wood <esio...@gmail.com>
Reply all
Reply to author
Forward
0 new messages