Clojure

defn :or form does not warn you if you provide a vector instead of a map

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Trivial Trivial
  • Resolution: Declined
  • Affects Version/s: Release 1.3
  • Fix Version/s: Backlog
  • Component/s: None
  • Labels:
    None
  • Environment:
    Clojure 1.3, Emacs 23, OSX Lion

Description

I had a problem defining a function today, I passed :or a vector instead of a map - everything seemed to work fine but later on I got a really puzzling "Null pointer exception".

raek helped me debug the problem and suggested opening this ticket. I think it would be useful if an exception was thrown on the :or line unless it's given a map.

Here are some examples

(defn broken-example [{:keys [i processor]
:or [processor identity]}]
(processor i))

(defn working-example [{:keys [i processor]
:or {processor identity}}]
(processor i))

(working-example {:i 1})
=> 1
(broken-example {:i 1})
=> Null pointer exception

Cheers, Dave.

Activity

Hide
Dave Barker added a comment -

Apologies, I meant to tag this as a minor issue instead of a major one!

Show
Dave Barker added a comment - Apologies, I meant to tag this as a minor issue instead of a major one!
Fogus made changes -
Field Original Value New Value
Fix Version/s Release 1.4 [ 10040 ]
Priority Major [ 3 ] Minor [ 4 ]
Hide
Stuart Halloway added a comment -

Since vectors are associative, maybe using vectors for :or should just work?

(let [{noun 0
       verb 1
       obj 2
       :or [:n :v :o]}
      [:code :is]] obj)
Show
Stuart Halloway added a comment - Since vectors are associative, maybe using vectors for :or should just work?
(let [{noun 0
       verb 1
       obj 2
       :or [:n :v :o]}
      [:code :is]] obj)
Stuart Halloway made changes -
Issue Type Defect [ 1 ] Enhancement [ 4 ]
Fix Version/s Approved Backlog [ 10034 ]
Fix Version/s Release 1.4 [ 10040 ]
Priority Minor [ 4 ] Trivial [ 5 ]
Hide
Stuart Halloway added a comment -

Closing this until someone wants to make a principled argument for what should happen (see my previous comment)

Show
Stuart Halloway added a comment - Closing this until someone wants to make a principled argument for what should happen (see my previous comment)
Stuart Halloway made changes -
Status Open [ 1 ] Closed [ 6 ]
Resolution Declined [ 2 ]
Alex Miller made changes -
Fix Version/s Approved Backlog [ 10034 ]
Fix Version/s Backlog [ 10035 ]

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: