Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke superclass' method in a Clojure gen-class method?

Tags:

clojure

I'm trying to create a class that extends input stream Clojure via gen-class. If I want to invoke the parent class' method, how do I do that?

like image 377
Bill Avatar asked Jan 30 '12 06:01

Bill


1 Answers

From (doc gen-class)1:

:exposes-methods {super-method-name exposed-name, ...}

It is sometimes necessary to call the superclass' implementation of an
overridden method.  Those methods may be exposed and referred in 
the new method implementation by a local name.

So, in order to be able to call the parent's fooBar method, you'd say

(ns my.custom.Foo
  (:gen-class
    ; ...
    :exposes-methods {fooBar parentFooBar}
    ; ...
    ))

Then to implement fooBar:

(defn -fooBar [this]
  (combine-appropriately (.parentFooBar this)
                         other-stuff))

1 In addition to the :gen-class facility provided by ns forms, there is a gen-class macro.

like image 120
Michał Marczyk Avatar answered Sep 27 '22 21:09

Michał Marczyk