Skip to content

Instantly share code, notes, and snippets.

@owenthereal
Created April 28, 2012 21:54
Show Gist options
  • Save owenthereal/2522346 to your computer and use it in GitHub Desktop.
Save owenthereal/2522346 to your computer and use it in GitHub Desktop.
Show case of Clojure's powerful meta-programming system
user=> (defn type_checking [^String s] (.toUpperCase s))
#'user/type_checking
user=> (type_checking 12)
ClassCastException java.lang.Long cannot be cast to java.lang.String user/type_checking (NO_SOURCE_FILE:43)
user=> (defn no_type_checking [s] (.toUpperCase s))
#'user/no_type_checking
user=> (no_type_checking 12)
IllegalArgumentException No matching field found: toUpperCase for class java.lang.Long clojure.lang.Reflector.getInstanceField (Reflector.java:271)
@owenthereal
Copy link
Author

As you may see, we add String type to the metadata of the argument s in function type_checking and Clojure will type check for us. The 2nd example is through reflection. Reference: http://clojure.org/metadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment