Skip to content

Instantly share code, notes, and snippets.

@jneira
Forked from anonymous/ejemplo1.clj
Last active Dec 15, 2015
Embed
What would you like to do?
;; Se define en un modulo una funcion polimorfica en funcion del tipo
;; de los *dos argumentos*
(ns modulo1)
(defmulti write (fn [writer data] [(type writer) (:type data)])
;; En otro modulo que importa la definicion anterior generica de write
(ns modulo2 (require modulo1))
(defmethod modulo1/write [java.io.FileWriter :user] [writer user]
(comment codigo especifico usando el FileWriter y teniendo en cuenta
que los datos son del tipo :usuario))
(defmethod modulo1/write [java.io.CharArrayWriter :user] [writer user]
(.write writer (str user)))
(def writer (java.io.CharArrayWriter.))
(modulo1/write writer {:name "jmarranz" :type "user"})
(str writer)
;; "{:name "jmarranz" :type "user"}"
(defmethod modulo1/write [java.io.FileWriter :session] [writer session]
(comment otro codigo que usa FileWriter pero especifico para
el tipo :session))
;; En otro modulo
(ns modulo3 (use modulo1))
(defmethod write [SQLWriter :user] [w u]
(comment codigo que escribe los datos del usuario en la db))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment