Klitos Kyriacou
05/17/2022, 10:17 AMString
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.Norbi
05/17/2022, 10:35 AMbeginners in Kotlin writing code likeThey must have been veeery beginners, and probably the code haven't compiled anyway... :)
whose only purpose is to allow extension functionsI think this is a related issue: https://youtrack.jetbrains.com/issue/KT-11968
Klitos Kyriacou
05/17/2022, 11:52 AMThey 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.Mikhail
05/17/2022, 1:49 PMKlitos Kyriacou
05/17/2022, 2:04 PMString.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.)Mikhail
05/17/2022, 2:08 PMphantom
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?Klitos Kyriacou
05/17/2022, 2:26 PMphantom
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).Mikhail
05/17/2022, 2:35 PMphantom fun String.customFormat(...) = ...
Klitos Kyriacou
05/17/2022, 2:39 PMphantom
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.Mikhail
05/17/2022, 2:52 PMRuckus
05/17/2022, 4:36 PMphantom
keyword and the already proposed namespace
feature is, that would probably clear some things up.Klitos Kyriacou
05/17/2022, 4:39 PMnamespace
(and have used the C++ namespace
keyword) but I haven't read about it in depth. Once I do, I'll come back here.gildor
05/18/2022, 7:18 AMMikhail
05/18/2022, 7:21 AMgildor
05/18/2022, 7:22 AMMikhail
05/18/2022, 7:30 AMgildor
05/18/2022, 8:14 AMgildor
05/18/2022, 8:14 AM