Uploaded image for project: 'core.typed'
  1. CTYP-289

Add ability to specify the static type expected for a particular extend-type dispatch

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Declined
    • Affects versions: None
    • Fix versions: None
    • Components: Core type system
    • Labels:
      None

      Description

      A link to the instigating mailing list post: https://groups.google.com/forum/#!topic/clojure-core-typed/Af650Di0VT4.

      The following is a sketch of what I think should be possible. (adapted from java.jdbc)

      (typed/defprotocol Connectable
        (add-connection [db connection :- java.sql.Connection] :- '{:connection java.sql.Connection})
        (get-level [db] :- typed/AnyInteger))
      
      (typed/extend-protocol Connectable
        String
        (add-connection [s connection] :- '{:connection java.sql.Connection
                                            :connection-string String
                                            :level '0})                      ; Ideally, this type is inferred, but that seems like a separate enhancement.
          {:connection connection :level 0 :connection-string s})
        (get-level [_] 0)
      
        (clojure.lang.Associative typed/Any typed/Any)  ; N.B. this type could/should be made more precise, but I'm leaving it as is to keep the example focused.
        (add-connection [m connection] (assoc m :connection connection))
        (get-level [m] (or (:level m) 0))
      
        nil
        (add-connection [_ connection] {:connection connection :level 0 :legacy true})
        (get-level [_] 0))
      
      
      (typed/defn get-connection-string [s :- String, conn :- java.sql.Connection] :- String
        (:connection-string (add-connection s conn)))
      

      In particular, note how the body of get-connection-string "knows" that the value of (add-connection s conn) will contain a string for the key :connection-string.

        Attachments

          Activity

            People

            • Assignee:
              ambrosebs Ambrose BS
              Reporter:
              merelyapseudonym Josh Tilles
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: