Fork me on GitHub
#cljs-dev
<
2022-01-05
>
dnolen15:01:43

@lilactown you could also do via default if performance is not really much of a concern for your own protocols

dnolen15:01:02

i.e. default + satisifes? etc.

lilactown16:01:02

yeah, I was originally trying to get around checking satisfies? for perf

dnolen16:01:36

@lilactown there's really no way to do what you want and for it to be fast

emccue16:01:26

is satisfies that slow? I ask because satisfies checks are a good chunk of what our app does when handling http responses and it hasn’t been that big a deal

dnolen17:01:20

@emccue it's not that slow, generally when I'm saying "slow" I mean something that might appear in an inner loop and thus not be suitable

lilactown18:01:50

what i ended up doing is what emccue suggested above: adding checks for classes of types like sequential? map? set? and then falling back to protocol dispatch

lilactown18:01:08

i'm assuming that sequential? et al. are faster than satisfies?

lilactown18:01:20

haven't benchmarked it yet

dnolen18:01:51

@lilactown no, those have to rely on satisfies?

emccue19:01:53

the map? one might be an issue since most custom types could be records now that i think about it

emccue19:01:50

So you probably actually want

(defn method [o]
  (cond
    (satisfies? Proto o) 
    (method* o)

    (map? o)
    (map-impl o)

    (set? o)
    (set-impl o)

    (sequential? o)
    (sequential-impl o)

    :else
    (throw (ex-info "" {})))