Fork me on GitHub
#clojure
<
2024-02-10
>
otwieracz00:02:19

Hey. How can I extend Java class with another (instance) field on Clojure?

phronmophobic00:02:13

There options for doing fancy java-isms from clojure, but sometimes it's easier to just write a bit of java in your clojure project for the non-trivial interop.

phronmophobic00:02:04

Otherwise, which method you use kind of depends on what problem you're trying to solve and why you're trying to extend a class while adding an instance field.

otwieracz00:02:34

Yeah, I was suspecting that straight Java might be easier.

otwieracz00:02:19

What I am trying to do is to create a record that will store initialisation parameters for Java object and have it implement component/Lifecycle protocol. Upon component startup I need it to return new object extending a Java class but with additional field carrying over the original object with init params so this original object can be returned after the stop is called on component.

phronmophobic00:02:11

You can close over state by defining an atom with a let and then create the proxy object using proxy which can access and mutate the atom

phronmophobic00:02:45

proxy lets you extend a class and interfaces.

phronmophobic00:02:18

i’m away from keyboard, but i can try to give an example later

otwieracz12:02:45

But won’t the state atom be shared across all instances of this protocol?

otwieracz09:02:26

Problem I see with proxy is that I don't think I can add new method.

otwieracz09:02:30

Only overwrite existing one.

phronmophobic09:02:33

not sure what you mean by add a new method. you can always create an interface with the method you want and implement it

phronmophobic09:02:09

I would give some example code, but I'm not totally sure I understand the goal

otwieracz09:02:49

Yeah, actually I did it the other way around. Burnt too much time here already.

otwieracz09:02:53

Thank you for help though!

👍 1
Nundrum00:02:18

I've built a cli tool, but before releasing it I need to handle config. Including API tokens and URLs. Are there any helpers out there for handling that cross-platform? I've been poking around today but can't find anything.

jpmonettas12:02:01

On Clojure 1.12, can type hinting like ^long* and ^longs be used interchangeable everywhere? Or are there any caveats? I see this cases work the same:

(def sorter ^[longs] Arrays/sort)
(def sorter ^[long*] Arrays/sort)
(defn sorter [^longs arr] (Arrays/sort arr))
(defn sorter [^long* arr] (Arrays/sort arr))

Alex Miller (Clojure team)13:02:30

As a type hint, yes

👍 1
jpmonettas13:02:11

and where aren't they equivalent? Since it looks like they also work the same for selecting methods as values

J14:02:45

Hi guys! I fixed a mistake that I really don't understand. Our CI use github action . Yesterday, we update the default runner to a custom machine (4cpu, 16GB, ubuntu 22). With this update, some tests started to fail. The tests focused on a function that used io/resource inside a promesa futur. The io/resource inside the futur returned nil ! I tap> to debug and outside the futur the io/resource returned the good result! Of course, on my local machine (M2 pro) no worries. When I came back to the original runner (default github ubuntu 22) the tests pass! Have you seen something like this.