Reputation: 7949
Coming from a C and C++ background, I found judicious use of typedef
to be incredibly helpful. Do you know of a way to achieve similar functionality in Java, whether that be a Java mechanism, pattern, or some other effective way you have used?
Upvotes: 288
Views: 147437
Reputation: 8333
Kotlin supports type aliases https://kotlinlang.org/docs/type-aliases.html. You can rename types and function types.
Upvotes: 1
Reputation: 8137
As noted in other answers, you should avoid the pseudo-typedef antipattern. However, typedefs are still useful even if that is not the way to achieve them. You want to distinguish between different abstract types that have the same Java representation. You don't want to mix up strings that are passwords with those that are street addresses, or integers that represent an offset with those that represent an absolute value.
The Checker Framework enables you to define a typedef in a backward-compatible way. It works even for primitive classes such as int
and final classes such as String
. It has no run-time overhead and does not break equality tests.
Section Type aliases and typedefs in the Checker Framework manual describes several ways to create typedefs, depending on your needs.
Upvotes: 4
Reputation: 561
Perhaps this could be another possible replace :
@Data
public class MyMap {
@Delegate //lombok
private HashMap<String, String> value;
}
Upvotes: 5
Reputation: 9
In some cases, a binding annotation may be just what you're looking for:
https://github.com/google/guice/wiki/BindingAnnotations
Or if you don't want to depend on Guice, just a regular annotation might do.
Upvotes: -1
Reputation: 4137
As others have mentioned before,
There is no typedef mechanism in Java.
I also do not support "fake classes" in general, but there should not be a general strict rule of thumb here:
If your code for example uses over and over and over a "generic based type" for example:
Map<String, List<Integer>>
You should definitely consider having a subclass for that purpose.
Another approach one can consider, is for example to have in your code a deceleration like:
//@Alias Map<String, List<Integer>> NameToNumbers;
And then use in your code NameToNumbers and have a pre compiler task (ANT/Gradle/Maven) to process and generate relevant java code.
I know that to some of the readers of this answer this might sound strange, but this is how many frameworks implemented "annotations" prior to JDK 5, this is what project lombok is doing and other frameworks.
Upvotes: 11
Reputation: 57325
You could use an Enum, although that's semantically a bit different than a typedef in that it only allows a restricted set of values. Another possible solution is a named wrapper class, e.g.
public class Apple {
public Apple(Integer i){this.i=i; }
}
but that seems way more clunky, especially given that it's not clear from the code that the class has no other function than as an alias.
Upvotes: -6
Reputation: 99949
Typedef allows items to be implicitly assigned to types they are not. Some people try to get around this with extensions; read here at IBM for an explanation of why this is a bad idea.
Edit: While strong type inference is a useful thing, I don't think (and hope we won't) see typedef
rearing it's ugly head in managed languages (ever?).
Edit 2: In C#, you can use a using statement like this at the top of a source file. It's used so you don't have to do the second item shown. The only time you see the name change is when a scope introduces a name collision between two types. The renaming is limited to one file, outside of which every variable/parameter type which used it is known by its full name.
using Path = System.IO.Path;
using System.IO;
Upvotes: -7
Reputation: 211
Really, the only use of typedef that carries over to Javaland is aliasing- that is, giving the same class multiple names. That is, you've got a class "A" and you want "B" to refer to the same thing. In C++, you'd be doing "typedef B A;"
Unfortunately, they just don't support it. However, if you control all the types involved you CAN pull a nasty hack at the library level- you either extend B from A or have B implement A.
Upvotes: 5
Reputation: 57668
If this is what you mean, you can simply extend the class you would like to typedef, e.g.:
public class MyMap extends HashMap<String, String> {}
Upvotes: 120
Reputation: 609
There is no need for typedef in Java. Everything is an Object except for the primitives. There are no pointers, only references. The scenarios where you normally would use typedefs are instances in which you create objects instead.
Upvotes: -14
Reputation: 7178
There is no typedef in java as of 1.6, what you can do is make a wrapper class for what you want since you can't subclass final classes (Integer, Double, etc)
Upvotes: 15
Reputation: 625237
Java has primitive types, objects and arrays and that's it. No typedefs.
Upvotes: 126