Bard keyword syntax

40 views
Skip to first unread message

James Reeves

unread,
Feb 11, 2010, 12:30:50 PM2/11/10
to Clojure Dev
I recently ran across a blog post on a new Lisp variant called Bard
(http://bywicket.com/users/mikel/weblog/126a4/).

One interesting piece of syntax is that Bard not only uses the normal
":foo" keyword syntax, but also allows the colon to be placed at the
end, e.g. "foo:". So both ":foo" and "foo:" are syntactically
equivalent.

This seems an nice piece of syntax sugar, especially for maps and
keyword functions:

(def example-user
{name: "Bob"
email: "b...@example.com"})

(find-user-by name: "Bob")

I'm not sure how much value this adds, but I'd be interested in
people's opinion on this syntax.

- James

Chouser

unread,
Feb 11, 2010, 12:47:36 PM2/11/10
to cloju...@googlegroups.com

You want opinions? I've got opinions... ;-)

I can see why a lisp would choose to do this, and there may have
been a time when I would even have supported it for Clojure. But
here are some questions that inform my initial reaction to this
idea:

1. How much pain, trouble, or learning difficult is caused by
having only Clojure's current keyword syntax? ...pain that
would be removed by supporting the postfix colon?

2. How many different ways do you want to be able to say the same
thing with *no* difference in meaning? Clojure already
supports more or less random placement of commas -- how much
is this used? Ruby APIs have lots of synomoym methods -- how
much benefit does this provide?

3. Postfix colon makes those map literals look so much like JSON
(presumably this is part of the attraction), but is this
helpful or is it perhaps misleading. {foo: "bar"} works, but
what about {"foo": "bar"}? {"my key": "val"}?

4. Clojure keywords support namespacing. Would this be supported
by the new syntax as well? :foo <- foo: is fine.
:myns/foo <- myns/foo: looks okay.
What about ::foo <- foo:: or ::foo <- :foo: ?

For me it really comes down to question 1 above. Does anyone
have enough difficulty learning or using the current syntax that
it's worth trying to answer Q's 2 through 4?

--Chouser
http://joyofclojure.com/

James Reeves

unread,
Feb 11, 2010, 3:07:55 PM2/11/10
to Clojure Dev
On Feb 11, 5:47 pm, Chouser <chou...@gmail.com> wrote:
> 1. How much pain, trouble, or learning difficult is caused by
>    having only Clojure's current keyword syntax?  ...pain that
>    would be removed by supporting the postfix colon?

Admittedly not much :)

> 2. How many different ways do you want to be able to say the same
>    thing with *no* difference in meaning?  Clojure already
>    supports more or less random placement of commas -- how much
>    is this used?  Ruby APIs have lots of synomoym methods -- how
>    much benefit does this provide?

I use commas quite a bit in Clojure code. They're a nice way of
separating connected forms without requiring a newline, e.g.

{:x 1, :y 2}

(let [x 1, y 2] ...)

