Welcome! Please see the About page for a little more info on how this works.

+9 votes
in Syntax and reader by

Clojure's PersistentQueue structure has been in the language for quite some time now and has found its way into a fair share of codebases. However, the creation of queues is a two step operation often of the form:

`
(conj clojure.lang.PersistentQueue/EMPTY :a :b :c)

;=> #
`

A better experience might be the following:

`

queue [:a :b :c]

;=> #queue [:a :b :c]

(pop #queue [:a :b :c])

;=> #queue [:b :c]
`

This syntax is proposed and discussed in the Clojure-dev group at https://groups.google.com/forum/?fromgroups#!topic/clojure-dev/GQqus5Wycno

Open question: Should the queue literal's arguments eval? The implications of this are illustrated below:

`
;; non-eval case

queue [1 2 (+ 1 2)]

;=> #queue [1 2 (+ 1 2)]

;; eval case

queue [1 2 (+ 1 2)]

;=> #queue [1 2 3]
`

The answer to this open question will determine the implementation.

20 Answers

0 votes
by

Comment made by: steveminer@gmail.com

I think the non-eval behavior would be consistent with the other reader literals in Clojure 1.4. It's definitely better for interop where some other language implementation could be expected to handle a few literal representations, but not the evaluation of Clojure expressions. Use a regular function if the args need evaluation.

0 votes
by
_Comment made by: cemerick_

The precedent of records seems relevant:


=> (defrecord A [b])
user.A
=> #user.A[(+ 4 5)]
#user.A{:b (+ 4 5)}
=> #user.A{:b (+ 4 5)}
#user.A{:b (+ 4 5)}


This continues to make sense, as otherwise queues would need to print with an extra {{(quote …)}} form around lists — which records neatly avoid:


=> (A. '(+ 4 5))
#user.A{:b (+ 4 5)}


Does this mean that a {{queue}} fn (analogous to {{vector}}, maybe) will also make an appearance?  It'd be handy for HOF usage.
0 votes
by

Comment made by: fogus

Added a patch for the tagged literal support ONLY. This is only one part of the total solution. This provides the read-string and printing capability. I'd like more discussion around the eval side before I get dive into the compiler.

0 votes
by

Comment made by: pmbauer

In addition to Chas' observations on consistency with record literals, would eval in queue literals open up the same security hole as #=, needing to respect read-eval?
When needing eval inside a queue literal, embedding a #= seems more apropos.

0 votes
by

Comment made by: fogus

Evalable queue literal support.

0 votes
by

Comment made by: jafingerhut

Neither of the patches CLJ-976-queue-literal-tagged-parse-support-only.diff dated Apr 27, 2012 nor CLJ-976-queue-literal-eval.diff dated May 4, 2012 apply cleanly to latest master as of May 10, 2012.

0 votes
by

Comment made by: fogus

Updated patch file to merge with latest master.

0 votes
by

Comment made by: fogus

New patch with support fixed for syntax-quote.

0 votes
by

Comment made by: stuart.sierra

Patch does not apply as of commit f5f4faf95051f794c9bfa0315e4457b600c84cef

0 votes
by

Comment made by: fogus

Weird. I was able to download the CLJ-976-queue-literal-eval-and-synquote.diff patch and apply it to HEAD as of just now (f5f4faf95051f794c9bfa0315e4457b600c84cef). There were whitespace warnings, but the patch applied, compiles and passes all tests.

0 votes
by

Comment made by: jafingerhut

With latest head I was able to successfully apply patch CLJ-976-queue-literal-eval-and-synquote.diff with this command:

git am --keep-cr -s < CLJ-976-queue-literal-eval-and-synquote.diff

with some warnings, but successfully applied. If I try it without the --keep-cr option, the patch fails to apply. I believe this is often a sign that either one of the files being patched, or the patch itself, contains CR/LF line endings, which some of the Clojure source files definitely do.

The command above (with --keep-cr) is currently the one recommended for applying patches on page http://dev.clojure.org/display/design/JIRA workflow in section "Screening Tickets". I added the suggested --keep-cr option after running across another patch that applied with the option, but not without it.

0 votes
by

Comment made by: jafingerhut

Presumptuously changing Approval from Incomplete back to Test, since the latest patch does apply cleanly if --keep-cr option is used.

0 votes
by

Comment made by: richhickey

this needs more time

0 votes
by

Comment made by: fogus

Rich,

Do you mind providing a little more detail? I would be happy to make any changes if needed. However, if it's just a matter of its relationship to EDN and/or waiting until the next release then I am happy to wait. In either case, I'd like to complete this or push it to the back of my mind. Thanks.

0 votes
by

Comment made by: jafingerhut

clj-976-queue-literal-eval-and-synquote-patch-v2.txt dated Oct 5 2012 is identical to Fogus's patch CLJ-976-queue-literal-eval-and-synquote.diff dated Jul 20 2012. It simply removes one line addition to clojure.iml that Rich has since added in a different commit, so that this patch now applies cleanly to latest master.

...