Fork me on GitHub
#clojure-dev
<
2020-09-18
>
slipset11:09:32

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?

borkdude11:09:14

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

borkdude11:09:59

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

bronsa12:09:27

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

bronsa12:09:15

even ignoring the extra needless bytecodes that would be emitted

slipset12:09:39

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

bronsa12:09:40

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

bronsa12:09:45

@slipset macros may emit it

bronsa12:09:06

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

bronsa12:09:11

it's convenient when doing codegen :)

borkdude12:09:40

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");
    }
}

borkdude12:09:59

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

slipset12:09:04

Oh, an iefe 🙂

mikerod12:09:13

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

mikerod12:09:37

meaning, it seemed heavier overhead than in java

alexmiller12:09:16

There’s more local clearing probably

souenzzo12:09:23

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

👍 3
mikerod13:09:16

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