11

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?

1
  • 1
    This is an old question, but sometimes it gets attention. FWIW, as I've used Clojure over the years, I've found that if I think I need the power of gen-class to get something like this done, it's easier just to write a little bit of Java. Commented Apr 2, 2017 at 20:03

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer and the additional information in the edit. The addition of the the . in the invocation is an important detail.
1

This is not an answer to your actual question, but I have a little library to let you pretend InputStream is an interface instead of a class (so that you don't need gen-class at all). Check out io.core.InputStream, which lets you reify io.core.InputStreamable and get out a customized InputStream. Whatever instance fields you need can just be locals closed over by the reify.

4 Comments

Interesting idea. I had thought about something like that, but ultimately want an input stream in case I want to hand these back to Java.
The whole point is that you do get an input stream. You create an InputStreamable describing your logic, and give that to InputStream, an AOTed Java class. Eg, (InputStream. (reify InputStreamable (read ...) (skip ...))).
@Bill Forgot to @ you in the previous comment. I think that means you didn't get notified; apologies if you're now getting a second notification.
Yes, I see what you mean. That's what I get for responding before my morning coffee. BTW, I get notifications on comment responses w/o an at sign.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.