Is there any way we can say that a companion objec...
# language-proposals
k
Is there any way we can say that a companion object shouldn't be used directly? For example,
String
has a companion object whose only purpose is to allow extension functions, but unfortunately I've seen people who are beginners in Kotlin writing code like
val x = foo()?: String
- presumably thinking
String
is the empty string, and not realising that it's the companion object. I can't think of any syntax that would allow us to mark such objects as not allowable to be used in this way.
n
beginners in Kotlin writing code like
They must have been veeery beginners, and probably the code haven't compiled anyway... :)
whose only purpose is to allow extension functions
I think this is a related issue: https://youtrack.jetbrains.com/issue/KT-11968
k
They must have been veeery beginners, and probably the code haven't compiled anyway... :)
Not at all, they had been using Kotlin for more than a month, and the code compiled perfectly. It just didn't appear quite right when used like
"... $x ..."
when x was the companion object. Thanks for the link to that interesting related issue.
m
Are you suggesting that it would be better to have the ability to leave the option of using companion object directly only for extension functions? @Klitos Kyriacou
k
No, even extension functions don't need the ability to use the companion object. They use it for syntactic purposes only; not for access to the object itself. For example
String.format(...)
relies on the companion object (it's actually
String.Companion.format
) and it is a member method of the companion object, but this companion object is not only empty but its presence is not even cared about by its extension functions like
format
. So perhaps what I'm imagining might be an idea is some keyword like
phantom companion object
. Such a keyword "phantom" would prefer you from doing pointless things like
var x = String
because
String
does not really exist, it's only imagined to exist in order to enable certain syntax. (I know that such a companion object possibly does exist, but if we had such a keyword the compiler would not have to create such a companion object.)
m
ok well. what
phantom
keyword actually would do? allow the companion object to be only used when declaring extension functions for it? how would we use the companion object for accessing such extension functions if it "doesn't even exist"? Would it be possible to declare members in such a companion object?
k
The
phantom
keyword would allow you to define extension functions, and to call them. So you could do
var mystring = String.format("%4d", n)
but you'd get a compilation error if you tried
var x = String
. In the former case,
String.format
is a member of the companion object (which is phantom so exists only in our imagination). Phantom objects cannot have members such as properties since they don't actually exist and you need memory to store those properties (perhaps compile-time constant properties are ok).
m
its cool, but it would be better to bring this keyword to the level of functions right? something like this
Copy code
phantom fun String.customFormat(...) = ...
k
No. I think I haven't described my idea clearly enough. The
phantom
keyword should certainly be part of the companion object's declaration, not the function's. The point of the
phantom
keyword is to prevent pointless usage like
var x = String
. Extension functions can continue to be defined and used the same way as they are with existing, normal companion objects, and such extension functions wouldn't care whether a companion object is a phantom one or not.
m
okay, it would be interesting to read more clear explanation
r
Sorry for jumping in, but I'm just reading along and I think I'm missing something. If you could explain what the difference between your proposed
phantom
keyword and the already proposed
namespace
feature is, that would probably clear some things up.
k
I've heard about
namespace
(and have used the C++
namespace
keyword) but I haven't read about it in depth. Once I do, I'll come back here.
👍 2
g
@Klitos Kyriacou Please, check this channel description
👍 1
m
@gildor we are complaining about what's missing.
g
Sure, but this channel is not for complains, questions, it’s about proposals, #language-evolution is perfect place for it
👍 2
m
Alright, I guess the OP will move to the mentioned channel. Also, #language-proposals description says that the channel can include complains.
g
Fair enough
I think we should update the description, thanks for pointing this out
👍 1