I’m trying to understand what the purpose of species
is. For most classes in the classlib it’s the same as class
, but I see e.g. Collection
has species { ^Array }
. When should one use species and when class, and when should one override the species for a class?
I suspect species
was used to refer specifically to the class of a collection? It’s archaic - I would avoid using it or implementing it. In general, super-class implementations of algorithms that return a new collection will base the type of that collection on this:class
, but sometimes I see this:species
(which maps to this:class
in almost all cases). The Collection
case is a little strange - I think Array is the first non-abstract subclass of Collection, so it makes sense that this would be it’s species, but since it’s not possible to instantiate an abstract class, this is the result you’d get from :class
anyway. This arguably breaks other non-abstract classes that derive from e.g. Collection, since they’d return Array
's for some methods?
aList.collect { ... }
→ Array has messed me up in the past. I don’t think there’s a good reason for that.
hjh
Yeah, some sub-classes of Collection
“override it back”, e.g. LinkedList
does species { ^this.class }
and so does Set
. Dictionary
inherits from Set
so it gets the “usual” implementation.