by qcnguy · 1y ago · 0 comments
In OOP languages as long as the type you want to specialize isn't final you can just create a subclass. It's cheap (no additional wrappers or boxes), easy, and you can specialize behavior if you want to.
Unfortunately for various good reasons Java makes String final, and String is one of the most useful types to specialize on.
MyType extends String;
void foo(String s);
foo(new MyType()); // is valid
Leading to the original problem. I don't want to represent MyType as a String because it's not. StringUtils.trim(String foo);
but myApp.doSomething(AnotherMyType amt);
The latter is saying "I need not any string but a specific kind of string". StringUtils.trim(MyType.toString());