3 ms·
Haskell has this, it's called newtype. 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 a
by qcnguy 1y ago
Haskell has this, it's called newtype.
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.
- buerkle 1y agoBut then you are representing two distinct types as the same underlying type, String. 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.
- qcnguy 1y agoIt has to work that way or else you can't use the standard library. What you want to block is 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".