7

Is there anyway (a workaround) to define type synonyms in Java, similar to the following definition in Scala?

type Row = List[Int];

Though it might not be exactly the same, I was thinking of the following code (I replaced List with ArrayList, since List is an interface in Java):

public class Row extends ArrayList<Integer>{}

Is there any other way to achieve type synonym mechanism in java?

qartal
  • 2,024
  • 19
  • 31
  • 2
    I don't think so. The `extends` is the best you can do. Since `List` (not `List`) is an interface, I think that what you wrote may be good enough, but I'm not sure. If you do this with a _concrete_ type, then to get a type that is as close as possible to being equivalent, you also have to redeclare all the constructors and make them all just `super(arguments);`. – ajb May 31 '15 at 23:11
  • Nope, there is no way. Your method is considered in some circles to be an anti-pattern. –  May 31 '15 at 23:30
  • @ajb I changed List to ArrayList. You are right, List is interface in java. – qartal May 31 '15 at 23:31
  • 1
    "Considered in some circles to be an anti-pattern"... personally, I'd want to see concrete reasons why. In general, though, a class should represent a cohesive concept; creating a class just to rename a type isn't usually the best use, IMHO. And if the class represents a concept, the `ArrayList` would be considered the implementation of that concept, and therefore should be a private field instead of a superclass. – ajb Jun 01 '15 at 00:13
  • 2
    On the other hand, if you have a very complex type like `ArrayList>>>>`, maybe a type rename would enhance readability; I've seen this sort of type in practice, and spelling out the entire `ArrayList` type name multiple times really clutters the code. (But thinking of the concept it represents, and coming up with a new class that represents this concept, would be better.) – ajb Jun 01 '15 at 00:15
  • 2
    Also, there are cases in Java where using arrays and generics together are restricted, and you could use `Row` in a context where using `List` would be an error. That would be a legitimate reason for the type rename, I think. – ajb Jun 01 '15 at 00:16
  • I think my design is going to end up having something like `ArrayList>>>>`, so I am thinking to switch to Scala! if I can not find an easy workaround. – qartal Jun 01 '15 at 00:19
  • 1
    I'd suggest to try with a better delegation / composition. I'd rather not pile up stuff within collections that way... Map -> CustomerDataMap; Map -> FutureMap. You end up with a List ... – m c Jun 03 '15 at 15:38
  • as medveshonok117 said, better composition will give you a clear result than simply throwing every nested generic altogether. And yes. ```extends``` is the only way to it right in Java. – Alexander Jardim Jun 03 '15 at 16:17
  • I would also suggest to take a look at TypeLiteral, e.g. http://stackoverflow.com/questions/8655921/use-of-typeliteral-in-java – m c Jun 05 '15 at 10:34
  • possible duplicate of [Is there a Java equivalent or methodology for the typedef keyword in C++?](http://stackoverflow.com/questions/1195206/is-there-a-java-equivalent-or-methodology-for-the-typedef-keyword-in-c) – Rich Jun 10 '15 at 11:06

1 Answers1

2

Unfortunately not. See Is there a Java equivalent or methodology for the typedef keyword in C++? for an older duplicate of this question, using C++ terminology instead of Scala terminology.

Community
  • 1
  • 1
Rich
  • 15,048
  • 2
  • 66
  • 119