Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6167685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:33:22+00:00 2026-05-23T22:33:22+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:33:23+00:00Added an answer on May 23, 2026 at 10:33 pm

    I have always used maps that have two different types for the key and value, never the same types. I suppose there are times when you would want to use the same type for the key and value as in your example. I can’t think of any languages off the top of my head that have this feature.

    Actually I have used Clojure before and for maps it uses a keyword for the key which looks like :key and then the value is just whatever the value is. It makes it clearer but now i’m dabbling into functional programming which is not really the question you were asking. Check out Lisp-like languages.

    I think its just a case of not getting them mixed up, Java certainly cannot enforce this kind of checking.

    Best advice would be to use different types for the key and value so the compiler can detect the type error, or name the key and value variables explicitly as you have done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: In Java, what does a reference to Class.class do? Firstly apologies for
Firstly, apologies for all the XAML. I've tried for a few days to resolve
Firstly, apologies for the vague title, but I'm not sure exactly what I'm asking
Firstly this is not a homework question. I am practicing my knowledge on java.
Firstly, I am not at all familiar with Windows batch file programing. Recently I
Firstly apologies for the title, I don't know if it describes what I am
Firstly, apologies for the title, I could not think of a suitable one. I
Firstly, let me apologise for the title, I honestly couldn't think how to word
Firstly, I know that CSS expressions are defunct and wrong in so many ways,
Firstly, appologies if i get any terminology wrong on the upcoming post, this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.