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

January 15th, 2011

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 compiles, but at runtime I receive "java.lang.IllegalStateException: Var selfr is unbound.".

(defn magic []
  (println "magic happens") ; one too few closing parens

(defn selfr [i]
  (if (< i 0)
    0
    [(selfr (dec i))
     (selfr (dec i))]))) ; one too many closing parens

(defn selfr-caller []
  (selfr 3))

(defn -main [& args]
;  (magic) ; uncomment to make everything work
  (println (selfr-caller)))

If you stare at the code for a bit, you'll see that selfr is defined inside the magic function. So why doesn't the compiler complain that selfr-caller can't see it? What I didn't realize until today (with the help of some wonderful people on IRC) was 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. I suppose this could have been prevented by having a better editor, such as one with draconian indentation support (e.g. Emacs) or perhaps a matching-paren highlighting feature.

Tone down your system beep

August 9th, 2009

The "system beep" (or "system bell") can be quite annoying. Instead of disabling it altogether, you may be able to change it to something a little less irritating. Some operating systems will allow you to change it to a visual alert or a sound file of your choice. Today, I'll show you how to change the duration, pitch, and volume to something less grating if you're using X.Org (applies to just about all Unix or GNU/Linux systems.) [via]

I use xset, a command that can set various preferences in the current X display. One of the parameter sets is b, the properties of the system bell. It can be used to set the relative volume, pitch, and duration. Here's an example:

xset b 50 700 5

That example sets the system bell to 50% volume, a pitch of 700 hertz, and a duration of 5 milliseconds.

Experiment by entering that command, then pressing [Backspace] on the next line of your terminal (to generate a system bell.) Your hardware may not support certain combinations, in which the bell is silent.

To have this setting take effect every time you log in, add it to your session startup commands. In Ubuntu, that's System :: Preferences :: Sessions.

GWT, standard library, and IncompatibleRemoteServiceException

July 15th, 2009

The GWT is a set of programs that allows developers to write Java code and compile it down into Javascript to be run on browsers. This requires you to use the subset of Java that the toolkit can emulate, but the compile-time and runtime error messages it displays are often not very useful.

I was developing a GWT application with Eclipse and ran into a client-server communication issue that manifested at runtime. The client would request an object from the server, the object would properly serialize and deserialize (verified by JS debugger), and then GWT's glue code would throw IncompatibleRemoteServiceException (saying "This application is out of date, please click the refresh button on your browser.")

Read full entry »

Fast, manual, incremental updating of WordPress

February 6th, 2008

WordPress recently released 2.3.3 as an urgent security fix for 2.3.2. Rather than wiping all non-configured files from my development site, extracting the replacement files from the tarball, and re-uploading said files by FTP, I used the fast and precise approach: Only upload changed files.

Read full entry »

No-hassle IE movie embedding

October 5th, 2007

Due to a lawsuit by Eolas against Microsoft, Internet Explorer (both 6 and 7) is not allowed to auto-load movies, flash, and other plugins. The effect is that users must click an empty area with a gray outline to load the content or start interacting with it. The standard approach to route around this usability fiasco is to dynamically load the <object> and <embed> tags with javascript. Here I present an easier, more readable, and unobtrusive technique for embedding content in IE6 and IE7.

Read full entry »