(defn foo [#^String x, #^InputStream y] ...)

Clojure also allows keywords to be used in prefix or postfix notation
when accessing map data. Clojure doesn't seem to follow the "there's
one right way to do it" philosophy that, say, Python does.

On the other hand, I don't like Ruby's synonym methods very much. It's
one thing to replace ":foo" with "foo:", as you can see they're the
same thing. But "collect" and "map" have no obvious connection,
despite being two names for the same function.

> 3. Postfix colon makes those map literals look so much like JSON
>    (presumably this is part of the attraction), but is this
>    helpful or is it perhaps misleading.  {foo: "bar"} works, but
>    what about {"foo": "bar"}?  {"my key": "val"}?

Yes, that's a valid concern, I think, but I don't think it's any more
misleading than the Lisp ' syntax.

> 4. Clojure keywords support namespacing.  Would this be supported
>    by the new syntax as well?  :foo <- foo: is fine.
>    :myns/foo <- myns/foo: looks okay.
>    What about ::foo <- foo:: or ::foo <- :foo: ?

"foo::" seems most natural, in my opinion.

> For me it really comes down to question 1 above.  Does anyone
> have enough difficulty learning or using the current syntax that
> it's worth trying to answer Q's 2 through 4?

The only real merit to this syntax is readability, though I think
that's pretty important on its own. It seems to me like its a
relatively minor syntax change that would allow developers to write
slightly clearer code for relatively little syntax cost. It might also
encourage more use of keyword arguments in functions, which I rather
like.

That said, I don't feel particularly strongly about the syntax :). I'm
certainly not going to lose any sleep over (find-by :name "Bob"),
instead of (find-by name: "Bob").

- James

Chas Emerick

unread,
Feb 11, 2010, 3:22:13 PM2/11/10
to cloju...@googlegroups.com

On Feb 11, 2010, at 3:07 PM, James Reeves wrote:

>> For me it really comes down to question 1 above. Does anyone
>> have enough difficulty learning or using the current syntax that
>> it's worth trying to answer Q's 2 through 4?
>
> The only real merit to this syntax is readability, though I think
> that's pretty important on its own. It seems to me like its a
> relatively minor syntax change that would allow developers to write
> slightly clearer code for relatively little syntax cost. It might also
> encourage more use of keyword arguments in functions, which I rather
> like.

That's far more compelling than the map-key usage IMO. If keyword arg
processing becomes a commonly-available defn variant (which has been
rumored, anyway?), then postfix colons on keywords make tons of sense.

- Chas

Richard Newman

unread,
Feb 11, 2010, 3:23:50 PM2/11/10
to cloju...@googlegroups.com
> The only real merit to this syntax is readability, though I think
> that's pretty important on its own.

I don't think colon-comes-last is in any way more readable.

Also, I don't think this has been mentioned yet:

In JSON, ":" is a syntax element: the dictionary separator. In
Clojure, ":" is part of the keyword. Putting the colon last would drop
things into the "similar yet different" valley: I suspect that any
programmer who can't get their head around "leading colon means
keyword" would do silly things like trying to treat colons as map
separators with string keys, etc., so this wouldn't help anyone.

I find it frustrating to write JSON:

{"foo": ["bar", "baz"]}

because I don't see the point in the extra syntax. (I also delete
commas from Clojure source code wherever I find them: they're a waste
of bits.)

I also disagree with:

> It seems to me like its a
> relatively minor syntax change that would allow developers to write
> slightly clearer code for relatively little syntax cost.

I personally prefer :foo to foo: -- I don't think foo: is "clearer" in
any way -- so phrasing this in objective terms ("clearer code") is a
little inaccurate. "Colon comes last is clearer" is in the exact same
bucket as "you should support infix math notation because it's more
intuitive". At least to this Lisper, leading colon is clearer.

It's also got some reasonable advantages: with the leading colon I
know from the very first character that it's a keyword, and my brain
can parse it accordingly. If the colon comes last, this could be a
regular symbol, the start of a namespace, or a keyword, and I need to
look further ahead.

Chouser

unread,
Feb 11, 2010, 4:40:49 PM2/11/10
to cloju...@googlegroups.com
On Thu, Feb 11, 2010 at 3:07 PM, James Reeves
<weave...@googlemail.com> wrote:
>
> I use commas quite a bit in Clojure code. They're a nice way of
> separating connected forms without requiring a newline, e.g.
>
>  {:x 1, :y 2}
>
>  (let [x 1, y 2] ...)
>
>  (defn foo [#^String x, #^InputStream y] ...)
>
> Clojure also allows keywords to be used in prefix or postfix notation
> when accessing map data. Clojure doesn't seem to follow the "there's
> one right way to do it" philosophy that, say, Python does.
>
> On the other hand, I don't like Ruby's synonym methods very much. It's
> one thing to replace ":foo" with "foo:", as you can see they're the
> same thing. But "collect" and "map" have no obvious connection,
> despite being two names for the same function.

You're right, these are imporant distictions. I came to hate the
synonyms in Ruby libs, but I do tend to use commas where I think
they're helpful.

That said, I think I'm content leaving Clojure keywords the way
they are. :-)

--Chouser
http://joyofclojure.com/

Sean Devlin

unread,
Feb 11, 2010, 4:48:05 PM2/11/10
to Clojure Dev
It's an interesting idea, and looking at new ways of doing things
always help. In this case, though, I agree w/Chouser. If it ain't
broke...

Sean

Chas Emerick

unread,
Feb 11, 2010, 4:49:59 PM2/11/10
to cloju...@googlegroups.com

On Feb 11, 2010, at 3:23 PM, Richard Newman wrote:

> In JSON, ":" is a syntax element: the dictionary separator. In
> Clojure, ":" is part of the keyword. Putting the colon last would
> drop things into the "similar yet different" valley: I suspect that
> any programmer who can't get their head around "leading colon means
> keyword" would do silly things like trying to treat colons as map
> separators with string keys, etc., so this wouldn't help anyone.

Well put. The "similar yet different" phrase reminded me of
Applescript. :-x

The point about prefixed colons (and really, most things in the
language being recognizable from the first character) is a big
advantage.

- Chas

Timothy Pratley

unread,
Feb 11, 2010, 6:39:13 PM2/11/10
to cloju...@googlegroups.com
On 12 February 2010 06:23, Richard Newman <holy...@gmail.com> wrote:
> In JSON, ":" is a syntax element: the dictionary separator. In Clojure, ":"
> is part of the keyword. Putting the colon last would drop things into the
> "similar yet different" valley: I suspect that any programmer who can't get
> their head around "leading colon means keyword" would do silly things like
> trying to treat colons as map separators with string keys, etc., so this
> wouldn't help anyone.

Yeah, one temptation would be to think of : as an operator or a
separator. Having two options might conflating the keyword concept
with syntax. So although it looks pretty my opinion is (dec).

James Reeves

unread,
Feb 12, 2010, 8:09:16 AM2/12/10
to Clojure Dev
On Feb 11, 8:23 pm, Richard Newman <holyg...@gmail.com> wrote:
> (I also delete commas from Clojure source code wherever I find them:
> they're a waste of bits.)

No more so than newlines and indentation. They waste less screen space
than newlines, too.

> I personally prefer :foo to foo: -- I don't think foo: is "clearer" in  
> any way -- so phrasing this in objective terms ("clearer code") is a  
> little inaccurate. "Colon comes last is clearer" is in the exact same  
> bucket as "you should support infix math notation because it's more  
> intuitive". At least to this Lisper, leading colon is clearer.

This is a good point. Macros like -> and ->> muddy the waters a
little, but Clojure is primarily prefix-based.

> It's also got some reasonable advantages: with the leading colon I  
> know from the very first character that it's a keyword, and my brain  
> can parse it accordingly. If the colon comes last, this could be a  
> regular symbol, the start of a namespace, or a keyword, and I need to  
> look further ahead.

Again, a good point.

I think I'm convinced by the arguments against adopting this syntax.

- James

Richard Newman

unread,
Feb 12, 2010, 4:59:26 PM2/12/10
to cloju...@googlegroups.com
>> (I also delete commas from Clojure source code wherever I find them:
>> they're a waste of bits.)
>
> No more so than newlines and indentation. They waste less screen space
> than newlines, too.

I find that newlines and indentation makes it easier for me to read
the code; not so for commas, which just distract me. I like my
unadorned tokens :)

Reply all
Reply to author
Forward
0 new messages