polylith

Andrew Leverette 2025-02-16T21:10:24.975719Z

Can you define interfaces in Polylith with a protocol? My assumption is that you can't based on the way some of the documentation is written, but it's not explicitly stated either way. Specifically, I'm curious about using the same interface for multiple components.

Leo E 2025-02-18T06:19:06.394019Z

same interface for multiple componentsI think that if you switch to polylith, then at some point you will still need to implement runtime polymorphism. For example, for use several LLM or Storage implementations in same time or something like that. My solution for now is to create a thin (only interface) base component containing the protocol and calls to its methods. Implementations are moved to separate components in which only the creation function will be implemented. But I'm still looking for an even more obvious way of doing runtime polymorphism in polylith. Smth like this

(ns storage.interface)

(defprotocol Storage
  (get-value [this key])
  (set-value [this key value]))

(ns storage-pg.interface)

(defn create
  [config]
  (impl/create config))

seancorfield 2025-02-16T21:25:01.097099Z

Protocols are runtime polymorphism. Multi-implementation components are effectively "build-time" polymorphism. They're orthogonal.

seancorfield 2025-02-16T21:26:06.015899Z

Can you elaborate on your "curiosity"?

Andrew Leverette 2025-02-16T21:29:36.265189Z

Ah, okay. That makes sense. I was curious because I just realized that I've looked at a bunch of examples and never saw anyone using protocols in their interfaces. I forget that the key is "build-time".

Andrew Leverette 2025-02-16T21:29:42.685959Z

Thanks for the reminder!

seancorfield 2025-02-16T21:33:25.505259Z

I mean, we have protocols as part of some components, usually in a ns like ws.executors.interface.protocols but that's for multiple implementations at runtime, not at build time.