How do I pass the constructor of a nested class? ...
# announcements
j
How do I pass the constructor of a nested class?
Copy code
class Base() {
    class Nested() {
    }
}

funWithCtor(::Base) // works
funWithCtor(::Base.Nested) // doesn't work
funWithCtor(::Nested) // works if: import Outer.Nested
m
Base::Nested
j
Hmm, that doesn't work right for me either
d
Works fine for me: https://pl.kotl.in/IrPAmrRSX
m
@Jorrit what does “doesn’t work” mean here?
j
Copy code
fun funWithCtor(ctor: KFunction1<Param, ReturnType>) {
    // ...
}
This gives a type mismatch if using the nested constructor, but works fine with the base class constructor. Need to use KFunction1 rather than () -> Any. Having a bad time copy/pasting the error message.
m
That doesn’t even work with
Base
in your example, because
KFunction1
expects 1 parameter. Neither
Base()
nor
Base.Nested()
have a parameter.
d
Still works fine for me: https://pl.kotl.in/gylSVL9Gz
j
Sorry, Marc, just trying to simplify from my full code. Will see about reproducing it at pl.kotl.in
👌 2
https://pl.kotl.in/eGDRG0nMd Interestingly, this works fine on the playground, but IDEA is complaining: "Type mismatch: inferred type is Param but Outer was expected"
m
Are you sure you’re not dealing with an
inner class
somewhere?
j
No, I straight up copy/pasted that snippet from playground into IDEA
m
ah funny, I see different code in the playground after resetting site data I see the correct one now 😄
j
image.png
m
Does it compile in the IDE? If yes then the difference is because of New Type Inference.
Also doesn’t work in the Playground with Kotlin 1.4.0-rc
j
Ah yes, never used the playground before (new to Kotlin). I see indeed when switched to 1.4.0-rc on the playground with the JS target (which I'm using in IDEA) it gives the same error.
I guess this is one for YT then?
m
Yeah looks like a bug.
Outer::Nested::invoke
works 🤔
j
👍 2