Clojure

Unordered literals does not preserve left-to-right evaluation of arguments

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Declined
  • Affects Version/s: Release 1.4
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

Given: (defn f [x] (println x) x)

#{(f 2) (f 1)}

Prints:

1
2

But expected would be:

2
1

This issue is related to CLJS-288

Activity

Hide
Brandon Bloom added a comment -

Awesome, thanks!

Small typo: "Vectors elements" should be "Vector elements".

Show
Brandon Bloom added a comment - Awesome, thanks! Small typo: "Vectors elements" should be "Vector elements".
Hide
Alex Miller added a comment -

I added a comment to that page.

Show
Alex Miller added a comment - I added a comment to that page.
Hide
Brandon Bloom added a comment -

Sometime over the past two years, I've come around to the perspective that this is expected behavior. However, I still maintain that this should be documented somewhere!

On this page: http://clojure.org/evaluation

This paragraph:

Vectors, Sets and Maps yield vectors and (hash) sets and maps whose contents are the evaluated values of the objects they contain. The same is true of metadata maps. If the vector or map has metadata, the evaluated metadata map will become the metadata of the resulting value.

I'd propose inserting a sentence like:

Contents are evaluated sequentially in vectors, but in an unspecified order for sets and maps.

Show
Brandon Bloom added a comment - Sometime over the past two years, I've come around to the perspective that this is expected behavior. However, I still maintain that this should be documented somewhere! On this page: http://clojure.org/evaluation This paragraph:
Vectors, Sets and Maps yield vectors and (hash) sets and maps whose contents are the evaluated values of the objects they contain. The same is true of metadata maps. If the vector or map has metadata, the evaluated metadata map will become the metadata of the resulting value.
I'd propose inserting a sentence like:
Contents are evaluated sequentially in vectors, but in an unspecified order for sets and maps.
Hide
Alex Miller added a comment -

Sets are unordered.

Show
Alex Miller added a comment - Sets are unordered.
Hide
Nicola Mometto added a comment -

I don't think it's possible to preserve left-to-right evaluation of args on unordered literals, the original order will be lost during read-time, the compiler has no way to know about it.

Show
Nicola Mometto added a comment - I don't think it's possible to preserve left-to-right evaluation of args on unordered literals, the original order will be lost during read-time, the compiler has no way to know about it.
Hide
Brandon Bloom added a comment -

I'd consider the expected default behavior of any syntax or macro to evaluate each sub-form once each, from left to right. Conditional, repeated, or out-of-order evaluation should be documented as deviations from that norm. If you buy that, then this is either a code or a documentation bug. My vote is for code bug.

Show
Brandon Bloom added a comment - I'd consider the expected default behavior of any syntax or macro to evaluate each sub-form once each, from left to right. Conditional, repeated, or out-of-order evaluation should be documented as deviations from that norm. If you buy that, then this is either a code or a documentation bug. My vote is for code bug.
Hide
Andy Fingerhut added a comment -

I have the same question as David Nolen for CLJS-288: Is this a bug, or just behavior you didn't expect?

It seems that vectors preserve the order of evaluation, so if you really want to control evaluation order you could use something like (set [(f 2) (f 1)]) or (set (map f [2 1])).

Show
Andy Fingerhut added a comment - I have the same question as David Nolen for CLJS-288: Is this a bug, or just behavior you didn't expect? It seems that vectors preserve the order of evaluation, so if you really want to control evaluation order you could use something like (set [(f 2) (f 1)]) or (set (map f [2 1])).

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: