Fork me on GitHub
#off-topic
<
2018-03-29
>
gklijs06:03:21

calling clojure from kotlin (in this case it makes little sence since the fucntion is only java interop, and loses type safaty because of it)

val require = Clojure.`var`("clojure.core", "require")
        require.invoke(Clojure.read("nl.openweb.topology.value-generator"))
        val uuidF = Clojure.`var`("nl.openweb.topology.value-generator","bytes->uuid")
        val cmtId = uuidF.invoke(message.id.bytes())

gklijs06:03:10

I just need to do the require once fro the jvm right? So best would be to do it in the main?

borkdude10:03:38

require is idempotent

gklijs10:03:28

I was trying to find out what would be the ‘best’ way to include the bridge to clojure from kotlin, maybe make a seperate spring component, with a @postconstruct which does the require, and gets all the functions?

borkdude10:03:44

Maybe @cfleming has a good idea about this. He uses Kotlin in combination with Clojure afaik.

michaels13:03:15

curious about this too - I got the go ahead to do a clojure module in our java/kotlin Spring codebase….

👌 4
borkdude13:03:52

Name suggestion for this class: ClojureBridge

😆 4
mikerod15:03:46

@borkdude > require is idempotent Yes, but it isn’t “free”. If perf doesn’t matter whatsoever though, sure you can just keep calling require all the time

mikerod15:03:27

I think it is fairly common to do the require.invoke(etc) sort of logic in a static block of a Clojure interop sort of class like this.

gklijs16:03:50

Turned out like this, seems clean enough

package nl.openweb.commandhandler

import clojure.java.api.Clojure
import clojure.lang.IFn
import org.springframework.stereotype.Component
import java.util.*

@Component
class ClojureBridge {
    private final var toUuid: IFn

    init {
        val require = Clojure.`var`("clojure.core", "require")
        require.invoke(Clojure.read("nl.openweb.topology.value-generator"))
        toUuid = Clojure.`var`("nl.openweb.topology.value-generator","bytes->uuid")
    }

    fun toUuid(bytes: ByteArray): UUID {
        return toUuid.invoke(bytes) as UUID
    }
}

👍 8