Use Java inside Clojure - Java Interop Tutorial

Поділитися
Вставка
  • Опубліковано 25 гру 2024

КОМЕНТАРІ • 27

  • @TomasBrejla
    @TomasBrejla 3 роки тому +11

    Once again, nice tutorial.
    The only thing to add here: besides the (new String "abc"), you can also use (String. "abc"). I believe that the latter special form is actually more idiomatic. At least I'm seeing it much more often than (new) call.
    One of the very useful macros is also (doto). It allows "chained" execution of side-effectful (all those setters, .put .add etc) methods on some object. Very handy for working with java APIs which don't offer "fluent" (builder-based) API.
    For example, where in java you would have to define a variable and use it over and over again to call functions...
    Map myMap = new HashMap();
    myMap.put("some", "value");
    myMap.put("another", "value");
    In clojure, you can simply do
    (doto (java.util.HashMap.)
    (.put "some" "value")
    (.put "another" "value"))
    "fluent API" for free. Beauty and simplicity of macros.
    Btw it gets expanded to
    (let* [G__7500 (java.util.HashMap.)]
    (.put G__7500 "some" "value")
    (.put G__7500 "another" "value")
    G__7500)

  • @vladhajduk6555
    @vladhajduk6555 3 роки тому +2

    Awesome video, wasnt aware that it was this easy to have code interoperability between the two!

  • @professorclever6487
    @professorclever6487 3 роки тому +3

    Learning something new every time. Great stuff! I love the format!

  • @isaacomy274
    @isaacomy274 3 роки тому +1

    great job dude,
    Keep make more educative videos.
    Thanks
    -Clojurian from Indonesia

  • @jackliew7993
    @jackliew7993 2 роки тому +1

    Super awesome. Your videos are easy to follow. Thanks for sharing this great tutorial.

  • @SindhuS
    @SindhuS Рік тому +1

    Thanks so much for making this!! I'd defo pay for your patreon

  • @Johan4WD
    @Johan4WD 3 роки тому

    Very informative videos Dan. Thanks a mil bud!!

  • @chillbro2275
    @chillbro2275 2 роки тому +1

    Oh cool man!! So if your doing a Clojure project and find a class would make sense, you build a Java class then import it? Is that the normal pattern?

    • @onthecodeagain
      @onthecodeagain  2 роки тому

      Yeah you could do that if you want to copy and paste a premade class in :) you could also rewrite it with Clojure and use interop. Depends on the project!

  • @eugenej.5584
    @eugenej.5584 3 роки тому +2

    Next video - using Clojure from Java. Video preview should show a coffey cup with Clojure logo Latte art.

    • @onthecodeagain
      @onthecodeagain  3 роки тому +1

      Dude after I made this video I couldnt believe I didnt show that :/

    • @eugenej.5584
      @eugenej.5584 3 роки тому +1

      @@onthecodeagain You have to sip Clojure out of the java cup to make the meme complete.

    • @eugenej.5584
      @eugenej.5584 3 роки тому +1

      @@onthecodeagain You can make a whole video about **ns** macro. It can construct methods and stuff. Basically a class constructor for java interop.

    • @chillbro2275
      @chillbro2275 2 роки тому

      @@onthecodeagain hahah i would like to see that too

  • @GOZES
    @GOZES 3 роки тому +1

    This was really useful thank you very much. If can suggest and extension or a part 2 to this video. It would be great to have a video on how to read javadocs for non-java people :)

    • @onthecodeagain
      @onthecodeagain  3 роки тому

      Thanks man! Ill probably do a video showing how to make the docs at some point :)

    • @GOZES
      @GOZES 3 роки тому +1

      @@onthecodeagain I don't mean how to make the docs but more on how to make sense if Java docs for people like myself that have just started doing clojure but have zero experience with the jvm and jvm docs which you usually have to read and refer to when doing interop

    • @onthecodeagain
      @onthecodeagain  3 роки тому

      @@GOZES oh ok I get you!

  • @designernasser
    @designernasser 3 роки тому +1

    Good stuff! Keep it up!

  • @sadface7457
    @sadface7457 3 роки тому +1

    banging intro

  • @Brunodelucasdev
    @Brunodelucasdev Рік тому

    This bad example not work

    • @momo-dm3rw
      @momo-dm3rw Рік тому

      I tried and it works. You have to be patience. For some reason working Clojure is quite tricky sometimes.

    • @onthecodeagain
      @onthecodeagain  Рік тому

      Create a ticket