Created
April 28, 2012 21:54
-
-
Save owenthereal/2522346 to your computer and use it in GitHub Desktop.
Show case of Clojure's powerful meta-programming system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As you may see, we add
String
type to the metadata of the arguments
in functiontype_checking
and Clojure will type check for us. The 2nd example is through reflection. Reference: http://clojure.org/metadata.