title: Java generics with 'semantics', is this possible?
body: <p>Firstly apologies if I'm using the wrong term by picking the word 'semantics.'</p>
  
  <p>I'm a big fan of generics in Java for all the obvious reasons. It helps me enormously as I work with a huge variety of odd bits of code and I often have to go back to old stuff. Today I found myself with a classic parameter misplacement bug which I probably wouldn't have written in the pre-generic days - they have made me a bit lazy.</p>
  
  <p>I'd like to know if there is a language feature, either in Java or perhaps as a similar concept in other languages, which takes the type safety of generics and extends it to a kind of semantic safety. Specifically I want to help trap the kind of errors which result from putting the right thing in the wrong place.</p>
  
  <p>A simple example:</p>
  
  <pre><code>Map&lt;String, String&gt; myMap = new HashMap&lt;String, String&gt;();
  String myKey = "key";
  String myVal = "value";
  myMap.put(myVal, myKey);
  </code></pre>
  
  <p>Which no compiler will catch. I could <s>subclass <code>String</code></s>make wrappers to give me a <code>Key</code> type and a <code>Value</code> type, I suppose. I could name my variables to indicate their use (as I've done in this example). What else?</p>
  
  <p>So my questions are:</p>
  
  <ul>
  <li>out of academic interest, what is this concept called and what languages have this feature?</li>
  <li>In the absence of any such language feature, what are the best practices to avoid this kind of error?</li>
  </ul>
  
  <p>Thanks</p>