r/Clojure • • Apr 14 '23

Clojure 1.12.0-alpha2

https://clojure.org/news/2023/04/14/clojure-1-12-alpha2
96 Upvotes

23 comments sorted by

7

u/scopegoa Apr 14 '23

When I first started Clojure, it was so confusing to me that these features weren't here. For some reason, my brain just expected them.

Very happy to see the new support, can't wait to try it out. This is going to make it way easier for me to explore and try new libraries without breaking up my flow.

7

u/alexdmiller Apr 14 '23

Clojure 1.0 did (and current does still!) have `add-classpath`, but it lacked the infrastructure that has been built in tools.deps to really do good things with it, and was deprecated in 1.1.

15

u/geokon Apr 14 '23

I hope the requirement to have a REPL/deps.edn is eventually dropped.

It seems incredibly useful to just have single file executable '.clj' files that can fetch dependencies and run. For lots of things like runnable examples, small demos, and to get up and running quickly. You could just have small script files that do one thing. You can quickly email and share runnable minimal error repro files. I feel the possibilities are endless.

For instance I have a small program that manipulates Jasciidoc and outputs Reveal slides. It's like a couple dozen lines of code at most. But it's not a simple single file '.clj' in some scripts folder that I can run at the command line (when I need to convert documents). Instead it's a whole directory project. If I want to send it to someone I need to make and send a repo or like.. zip it or something. Or worse yet.. I'd need to compile it into an opaque executable. If I want to post it online (say in a comment on Reddit) I have to say "okay, so this part is the deps.edn and now this part is the code file. Make sure to copy and paste it correctly".

It's not a biggie if you've been mucking around Clojure for a while, but it's just needless friction. Most importantly, I think it'd greatly lowers the barrier to entry to try out Clojure for people curious about the language. I think the "gateway drug" aspect would be super simple single file Lisp things that manipulate the APIs of huge Java libs.

Not everything needs to be a multi-file "project" (this feels very Java/Enterpise) where you need to lug around bundles of files/directories

6

u/p1r4nh4 Apr 14 '23

Agree, dynamic var could be “faked” though, so I guess it’s still an exciting addition allowing not only for more comfortable REPL workflow, but also for a one-file scripts.

This is actually amazing. :)

13

u/alexdmiller Apr 14 '23

It is not hard to (binding [*repl* true] (add-lib 'whatever)) now and I think it may be interesting to have a runner mode of clj specifically for scripts that handled this for you. Future things to think about.

4

u/p1r4nh4 Apr 14 '23

Yeah, exactly what I was thinking about :)

1

u/p1r4nh4 Dec 05 '24

Well it turned out to be a little bit more involved (would love a correction if there is an easier way), but this works: https://gist.github.com/piranha/21efba5146fabf381cede6878001cedb

5

u/geokon Apr 14 '23 edited Apr 15 '23

yeah it's been around in alpha for a while and I used to use it in orgmode

https://kxygk.github.io/literate-clojure.html

interactively generating inlined svg into self contained HTML files. was pretty fun :)) (though I kinda gave up on the setup eventually)

29

u/Borkdude Apr 14 '23

babashka supports this as follows:

(ns my-single-file-script
  (:require [babashka.deps :as deps]))

(deps/add-deps '{:deps {your-library {:mvn/version "...."}}})
(require 'your-libary)
... rest of the script ...

3

u/geokon Apr 15 '23 edited Apr 15 '23

Yeah, it totally makes sense that you had to add this missing piece yourself. Esp in the scripting context it seems sort of essential (though bb bundles a lot of useful libs already). I haven't used bb, but I'm guessing add-deps will only works with pure Clojure libs. I guess a lot of usecases for me revolve around quickly using some Java libs to do some calculations or transformation/conversion.

Out of curiosity, what will you do now? Do you plan to support this global add-libs as well as babashka-specific add-deps? Or drop add-deps entirely?

4

u/Borkdude Apr 15 '23

I will probably also support the new clojure.repl/add-lib stuff for compatibility. No dropping/removing.

1

u/geokon Apr 16 '23

I kinda like your setup tbh (I can see why they designed add-libs the way they did though) b/c it's one step away from copy/pasting the map into a deps.edn file and making a full clojure project

5

u/seancorfield Apr 17 '23

Took me a while to find the gist...

https://gist.github.com/ericnormand/6bb4562c4bc578ef223182e3bb1e72c5

Shell scripts made of Clojure :)

1

u/seancorfield Apr 15 '23

add-lib/add-libs/sync-deps rely on the Clojure CLI in a couple of ways:

  1. They rely on the "basis" being available as a file specified in a JVM property (this is something the clojure shell script does when starting programs).

  2. They rely on shelling out to clojure as a command on your system to run as a "tool" with the :deps alias.

That's why it all depends on deps.edn and "the REPL" (it's actually all "main opts" entry points in clojure.main right now, in the alpha). So, if you start your REPL or program via clojure.main using the CLI, you have this functionality, otherwise you don't.

Clojure itself has long-supported core functions like load-file and slurp / read / eval / etc for loading and executing additional code at runtime, independent of any of this new stuff.

1

u/joinr Apr 17 '23

So, if you start your REPL or program via clojure.main using the CLI, you have this functionality, otherwise you don't.

Seems odd to add this implicit dependency on the cli, especially if it's not documented. If I started my own repl (e.g. via uber jar or java invocation), then none of this applies to me - despite tools.deps dependency resolution being in clojure and java. I don't get why this couldn't be run in-process (aside from lacking a "basis", which is a separate problem with some possible solutions) [maybe it can].

If pomegranate had git-deps resolution (seems in the realm of the possible) then it seems comparable, without the CLI requirement.

2

u/seancorfield Apr 17 '23

add-libs is built on invoke-tool which shells out to run clojure.

That means that none of the tools.deps code needs to be inside Clojure itself. But you need the CLI installed to use invoke-tool.

The docstring for invoke-tool is pretty clear about that: https://clojure.github.io/clojure/branch-master/clojure.tools.deps.interop-api.html#clojure.tools.deps.interop/invoke-tool

But the docstrings for the clojure.repl.deps stuff does not currently make that clear.

In addition, the add-libs stuff only runs if clojure.core/*repl* is bound to true (which clojure.main does for you).

The intention is that this is dev-only functionality (clojure.repl.deps being a good clue there :) )

You can build a program with tools.deps as dependency if you want all of that functionality at runtime for other than dev work -- it brings in a lot of libraries :)

6

u/lgstein Apr 14 '23

When a added lib transiently requires a newer lib than what is on the classpath it should throw or refuse and advise a JVM restart. Otherwise you might run into weird errors.

6

u/alexdmiller Apr 14 '23

Often, you (smart human) may know better whether this is definitely ok, maybe ok, or not ok than add-lib can know. Having lived with it for a while, it's usually fine. As the docstring says, this is a "best effort" operation designed to work at the repl. "best effort" is meant to convey - "may not always work".

5

u/lgstein Apr 15 '23

I'm surely not smart enough to know the dependency tree of libs I require... Maybe consider printing a warning? I would find it very helpful.

27

u/mtert Apr 14 '23

I feel like sync-deps has been on the clojure community wishlist for a long time. Nice to see it becoming a reality.

8

u/dustingetz Apr 14 '23

The invoke tools are unexpected and extremely useful - woot

11

u/v1akvark Apr 14 '23

Thanks Alex!

4

u/agumonkey Apr 14 '23

Sounds nice.