Advitiay Anand
02/19/2022, 12:18 PMVampire
02/19/2022, 12:26 PMYoussef Shoaib [MOD]
02/19/2022, 12:28 PMRob Elliot
02/19/2022, 12:36 PMclass Foo(val string: String) {
companion object {
operator fun invoke(string: String): Foo = Foo("changed $string")
}
}
fun main() {
println(Foo("initial").string)
}
Without testing it / looking it up I wouldn't know if initial
or changed initial
were printed there, so I'd have thought there was a case for prohibiting the clash. Particularly as if you change the constructor to class private constructor Foo(val string: String)
you actually change the behaviour for all callers, which might produce some unexpected results.Advitiay Anand
02/19/2022, 12:44 PMYoussef Shoaib [MOD]
02/19/2022, 12:45 PMAdvitiay Anand
02/19/2022, 12:46 PMRob Elliot
02/19/2022, 12:49 PMthe IDE will mark the companion invoke as unusedIt doesn't for me, as it happens. 2021.3.2 IntelliJ IDEA 2021.3.2 (Ultimate Edition) with the Kotlin: 213-1.6.10-release-961-IJ6777.52 plugin.
Youssef Shoaib [MOD]
02/19/2022, 12:55 PMephemient
02/19/2022, 12:57 PMVampire
02/19/2022, 12:57 PMFoo.Companion("")
, can you not?ephemient
02/19/2022, 12:57 PMFoo.invoke("")
tooVampire
02/19/2022, 12:58 PMYoussef Shoaib [MOD]
02/19/2022, 1:14 PMfun test(param: Int) {
var param = param
}
even though it is useful at times.
if making the constructor private effects all consumers, well it's just the same as if the implementation of the constructor was changedThis does have precedent in Kotlin already with member and extension functions having different resolution orders even if they have the same signature, which does result in that "switching of calls" if the member is made private, but does the average user expect the same behaviour from companion invokes? Maybe they should, but do they?