title: How does the Swift Dictionary subscript disambiguate Int keys and indices?
body: <p>Currently I'm working on a data structure intended to store store key-value pairs uniquely and keep them sorted by key. Essentially it is a sorted dictionary and as such I am seeking to keep as much of the Collection and Dictionary semantics of Swift as I can.</p>
  
  <p>In the documentation and Swift source (as best as I can find), Dictionaries have two subscripts. One is the most commonly used <a href="https://github.com/apple/swift/blob/48a8b2655a81bde972c139246ad024b55026a8cb/stdlib/public/core/HashedCollections.swift.gyb#L2077-L2092" rel="nofollow noreferrer">subscript by key (Github source)</a>:</p>
  
  <pre><code>extension Dictionary {
    ...
    public subscript(key: Key) -&gt; Value? {
      @inline(__always)
      get {
        return _variantBuffer.maybeGet(key)
      }
      set(newValue) {
        if let x = newValue {
          // FIXME(performance): this loads and discards the old value.
          _variantBuffer.updateValue(x, forKey: key)
        }
        else {
          // FIXME(performance): this loads and discards the old value.
          removeValue(forKey: key)
        }
      }
    }
    ...
  }
  </code></pre>
  
  <p>And the second is a <a href="https://github.com/apple/swift/blob/48a8b2655a81bde972c139246ad024b55026a8cb/stdlib/public/core/HashedCollections.swift.gyb#L2026-L2028" rel="nofollow noreferrer">subscript by position/index (Github) source)</a> as part of its conformance to the Collection protocol:</p>
  
  <pre><code>extension Dictionary: Collection {
    ...
    public subscript(position: Index) -&gt; Element {
      return _variantBuffer.assertingGet(position)
    }
    ...
  }
  </code></pre>
  
  <p>When using these with a Dictionary keyed by something other than an <code>Int</code>, they behave exactly as expected because the subscripts are distinguished by different parameter types, ie: <code>String</code> vs <code>Int</code>.</p>
  
  <pre><code>let stringKeys = ["One": 1, "Two": 2, "Three": 3]
  stringKeys["One"]   // 1
  stringKeys[1]       // ("Two", 2)
  </code></pre>
  
  <p>When <code>Int</code>s are used as keys, the key subscript is used as desired.</p>
  
  <pre><code>let intKeys = [1: "One, 2: "Two, 3: "Three"]
  intKeys[1]   // "One"
  </code></pre>
  
  <p>How does the Dictionary type accomplish this? It would seem to me that the <code>Index</code> and <code>Key</code> parameters of the subscripts are both <code>Int</code> and that the compiler should not know which was intended.  Indeed, when I implement the same subscripts for my custom dictionary, the compiler gives exactly that error—"Ambiguous use of 'subscript'"—when I test it with <code>Int</code> keys.</p>
  
  <p>At first I wondered if one was a default provided in a protocol extension and overridden by a more specific implementation, but from what I can tell above that is not the case. The only other theory I have is that <code>Index</code> is of some other type than 'Int` such that it remains unambiguous but I can't find my way to anything that confirms this. Can anyone shed some light on this? Beyond my immediate needs, it's quite a clever bit of behavior in Swift that I'd like to understand.</p>
  
  <p>Thank you to all for reading and for your help!</p>