4 ms·
Imagine 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.
by TheCoelacanth 7y ago
Imagine 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.