is there some lightweight tecnique for adding type safety to identifier properties?
Posted
by shoren
on Stack Overflow
See other posts from Stack Overflow
or by shoren
Published on 2010-05-24T09:33:46Z
Indexed on
2010/05/24
10:51 UTC
Read the original article
Hit count: 198
After using C++ I got used to the concept of Identifier which can be used with a class for the type, provides type safety and has no runtime overhead (the actual size is the size of the primitive). I want to do something like that, so I will not make mistakes like:
personDao.find(book.getId());//I want compilation to fail
personDao.find(book.getOwnerId());//I want compilation to succeed
Possible solutuions that I don't like:
- For every entity have an entity id class wrapping the id primitive. I don't like the code bloat.
Create a generic Identifier class. Code like this will not compile:
void foo(Identifier book);
void foo(Identifier person);
Does anyone know of a better way?
Is there a library with a utility such as this?
Is implementing this an overkill?
And the best of all, can this be done in Java without the object overhead like in C++?
© Stack Overflow or respective owner