Uploaded image for project: 'Clojure'
  1. CLJ-2376

[core.specs] Check early if let binding vector is even

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Duplicate
    • Affects versions: Release 1.9
    • Fix versions: Release 1.10
    • Labels:
    • Approval:
      Vetted
    • Patch:
      Code

      Description

      If you miss a value in the let binding vector, Spec will return a rather verbose and misguiding message:

      user=> (require '[clojure.spec.alpha :as s] '[clojure.core.specs.alpha :as cs])
      user=> (s/explain ::cs/bindings '[a #_1 b 2])
      In: [2] val: 2 fails spec: :clojure.core.specs.alpha/local-name at: [:binding :sym] predicate: simple-symbol?
      In: [2] val: 2 fails spec: :clojure.core.specs.alpha/seq-binding-form at: [:binding :seq] predicate: vector?
      In: [2] val: 2 fails spec: :clojure.core.specs.alpha/map-bindings at: [:binding :map] predicate: coll?
      In: [2] val: 2 fails spec: :clojure.core.specs.alpha/map-special-binding at: [:binding :map] predicate: map?
      

      The crux of the problem here is the odd number of parameters in the binding. Spec gets to it anyway, but if it first finds a parameter that is an invalid binding form, it will bark at that instead. With the suggested patch, the error looks like:

      user=> (s/explain ::cs/bindings '[a #_1 b 2])
      val: [a b 2] fails spec: :clojure.core.specs.alpha/bindings predicate: even-number-of-forms?
      

      Patch: clj-2376-2.patch

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              alexyakushev Alexander Yakushev
            • Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: