CLOVIS
06/30/2021, 10:24 AMstatic to the language. The semantic relationship between classes and objects are a beautiful way to remove complexity to the language while adding new features. To my understanding, the proposal exists for two reasons:
1. For performance reasons
2. To allow extension functions on classes that do not have a companion object
I don't think it's a good idea to introduce static for performance reasons (the language should be about semantics, the compiler should handle performance). If it's not possible to do automatically by the compiler (because it would break backward-compatibility, or whatever) then at most it should be a JVM-specific annotation.
I don't think a new keyword is necessary for classes that do not have a companion object. I think Kotlin should allow:
fun Foo.Companion.bar() = ...
even if Foo doesn't have a Companion. Obviously the compiler will have to replace the companion by something else, but that's not really a big deal (it will be invisible in Kotlin code, and extension functions are already a bit weird from Java code, so as long as it's not something extremely strange I think it's alright)ephemient
06/30/2021, 1:52 PMthis within the extension function changes depending on whether the target class has a companion or not at the time of compile? having a fake dangling reference like that seems prone to other consistency issuesRuckus
06/30/2021, 2:10 PMstatic into the language was only one of the three proposed solutions for that issue. Can you also explain how your example compares to namespaces? It seems that would solve exactly your problem in a more elegant and robust way.CLOVIS
06/30/2021, 2:13 PMRuckus
06/30/2021, 2:24 PMNir
06/30/2021, 2:58 PMephemient
06/30/2021, 3:39 PMephemient
06/30/2021, 3:41 PMephemient
06/30/2021, 3:41 PMephemient
06/30/2021, 3:43 PMCLOVIS
06/30/2021, 3:52 PMthis doesn't exist in it?Ruckus
06/30/2021, 4:01 PM[...] a kind of ephemeral object without an instance [...] keeps static helpers grouped together in the source, but removes all the object overhead.
Nir
06/30/2021, 4:07 PMRuckus
06/30/2021, 4:11 PMfun thingFromMap(map: Map<String, *>): Thing (where Thing is from an external library). You could change it to fun Thing.namespace.fromMap(map: Map<String, *>: Thing, and you wouldn't need to worry about the function polluting the global namespace.
(I'm sure that wouldn't be the syntax, but I don't know what it would be, so...)Nir
06/30/2021, 4:52 PMimportNir
06/30/2021, 4:52 PMRuckus
06/30/2021, 4:54 PMNir
06/30/2021, 4:55 PMNir
06/30/2021, 4:55 PMNir
06/30/2021, 4:56 PMephemient
06/30/2021, 4:56 PMRuckus
06/30/2021, 4:56 PMNir
06/30/2021, 4:56 PMNir
06/30/2021, 4:57 PMRuckus
06/30/2021, 4:58 PMNir
06/30/2021, 4:58 PMNir
06/30/2021, 4:59 PMNir
06/30/2021, 5:01 PMpackage in Kotlin, overall. C# namespaces aren't associated with a class, the way that namespaces in Kotlin are proposed to be 🤷ephemient
06/30/2021, 5:02 PMimport java.util; util.Map, but you can import java.util.Map; Map.EntryNir
06/30/2021, 5:02 PMimport java.util and then util.Mapephemient
06/30/2021, 5:02 PMNir
06/30/2021, 5:02 PMephemient
06/30/2021, 5:03 PMpackage java; val util and then it's ambiguous what java.util refers to as a termephemient
06/30/2021, 5:03 PMnamespace as a new concept, it could define what a naming conflict means (probably forbid it)Nir
06/30/2021, 5:03 PMephemient
06/30/2021, 5:04 PMephemient
06/30/2021, 5:04 PMRuckus
06/30/2021, 5:04 PMNir
06/30/2021, 5:04 PMRuckus
06/30/2021, 5:05 PMNir
06/30/2021, 5:05 PMephemient
06/30/2021, 5:07 PMNir
06/30/2021, 5:07 PMRuckus
06/30/2021, 5:07 PMNir
06/30/2021, 5:08 PMRuckus
06/30/2021, 5:09 PMKotlin is very unusual in not giving you the option to use qualified names in your actual codeCould you elaborate on that? As far as I'm aware, you can use fully qualified names in Kotlin. Or was this more about partially qualified names like in the
util.Map example above?Nir
06/30/2021, 5:13 PMNir
06/30/2021, 5:14 PMjava.util.Map but Map is still in the global namespaceNir
06/30/2021, 5:14 PMimport foo then you still need to refer to any entities in foo qualified, i.e. foo.bar will work but bar will notephemient
06/30/2021, 5:14 PMimport java.util.{List, Map}
use std::collections::{Vec, HashMap};
from collections import UserList, UserDict
which is a nice QoL improvementNir
06/30/2021, 5:15 PMfrom foo import bar, baz, qux then now you can use the unqualified names. So, you pick, qualified, or unqualified, or partially qualified if you want, and only the name you want gets brought inNir
06/30/2021, 5:16 PMNir
06/30/2021, 5:16 PMephemient
06/30/2021, 5:18 PM