Java generics with 'semantics', is this possible?

Viewed 475

Firstly apologies if I'm using the wrong term by picking the word 'semantics.'

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.

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.

A simple example:

Map<String, String> myMap = new HashMap<String, String>();
String myKey = "key";
String myVal = "value";
myMap.put(myVal, myKey);

Which no compiler will catch. I could subclass Stringmake wrappers to give me a Key type and a Value type, I suppose. I could name my variables to indicate their use (as I've done in this example). What else?

So my questions are:

  • out of academic interest, what is this concept called and what languages have this feature?
  • In the absence of any such language feature, what are the best practices to avoid this kind of error?

Thanks

5 Answers
Related