Skip to content

Latest commit

 

History

History
36 lines (26 loc) · 1.01 KB

wrong-num-args-passed-to-keyword.md

File metadata and controls

36 lines (26 loc) · 1.01 KB

Working with data literals

Error Message

IllegalArgumentException Wrong number of args passed to keyword: :bread

Cause

Using parens instead of curly braces to create a map literal will cause the above error, e.g:

(:first-name "Bob", :last-name "Bobberton")

What's happening here is that :first-name is being called like a function and the rest of the items in the list are treated as its arguments. Keywords implement the IFn protocol and can be called like functions. The keyword expects a map and an optional default value as its arguments:

(:first-name {:first-name "Bob"})

(:first-name {} "Bob")

When the first argument to a keyword is not a map, then the result will be nil. This leads to a related error where trying to create a map with a single key/value pair will return a nil:

(:first-name "Bob") ;=> nil

Solution

Make sure you're using the {...} parens when creating the map:

{:first-name "Bob", :last-name "Bobberton"}