https://kotlinlang.org logo
#random
Title
# random
m

Michael Bichlmeier

10/15/2023, 11:38 AM
Is there a way (or better what is the best way) to do get rid of the
prefix
in each icon element?
Copy code
object Icons {
    private const val PREFIX = "drawable/"
    const val LOGO = PREFIX + "icon.xml"
    const val AGAIN = PREFIX + "again.xml"
}
Is there a better way and how can I reduce the prefix redundancy? I want to call
Icons.LOGO
which represents
drawable/icon.xml
and this with many other types.
y

Youssef Shoaib [MOD]

10/15/2023, 2:24 PM
Copy code
interface Prefixing {
  val prefix: String
  operator fun String.getValue(thisRef: Prefixing, property: KProperty<*>) = prefix + this
}

object Icons: Prefixing {
  override val prefix = "drawable/"
  val LOGO by "icon.xml"
  val AGAIN by "again.xml"
}
🙌 2
Dealing with raw strings feels a little precarious though, so maybe you want to have some value class XmlFile that must be constructed with a prefix and a filename, and which is simply represented by a single string. Then, you can define new methods that replace the Android ones but only operate on the XmlFile typed values
l

Loney Chou

10/15/2023, 3:32 PM
Maybe wait for const functions?