Fork me on GitHub
#clojure-italy
<
2017-07-20
>
manuel13:07:59

ciao a tutti. Dubbi con defprotocol, deftype e extend-type. Ho un protocollo P così definito:

(defprotocol P
    (f [this])
ho bisogno di applicare f di P a tipi "base" (es. String) e a tipi custom creati ad hoc con deftype. Qual è la strada migliore? Il dispatch a runtime funziona per tipo, ma ho bisogno di distinguere quando chiamare f e .f a seconda se sono su tipo base (esteso con extend-type) o tipo custom (creato con deftype). Ammetto che il mio background Java mi spinge sempre verso l'OO, sorry. 🙂

bronsa13:07:28

perche` vuoi usare .f?

manuel13:07:32

come faccio a chiamare f di deftype, altrimenti?

bronsa13:07:29

user=> (defprotocol P (f [_]))
P
user=> (deftype T [] P (f [_] 1))
user.T
user=> (extend-type String P (f [_] 2))
nil
user=> (f (T.))
1
user=> (f "")
2

manuel13:07:53

capito. Quindi posso chiamare anche senza .. Grazie, faccio qualche prova.