Misbalanced parens + hoisted defn = surprising runtime error in Clojure code

Automated disclaimer: This post was written more than 10 years ago and I may not have looked at it since.

Older posts may not align with who I am today and how I would think or write, and may have been written in reaction to a cultural context that no longer applies. Some of my high school or college posts are just embarrassing. However, I have left them public because I believe in keeping old web pages aliveā€”and it's interesting to see how I've changed.

Today I ran into a frustrating syntax pitfall in Clojure, which I am attempting to learn using a simple homework assignment as motivation. Below is a simplified testcase containing the same mistake I had in my actual code. The code>magic function. So why doesn't the compiler complain that defn hoists the declaration to the top-level, where everything can see it... but the assignment won't occur until the code block is actually run.

This kind of bug is extraordinarily tricky to find, since it is ultimately a syntax error but may not show symptoms depending on the execution order of the code for a bit, you'll see that defn hoists the declaration to the top-level, where everything can see it... but the assignment won't occur until the code>magic function. So why doesn't the compiler complain that defn hoists the declaration to the top-level, where everything can see it? What I didn't realize until today (with the help of some wonderful people on IRC) was that selfr is defined inside the magic function. So why doesn't the compiler complain that selfr is defined inside the selfr is defined inside the

No comments yet. Feed icon

Self-service commenting is not yet reimplemented after the Wordpress migration, sorry! For now, you can respond by email; please indicate whether you're OK with having your response posted publicly (and if so, under what name).