I think part of the confusion comes from the example code that shows
Name.Comparator (note capitalized class name). This is an example of extending the class
Name's companion class.
Saying “extending a class with one or more named objects” doesn’t fully explain it either since it sounds like you want to extend the class (the kind) which is just a normal extension property. Extending a class usually means adding functionality that could be used via an instance of that class.
Maybe the confusion is coming from the slightly misleading idea that
object means “static” or has some global-ness or some inherent tie in to a class namespace?
Declaring an object as a top-level declaration does create a singleton. However, it may be helpful to think of this as a top-level anonymous object instead.
A companion object is a member of every instance of a class. It seems like you want the ‘camparator’ to be a a member of the companion, and you don’t want each instance of name to have a comparator.
If that were the case you’d have to say
myName.comparator instead of `Name.comparator’.
The ‘object’ declaration has nothing to do with static. There’s nothing preventing a change that allows you to declare static extensions like you ask but it may not be the best fit conceptually.
Again, ‘object’ has nothing directly tying it to “static” (aka, member owned by every instance) by itself. If you want to call a shared available single instance of camparator using the capitalized type name,
Name.comparator, then you want to extend the
Name class’s companion object. Another way of saying it is: you want to add a property to the
Name class’s namespace. Or a third way, more Java this time: you want a static camparator for the
Your request is already available for classes that have a companion object declared. But right now it’s impossible to extend a class that wasn’t already given a companion object. That’s the reason I suggested your request is the same as those asking to allow extension on companions for classes that don’t have an explicit companion object already declared. You may be able to find more discussion on this topic searching with the companion wording as well.