FalseHonesty
08/19/2020, 4:16 AMTypeResolutionInterceptorExtension
will become non-internal anytime soon? I'd like to be able to use it in my compiler plugin 🙂Roman Artemev [JB]
08/19/2020, 7:38 AMFalseHonesty
08/19/2020, 1:26 PMabstract class Parent {
fun action() = apply {
// do action
}
}
which then gets extended by a bunch of children, like
class Child : Parent() {
}
FalseHonesty
08/19/2020, 1:27 PMchild.action()
to return the instance of Child
FalseHonesty
08/19/2020, 1:28 PMFalseHonesty
08/19/2020, 2:30 PMChild
?Roman Artemev [JB]
08/19/2020, 2:53 PMabstract class Parent<T: Parent> {
open fun action(): T = apply {
// do action
}
}
class Child : Parent<Child>() {
// override action(): Child
}
So in that case you could always inject override member fun action(): Child
which will be properly compiled into what you exactly need including bridge method.FalseHonesty
08/19/2020, 2:58 PMParent
in the bound for T
needs a type argumentFalseHonesty
08/19/2020, 2:59 PMParent
would now need to have a type specified, even if that is just *
Roman Artemev [JB]
08/19/2020, 2:59 PMbut that wouldn’t suffice would itIf you goal is to make sure that types are correct it should to be done automatically since compiler inserts “implicit cast” wherever types are mismatch.
FalseHonesty
08/19/2020, 3:02 PMRoman Artemev [JB]
08/19/2020, 3:03 PMaction
in Child
has to override it from Parent
?Roman Artemev [JB]
08/19/2020, 3:03 PMFalseHonesty
08/19/2020, 3:05 PMChild
, rather, whenever i had an instance of Child
and called any method from Parent
on it, it would return the type Child
instead of Parent
, because I know that in my library it will always return the exact same instance that it was called withRoman Artemev [JB]
08/19/2020, 3:06 PMclass Child : Parent() {
override fun action(): Child { .. }
}
Roman Artemev [JB]
08/19/2020, 3:09 PMIrGenerationExtension
if you just add cast like this
use1(p.action()) -> use1(cast<Parent>(p.action()))
use2(c.action()) -> use2(cast<Child>(c.action()))
FalseHonesty
08/19/2020, 3:10 PMParent
has a load of methods that would need overriding. Additionally, Parent
and Child
are classes in a library, and it was suggested in the 1.4 release notes that libraries shouldn't compile with IR quite yet, but I guess I can require that the library use the IR compilerFalseHonesty
08/19/2020, 3:11 PMuse1
and that code block? I'm slightly confused to the context where that would be usedFalseHonesty
08/19/2020, 3:13 PMFalseHonesty
08/19/2020, 3:21 PMval child: Child
child = Parent().action()
the code doesn't even make it to the IrGenerationExtension because it fails type inference, saying that the inferred type is Parent
, not Child
, so I can't automatically insert a cast, somehow I'd need to tell the compiler the type earlier, and as you said, it might implicitly castRoman Artemev [JB]
08/19/2020, 3:47 PMuse1/2(..)
is just a meta of any possible usage. In our snippet such usage is assign into child
Roman Artemev [JB]
08/19/2020, 3:49 PMRoman Artemev [JB]
08/19/2020, 3:51 PMFalseHonesty
08/19/2020, 3:53 PMParent
that returns the type Parent
, but I think the general point is the same, yes.Roman Artemev [JB]
08/19/2020, 3:55 PMFalseHonesty
08/19/2020, 3:59 PMval myChild = Child().action() as Child
because later on, code often needs to use functions declared specifically in the child,
myChild.childAction()
which means the myChild
variable needs to be of type Child
. It's not too bad writing it with casts, but there are also a lot of parent functions that are infix, have lambdas, etc. and sometimes the assignment expression become complex, which means we need to wrap the left hand side of the cast in parentheses, just making the code much worse to readRoman Artemev [JB]
08/19/2020, 4:01 PMFalseHonesty
08/19/2020, 4:05 PMParent
class? If so, yeah I thought that might be the case, which is why I started looking into making a compiler plugin in the first placeRoman Artemev [JB]
08/19/2020, 4:10 PMFalseHonesty
08/19/2020, 4:28 PMRoman Artemev [JB]
08/19/2020, 4:29 PMFalseHonesty
08/20/2020, 1:21 AMRoman Artemev [JB]
08/20/2020, 8:25 AMFalseHonesty
08/20/2020, 1:25 PM