This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-11-26
Channels
- # babashka (5)
- # beginners (21)
- # cider (26)
- # clojure (46)
- # clojure-art (3)
- # clojure-dev (36)
- # clojure-europe (24)
- # clojure-norway (64)
- # conjure (28)
- # cursive (2)
- # datomic (1)
- # honeysql (12)
- # improve-getting-started (5)
- # introduce-yourself (5)
- # kaocha (1)
- # malli (17)
- # polylith (36)
- # reitit (3)
- # shadow-cljs (8)
hi, any idea how I can adapt Java generic code to clojure?
Cache<String, String> routeToAirline = new Cache2kBuilder<String, String>() {}
.name("routeToAirline")
.loader(new CacheLoader<String, String>() {
@Override
public String load(final String key) throws Exception {
String[] port = key.split("-");
return findFavoriteAirline(port[0], port[1]);
}
})
.build();
My current code is like this:
(ns ro.ieugen.training.cache2k
(:import (org.cache2k Cache2kBuilder)
( CacheLoader)))
(defn new-cache
[f]
(let [loader (reify CacheLoader
(load [_this key]
(f key)))]
(-> (proxy [Cache2kBuilder] [])
(.name "cache-example")
(.loader loader)
(.eternal true)
(.entryCapacity 100)
(.build))))
(comment
(let [cache (new-cache #(str % "-key"))]
(-> cache (.get "a")))
)
but it does not work because the types are not known
clj꞉ro.ieugen.training.cache2k꞉>
; Execution error (IllegalArgumentException) at org.cache2k.Cache2kBuilder/<init> (Cache2kBuilder.java:160).
; Use Cache2kBuilder.forUnknownTypes(), to construct a builder with no key and value types
I found the solution. use another API:
(ns ro.ieugen.training.cache2k
(:import (org.cache2k Cache2kBuilder CacheManager)
( CacheLoader)))
(defn new-cache
[f name]
(let [loader (reify CacheLoader
(load [_this key]
(f key)))]
(-> (Cache2kBuilder/of (class String) (class String))
(.name name)
(.loader loader)
(.eternal true)
(.entryCapacity 100)
(.build))))
(comment
(CacheManager/closeAll)
(new-cache #(str % "-key") "cache-example")
(let [cache (-> (CacheManager/getInstance)
(.getCache "cache-example"))]
(-> cache (.get "b")))
)
(class String)
will return java.lang.Class
I think? I.e. the class of a Class instance. You can just use String
as it refers to the class itself
Sooooooo soft
writing an article for the graalvm blog about babashka. I want to keep it accessible for Java programmers. If anyone wants to help proofread, let me know
@U011NGC5FFY Thanks, give me your github handle and I'll add you to the repo
hi, I read the article and I think it's pretty good in general. Regarding to accessibility part for Java developers: I don't know. I would be inclined to say it's not very accessible - since it's not a lot of Java related stuff there. Most of the things are Clojure specific and a bit graalvm. I would say that maybe jshell would be worth mentioning and compared with the REPL ? (not sure jshell is used that much) . also, I would imagine tooling like maven and gradle are much more used in Java and more familiar since there where no jshell or graalvm until recently. That IMO led Java devs on the tooling path.