5 ms·
I really wish Java had associated types and static methods as parts of interfaces (i.e. constructors as part of the interface). Those two features make complex
by slaymaker1907 7y ago
I really wish Java had associated types and static methods as parts of interfaces (i.e. constructors as part of the interface). Those two features make complex architectures much easier to design and use.
- chrisseaton 7y agoInterfaces can have static methods.
- james-mcelwain 7y agoI believe they mean static methods as part of the interface signature, e.g. to define constructor/factory methods. AFAIK there are issues with how Java handles dynamic dispatch that prevent this.
- chrisseaton 7y agoWhat would calling such a method look like?
- TheCoelacanth 7y agoImagine that Collection had a static method `empty` that created an empty collection of the appropriate type depending on the type you are assigning it to, e.g. `List list = Collection.empty();` would create a list, `Set set = Collection.empty();` would create a set, and so on. This would be fairly similar to how things work in Haskell.
- chrisseaton 7y agoIf you know you’re creating a List (it’s there on the LHS) why not write List.empty()?
- james-mcelwain 7y agoHere's how it works in Rust. You can imagine this working in Java with reified generics: https://gist.github.com/rust-play/214adf5bd15b18ec2d5c63d97ff8ba2c https://gist.github.com/rust-play/214adf5bd15b18ec2d5c63d97f....
- verdagon 7y agoFascinating! What are the uses of something like this? Is this at all related to concepts or metaprogramming?
- TheCoelacanth 7y agoYou know in this specific example, but not always. You could also do it in generic method where the collection type is parameterized.