Hacker News new | past | comments | ask | show | jobs | submit login

What would calling such a method look like?



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.


If you know you’re creating a List (it’s there on the LHS) why not write List.empty()?


Here's how it works in Rust. You can imagine this working in Java with reified generics: https://gist.github.com/rust-play/214adf5bd15b18ec2d5c63d97f....


Fascinating! What are the uses of something like this? Is this at all related to concepts or metaprogramming?


You know in this specific example, but not always. You could also do it in generic method where the collection type is parameterized.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: