3 ms·
Some of these, like reified generics, would be great, and it's sad it's too late to add them to the JVM. Some of these, like more expressive imports, seem prett
by twic 11y ago
Some of these, like reified generics, would be great, and it's sad it's too late to add them to the JVM. Some of these, like more expressive imports, seem pretty pointless given that we use IDE's. But some of these are quite un-Javaish.
Structural typing would really go against the grain of the language, and i have a very hard time believing it would actually be useful. What method name is currently widely used with the same semantics across many classes, without being specified by an interface or base class? I can't think of one. There used to be close(), but that's been dealt with. Are there others?
Collection literals seem like a poor idea when there are so many different collection implementations. If i say:
List<Integer> list = {1, 2, 3};
What kind of list do i get? ArrayList, as that's the usual go-to? A foot-shooting opportunity for anyone doing concurrent programming! And a frustrating missed chance for anyone with a functional bent who would prefer an immutable list. Moreover, it seems unnecessary when you could easily just add constructors or factory methods like:
List<Integer> list = new ArrayList(1, 2, 3);
List<Integer> list = ArrayList.of(1, 2, 3);
Which leave the choice in the hands of the programmer for only a minimum of extra syntax.
Maps are a bit harder, because you need a way to describe pairs. In most projects i work on, i end up creating some helper methods that let me write:
Map<String, Boolean> map = map(entry("Java", true), entry("Go", false));
And i wish there was a better syntax for that.
Something like algebraic data types would be good, though. You can already implement un-extendable classes using mild hacks:
public abstract class Shape {
private Shape() {}
public static class Circle {
public static Circle of(int radius) {
return new Circle(radius);
}
public final int radius;
private Circle(int radius) {
this.radius = radius;
}
}
public static class Rectangle {
public static Rectangle of(int width, int height) {
return new Rectangle(width, height);
}
public final int width, height;
private Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
}
}
Because the constructors are all private, so putative subclass can't call them. But you don't get pattern matching, and of course it's all rather verbose.
There are various madmen out on the edge of town trying to create terse approaches to this by meddling with forces beyond mortal knowledge:
https://github.com/poetix/mtuples https://github.com/poetix/mtuples
But i can't see that going mainstream.
- RyanHamilton 11y agoFYI Guava gives you those constructors, the map is: ImmutableMap.of(Key, Value, Key, Value...); For ones with more than 4 values you will need: ImmutableMap<String, Integer> WORD_TO_INT = new ImmutableMap.Builder<String, Integer>() .put("one", 1) .put("two", 2) .put("three", 3) .build();
- twic 11y agoYes, that's the most orthodox Java solution to this. It's not hard to do slightly better (IMHO!), though: public class MapBuilder<K, V> { public static <K, V> MapBuilder<K, V> with(K key, V value) { return new MapBuilder<K, V>().and(key, value); } private List<Map.Entry<K, V>> entries = new ArrayList<>(); public MapBuilder<K, V> and(K key, V value) { entries.add(new AbstractMap.SimpleEntry<>(key, value)); return this; } public Map<K, V> build(Map<K, V> map) { for (Map.Entry<K, V> entry : entries) { map.put(entry.getKey(), entry.getValue()); } return map; } public Map<K, V> build(Supplier<Map<K, V>> mapSupplier) { return build(mapSupplier.get()); } } Which lets you write: Map<String, Boolean> map = with("Java", true).and("Go", false).build(new HashMap<String, Boolean>()); Map<String, Boolean> map2 = with("Java", true).and("Go", false).build(Map::new); I don't have Java 8 on the machine i'm on right now, so apologies if the second example doesn't compile; it might need more manifest types on it.
- Pyxl101 11y agoThe visitor pattern essentially gives you pattern matching for your example. It can even give you destructuring pattern matching, if you wish. With a fixed hierarchy, each visit method handles one concrete case, and its signature can destructure fields and pass them individually. It's a hoop to jump through but not so bad.
- twic 11y agoVery true, but you have to write it all by hand, so it's the usual tedious and error-prone boilerplate until IDEs learn to autogenerate it. It would be nice to have an automatic solution like you get with real ADTs.