Hello, is it possible to achieve this goal without external lint
Copy code
@Target(
AnnotationTarget.PROPERTY,
AnnotationTarget.TYPE,
AnnotationTarget.VALUE_PARAMETER,
)
annotation class StringResource
// formatX only works with @StringResource annotated strings
fun @StringResource String.formatX(vararg arguments: Any): String = format(args = arguments)
fun main() {
Strings().hello.formatX("xxx")
// The below line should be shown as an error
"Hello, %s".formatX("xxx")
}
data class Strings(
@StringResource val hello: String = "Hello, %s",
)
y
Youssef Shoaib [MOD]
03/13/2024, 10:34 AM
Maybe what you want is a value class?
Copy code
@JvmInline value class StringResource(val underlying: String)
fun StringResource.formatX(vararg arguments: Any): String = underlying.format(args = arguments)
o
Omico
03/13/2024, 7:43 PM
That's an ok workaround, but it may not be as inconvenient as before if we add new strings. We need to type an extra S for the StringResource code completion. That's also the reason why I asked such a thing at the beginning.