Is there a good explanation somewhere of why `Map`...
# announcements
n
Is there a good explanation somewhere of why
Map
does not inherit from
Collection
?
z
It doesn't in Java, so it can't in kotlin.
Why it doesn't in Java is another question, and I don't know the answer.
n
ah that's a real shame
i thought things like the way MutableMap inherits from Map is already different from Java though?
s
Why should it inherit from Collection?
@stephanmg i'd have to look more carefully at the collection interface to see what's specific to collection. It is clearly iterable though.
s
collection extends Iterable
Collection
n
I understand that
I expected Map to extend collection, the same as List
but it doesn't even extend Iterable
s
well since you posted the StackOverflow question
don’t you agree with the accepted answer?
n
Uh, no, it's pretty terrible? and dated
Simply the fact that there's an obvious implementation of iteration, and
map
, should tell you that
Map
should at least be Iterable
z
I could certainly see a counter argument that a map can be seen as a collection of entries, but I guess that is a secondary aspect of it that java didn't want to highlight by encoding it in the type system?
n
Sure, it's clear that java didn't want to, the question is why?
z
Map and MutableMap work because they're actually the same JVM type.
n
So, Collection has a size/length attribute, right
Maps do as well
s
@Nir I agree with you
but I presume by the time it was, as they said, a design decision, to differentiate…
I do not know why.
s
If you read carefully they (Java FAQ) write: “we feel”.
so this is not set in stone in general
f
You’re probably looking for Collection#entrySet which is a collection.
n
uh no, I'm not 🙂
s
@Nir I understand your conceptional problem
n
What's annoying about all this is that currently if you want to implement something "map like", you now have to do it 3 times
s
that’s true
n
once for iterable, once for sequence, and once for map
sorry, for Map 🙂
all that Collection interface adds is: • size • contains • containsAll • isEmpty • Iterator
All of which make perfect sense
for a Map
I wonder how reasonable it would be to write your own
ReadOnlyMap
which inherited from both
Map
and
Iterable
Map
and
Collection
I mean
s
@Nir: you are very certain about your assertions
😄
at least the original developers would disagree (Java)
but IMHO it makes sense
but this is just my point of view
n
It's possible they would, I can't say, and maybe they actually had good reasons, but that SO answer does not show good reasons
The developers of basically all other languages would disagree :-) associative maps always conform to the collection/container/iterable interface
In every other language I have used
Python, C++, Rust, to name a few
s
Yeah
as I said they write “feel”
and this is like 15 years ago probably
so maybe today they would “feel” other way
as you maybe
🙂
n
heh maybe. Most of that quote is actually discussing alternative things that very obviously make no sense, like having Collection inherit from Map.
There's only like 1 sentence about why Map inheriting Collection isnt' good
z
I'm guessing there are historical reasons for java - eg before generics were added in (1.5?) it may not have been clear which parts of the interface applied to keys, values, or entries.
n
yeah, possibly there's more API in collections in Java than in Kotlin.
From what I can glean from the answer, it seems like it's at least partly that they just thought super heavily in terms of mutation
if you don't have something like
map
, then yes, you might question what the purpose of looking at associative containers that way is
instead push people to be explicit about what they want to iterate over 🤷
On the off chance anyone cares: I had my ImmutableMap class implement both Map and Collection, trivially implemented the methods in question, everything seems to be fine
There's actually no ambiguity errors,.almost all of iterable is extension methods, so the member functions get priority
Eg iterable map vs Map.map
s
Nir, did you discuss with Java folks?
Did they offer an answer?
n
No. where should I go to discuss with Java folks?
s
I do not know.
Just a suggestion
n
i could see if there's a java slack or something. I feel like the odds of getting a good answer are low though.
s
well @Nir if you do not try, you never know 😄
I presume it will be a fruitful discussion
did you consider using Usenet?
comp.lang.java?