rook
02/08/2022, 4:26 PMFoo's ext
, whereas the output of line 24 is Bar's ext
. I was under the impression that foo as? Bar ?: foo
was effectively the same type inference logic as if (foo is Bar) foo else foo
.
Can someone shed some light on the differences here?Tim Oltjenbruns
02/08/2022, 9:05 PM(foo as? Bar).ext()) ?: foo.ext()
would have been the same as if (foo is Bar) foo.ext() else foo.ext()
The issue is with doing (foo as? Bar ?: foo).ext()
. Even though foo gets smart-cast to Bar, it gets downcast again to fit with the upper bound of Foo again.
And because extension functions are statically dispatched, which function is executed gets decided at compile time. Foo.ext() is chosen over Bar.ext().