https://kotlinlang.org logo
#getting-started
Title
# getting-started
u

ursus

05/23/2022, 2:43 PM
What’s the reason that I cannot typealias a nested class?
typealias BR = foo.bar.base.R
if I then use
BR.
I don’t see
string
,
drawable
etc I can only do
typeealias BR_string = foo.bar.base.R.string
typyealias BR_drawable = foo.bar.base.R.drawable
etc, which is stupid
I don't know if there's a reason but it's been an issue for quite some time
u

ursus

05/23/2022, 2:48 PM
dammit .. thank you!
e

ephemient

05/23/2022, 3:36 PM
instead of typealias, try import aliases:
Copy code
import foo.bar.base.R as BR
then
BR.string
,
BR.drawable
, etc. work
u

ursus

05/23/2022, 3:40 PM
I know but that has bad ux, I need to spell out the import manually first... defeats the purpose
Im trying to do nontransitive R classes in android, where I need to deduplicate R literal with correct import. Changing the name to something unique will be the best dev ux
e

ephemient

05/23/2022, 3:56 PM
in my app, we've been happy to use
import module.R as ModuleR
. I don't understand what you mean by "need to spell out the import manually first" - just let the IDE auto-fill to "module.R.string.foo", then copy the prefix to the import list and replace
t

tseisel

05/23/2022, 4:58 PM
There's also a "Introduce import alias" IDE action that does exactly that
u

ursus

05/23/2022, 5:29 PM
but if you already have the module local import then you need to remember the full package name..
and only then create the alias .. so there is a burden of “knowing” the packagename from memory
@ephemient does IDE allow you to import the base one? If I remove the import, R goes red, I hit alt enter, it automatically imports the local one, doesnt even give option of importing the module R do you really always type out
foo.bar.quax.R.string.meh
and then clean it up with import alias?
e

ephemient

05/24/2022, 12:12 PM
well, I'm basically always in some subpackage, so unqualified
R
doesn't exist anyway. but yes, if I were in the same package name as the Android package, I'd just type it out, it all auto-completes
u

ursus

05/24/2022, 5:36 PM
by type it out you mean enerting the full package name, right?
t

tseisel

05/24/2022, 6:48 PM
Here's an IDE tip that might save your day in Android Studio : Anywhere you'd like to reference a resource, simply type "R", then Ctrl+Space to invoke auto-suggestion. Search the desired qualified R class in the list, then type Enter. Then, you could define an import alias with Alt+Enter then "Introduce import alias". Type in the name of your alias, for example "ModuleR", and you'll define an alias without typing a single package name!
31 Views