Just came across a curious situation. Suppose `A` ...
# getting-started
m
Just came across a curious situation. Suppose
A
extends
B
and we have two extension funs declared in different packages:
Copy code
fun A.foo()
fun B.foo()
How can we call both of these from the same class/file which is in the same package as
A
? Because as soon as we include the import for
B.foo()
, it will be used for both calls.
v
What about import aliases ?
m
Nice - thanks!
👍 1
I feel there should be some kind of lint warning.
v
Yup, it's a bit misleading.
m
Took me a while to realise how it was possible that an instance with type
A
was resolving to use
B.foo()
instead of
A.foo()
😅 1
I guess at some point, I added a call to
B.foo()
which added the import, which then meant that a pre-existing
a.foo()
switched from resolving
A.foo()
to
B.foo()
p
you can also
(a as B).foo()
to force
B.foo()
to be resolved
m
Hmm, I thought I tried that
I mean, the type was already
A
and so the IDE showed the
as A
as redundant. And anyway, it clicked through to
B.foo()
p
Ah sorry, missed the multiple packages part...
👍 1
You'd need to import the
foo
from the same package additionally (explicit imports have a higher resolution priority)
m
It was solved by just doing:
import packageB.foo as bfoo
instead of
import packageB.foo
. No need to import current package
A.foo
. Although I suppose it might be good for clarity to import both.
p
Unfortunately I think IntelliJ will consider the import redundant and try to remove it 😄
m
Oh, and found a bug - Optimize Imports… (in AS) doesn’t sort import aliases
Actually, Android Studio allowed me to add it fine, presumably because import alias
Hmm, perhaps not a bug - it just puts them to the end
p
It doesn't remove on optimize but it is highlighted as unused 🤔
m
Need to be an import alias
p
alias is probably clearer though and avoids needing the explicit cast