javaru
02/01/2021, 2:12 PMthis
being used?
In my IntelliJ Plugin have this:
fun Project.foo {
...
service<SomeApplicationService>().bar()
}
The com.intellij.openapi.components.service.kt
file has two service
functions:
inline fun <reified T : Any> service(): T = ApplicationManager.getApplication().getService(T::class.java)
inline fun <reified T : Any> Project.service(): T = getService(T::class.java)
Because I'm inside a Project
extension function, it is implicitly calling this.service<SomeApplicationService>().work()
and thus the Project.service()
extension function rather than service()
. I was able to workaround by creating a separate applicationService()
convenience function. But I have to imagine this is not an unheard of situation and was wondering if there is some syntax to say not to use the implicit this
. It's a top-level function, to I'm not aware of any this@
syntax for this case.Vampire
02/01/2021, 2:17 PMVampire
02/01/2021, 2:17 PMdmcg
02/01/2021, 2:29 PMthis@functionName
?Animesh Sahu
02/01/2021, 2:35 PMCan you not say this@functionName ?
How would that refer to top-level function blob thinking upside down
Vampire
02/01/2021, 2:35 PMthis@foo
? That would be the same as the implicit this
dmcg
02/01/2021, 2:37 PMjavaru
02/01/2021, 2:45 PMthis@service
shows as invalid syntax.
Thanks Björn (@Vampire)
Maybe if you alias the other methodHmmm... I'll try that. I also thought of just using a wrapper function but just rewrote it with a different name since its a simple function. I had a suspicion that it was not directly possible (other than using a wrapper function) as it is a bit of a corner case. But sometimes it turns out that the Kotlin designers thought of the corner cases. So I figured I'd check.
javaru
02/01/2021, 2:47 PMVampire
02/01/2021, 2:48 PMVampire
02/01/2021, 2:48 PMVampire
02/01/2021, 2:48 PMAnimesh Sahu
02/01/2021, 2:50 PM::func
(c++) but that's reserved for function reference blob thinking upside down.javaru
02/01/2021, 2:51 PMDid you actually use the alias?Yes.
import com.intellij.openapi.components.service as appService
fun Project.foo() {
appService<SomeApplicationService>().bar()
}
Or are both functions in the same package?Not only same package, but same file.
Animesh Sahu
02/01/2021, 2:51 PMVampire
02/01/2021, 2:52 PMjavaru
02/01/2021, 2:53 PMVampire
02/01/2021, 2:53 PMprivate inline fun <reified T : Any> appService() = service<T>()
fun Project.foo {
...
appService<SomeApplicationService>().bar()
}
javaru
02/01/2021, 2:55 PMAnimesh Sahu
02/01/2021, 2:57 PMVampire
02/01/2021, 2:59 PMjavaru
02/01/2021, 3:09 PMNir
02/01/2021, 3:17 PMthis@null
for thisNir
02/01/2021, 3:17 PMthis@T
for cases where you have multiple T possible as the receiver and want to select oneAnimesh Sahu
02/01/2021, 3:18 PMthis
?Animesh Sahu
02/01/2021, 3:19 PMAnimesh Sahu
02/01/2021, 3:19 PMjavaru
02/01/2021, 3:20 PMUnit.funciton()
would work. Basically declaring no receiver.Nir
02/01/2021, 3:21 PMNir
02/01/2021, 3:21 PMNir
02/01/2021, 3:22 PMthis
because we already have syntax to force lookup on a specific receiver type when there is ambiguityNir
02/01/2021, 3:23 PMNir
02/01/2021, 3:23 PMimport foo as
statement a little more power, it is a bit weird that if you have overloaded things you just get all of them imported with no controlNir
02/01/2021, 3:24 PMAnimesh Sahu
02/01/2021, 3:24 PMlabel@ for(...) { break@label }
🤔 this@
does makes some sense, but like I like to have a different operator, this seems to refer to current scope...Animesh Sahu
02/01/2021, 3:25 PMnoreceiver@func
lel, but something short/more conciseNir
02/01/2021, 3:25 PMthis@
does. In this case, we want to force no receiver, but we are still choosing the receiver.javaru
02/01/2021, 3:25 PMthis
in the function and thus you would have to declare this
when you want it rather than having to declare in some strange way to not use this
.Animesh Sahu
02/01/2021, 3:26 PMexplicit@function
Nir
02/01/2021, 3:27 PMjavaru
02/01/2021, 3:27 PMNir
02/01/2021, 3:27 PMNir
02/01/2021, 3:28 PMNir
02/01/2021, 3:28 PMAnimesh Sahu
02/01/2021, 3:29 PMNir
02/01/2021, 3:30 PMprivate void foo();
Nir
02/01/2021, 3:30 PMprivate:
Nir
02/01/2021, 3:30 PMNir
02/01/2021, 3:30 PMAnimesh Sahu
02/01/2021, 3:30 PMNir
02/01/2021, 3:31 PMNir
02/01/2021, 3:31 PMNir
02/01/2021, 3:31 PMNir
02/01/2021, 3:33 PMjavaru
02/01/2021, 3:36 PMAnimesh Sahu
02/01/2021, 3:37 PMpublic class Main
{
public:
void test() { System.out.println("World"); }
static void main(String[] args) {
System.out.println("Hello ");
test();
}
}
This doesn't seem to compile on Java 8 🤔Nir
02/01/2021, 3:40 PMNir
02/01/2021, 3:40 PMRuckus
02/01/2021, 4:14 PMcom.intillij.openapi.components.service.getService<SomeApplicationService>().bar()
Vampire
02/01/2021, 4:14 PMVampire
02/01/2021, 4:15 PMRuckus
02/01/2021, 4:16 PMAnimesh Sahu
02/01/2021, 4:16 PMVampire
02/01/2021, 4:17 PMRuckus
02/01/2021, 4:19 PMjavaru
02/01/2021, 4:42 PMjavaru
02/01/2021, 4:47 PMNir
02/01/2021, 4:51 PMNir
02/01/2021, 4:51 PMthis
for the rest of the functionVampire
02/01/2021, 4:52 PMNir
02/01/2021, 4:52 PMNir
02/01/2021, 4:53 PMNir
02/01/2021, 4:54 PMNir
02/01/2021, 4:55 PMVampire
02/01/2021, 4:56 PMVampire
02/01/2021, 4:56 PMjavaru
02/01/2021, 4:57 PMfun Project.foo {
...
com.intellij.openapi.components.service<SomeApplicationService>().bar()
}
Thanks @Ruckus 👍
So I'm thinking maybe I should change the issue request to be on of documenting how to handle this corner case rather than adding new syntax.Vampire
02/01/2021, 4:58 PMVampire
02/01/2021, 4:58 PMjavaru
02/01/2021, 5:03 PMNir
02/01/2021, 5:06 PM