Hi Istvan,
I've run into this a fair bit too. To catch such problems (at
runtime), I sprinkle my code with (safe-get m :key) in key places,
rather than (:key m) or (m :key) or (get m :key). safe-get:
(defmacro lazy-get
"Like get but lazy about evaluating default"
[m k d]
`(if-let [pair# (find ~m ~k)]
(val pair#)
~d))
(defn safe-get
"Like get but throw an exception if key not found"
[m k]
(lazy-get m k
(throw (IllegalArgumentException.
(format "Key %s not found in %s" k m)))))
It's not ideal though, especially since I'd imagine you won't get fast
access for new defrecords.
I think it might be nice if one could optionally make "closed" maps or
records, which throw when you try to get a missing key rather than
returning nil. I'm not sure what the implications of this would be,
though. Maybe others have more elegant solutions...
-Jason
On Apr 22, 10:43 am, Istvan Devai <
ist...@istvandevai.com> wrote:
> Hi!
>
> In general, what to give greater attention if I'm getting lots of
> runtime errors due to mistyped keywords? (eg. I'm referencing a map
> where the keyword is non-existent and this nil value goes deep down into
> my code where it is very hard to see that this was caused by a
> non-existing map entry).
>
> Are there some constructs that allow catching some of these errors
> compile time or ease debugging runtime?
>
> Cheers,
> Istvan
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.To post to this group, send email
tocl...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email
toclojure+...@googlegroups.com