clojure-dev

Issues: https://clojure.atlassian.net/browse/CLJ | Guide: https://insideclojure.org/2015/05/01/contributing-clojure/
slipset 2020-09-18T11:50:32.001100Z

clj-kondo has a linter which checks for a try without a catch or finally. This made me curious and led me to this commit https://github.com/clojure/clojure/commit/6c0c37e048f49ee5cd3afa79ce461abbc6a0c367 Anyone know the history/rationale behind this?

borkdude 2020-09-18T11:55:14.001600Z

I suppose you mean: why not just throw if there's no catch/finally?

borkdude 2020-09-18T11:55:59.002300Z

There is one try without catch/finally in the locking macro. Not sure why it's there, but it has a subtle implication for the bytecode, if I remember correctly

bronsa 2020-09-18T12:02:27.003200Z

@slipset (try x (catch Exception _)) has slightly different semantics than (do x) even if no exception is thrown

bronsa 2020-09-18T12:03:15.003600Z

even ignoring the extra needless bytecodes that would be emitted

slipset 2020-09-18T12:03:39.004400Z

@bronsa I guess what I’m asking is why would you want (try foo)

bronsa 2020-09-18T12:03:40.004600Z

the body of a try is hoisted in a fn, so recur breaks for example

bronsa 2020-09-18T12:03:45.004800Z

@slipset macros may emit it

bronsa 2020-09-18T12:04:06.005100Z

it's the same reason why you'd want (do x) instead of simply x

bronsa 2020-09-18T12:04:11.005300Z

it's convenient when doing codegen :)

slipset 2020-09-18T12:05:02.005900Z

Right.

borkdude 2020-09-18T12:14:17.006100Z

ah nice

borkdude 2020-09-18T12:15:40.006300Z

user=> (clj-java-decompiler.core/decompile (try :dummy))

// Decompiling class: user$fn__198
import clojure.lang.*;

public final class user$fn__198 extends AFunction
{
    public static final Keyword const__0;

    public static Object invokeStatic() {
        return user$fn__198.const__0;
    }

    @Override
    public Object invoke() {
        return invokeStatic();
    }

    static {
        const__0 = RT.keyword(null, "dummy");
    }
}

borkdude 2020-09-18T12:16:59.006800Z

I guess (try :dummy) would be equivalent to ((fn [] :dummy)) for that purpose

slipset 2020-09-18T12:26:04.007400Z

Oh, an iefe 🙂

2020-09-18T12:49:13.008100Z

I don’t have the details on hand, but I recall that Clojure seemed to emit more bytecode ops than java in to do a try-catch and I was confused why

2020-09-18T12:49:37.008600Z

meaning, it seemed heavier overhead than in java

alexmiller 2020-09-18T12:52:16.010700Z

There’s more local clearing probably

souenzzo 2020-09-18T12:52:23.010900Z

I think that is nice to clojure have (try ... #_(catch ...)) for REPL proposes (just comment the catch part for dev) And also nice kondo warns you about that

👍 1
2020-09-18T13:00:16.011500Z

that may be it @alexmiller maybe I’ll redo my example again to remember - wish I would have noted it