Fork me on GitHub
#clj-kondo
<
2022-10-09
>
seancorfield17:10:18

Following on from a discussion in #C03S1KBA2 I would like to see clj-kondo flag this as problematic code:

(defn panda
  [& {:keys [a b c d e f g h]
      :or {a 1
           b a
           c b
           d c
           e d
           f e
           g f
           h g}}]
  [a b c d e f g h])

(defn sad-panda
  [& {:keys [a b c d e f g h i]
      :or {a 1
           b a
           c b
           d c
           e d
           f e
           g f
           h g
           i h}}]
  [a b c d e f g h i])
The first fn works by accident as the defaults map is small and uses an array map so the keys stay in order and defaults can depend on earlier ones. The second fn fails because the defaults map is larger and uses a hash map and so the keys are in a different order. This has come up before and I'm pretty sure Alex confirmed that this was never intended to work -- it's just an accident of implementation in the small case. Could clj-kondo detect that the default values are depending on each other?

borkdude17:10:53

I think it's a similar but not exactly the same issue

borkdude17:10:59

Maybe add a comment to the issue

seancorfield18:10:13

Added a 👍:skin-tone-2: Interesting group of related issues there around destructuring. Sorry I didn't just search the open issues before commenting 😕

borkdude18:10:30

No problem, there are so many :)

seancorfield18:10:35

The burden of popularity 🙂

Noah Bogart20:10:11

Oooooh this is a brain tickler. I might give this a go, intrigued by how I’d go about it.

borkdude20:10:12

It's fairly easy to implement

borkdude20:10:06

Probably the hardest part is a good name. Suggestions welcome :)

😂 1
Noah Bogart21:10:59

:or-local-binding, :or-non-local-binding, :or-bindings, :or-expression-bindings, etc

borkdude21:10:59

:destructured-binding-in-default?

Noah Bogart14:10:57

:self-referential-default-bindings lol

borkdude14:10:49

I think the issue is now beyond self-referential bindings, any destructured binding (from the same map) in defaults is considered undefined now

Noah Bogart14:10:03

or maybe :self-referential-destructured-bindings because any sort of self-reference is undefined

borkdude14:10:32

Keep trying :)

Noah Bogart14:10:13

i'm using self-referential to mean "from the same map". i don't have a good phrase at hand that means the same

borkdude14:10:46

undefined-default

Noah Bogart14:10:38

(just in case, i love these bikeshedding discussions but if you find something you like, you don't need to idk get my permission or blessing lol)

Noah Bogart14:10:09

undefined-default is pithy and short but doesn't tell me what exactly it's protecting against unless I already know the error

borkdude14:10:46

I'll wait for @U04V70XH6 to throw in a good name suggestion as well

seancorfield15:10:19

After several of the suggestions here, I have nothing 😆