Is it possible to add annotations to extension fun...
# announcements
a
Is it possible to add annotations to extension functions? I am currently writing an extension function that returns a string based on the value of an Int:
Copy code
fun Int.toFriendlyName(): String{
    return when(this) {
        PredefinedValue1 -> "PredefinedValue1"
        PredefinedValue2 -> "PredefinedValue2"
        PredefinedValue3 -> "PredefinedValue3"
        else -> willNeverHappen("This value is not supported")
    }
}
However this is problematic as this extension function can now be used on all integers in the scope of the extension functions. I know that the `Int`s that I will be calling this on will be annotated with @Barcode.BarcodeFormat - so is there a way for me to mark that on my extension function so that I can only use it on integers with that specific annotation?
r
afaik it's not possible to do this and if you have full control of code, you should consider using an enum class BarcodeFormat, if I'm reading the code correctly
a
@Roukanken Unfortunately the BarcodeFormat is part of the MLKit for Android šŸ˜ž But that would have been a good shout! I am wondering if I should submit this is a feature request - do you think there would be much value in this?
j
@Andrew the IDE makes it seem like you can call the extension function on any
Int
, but it really must be imported first, which allows you to limit the blast radius. Honestly I'd say this is working as expected. You could put the extension function inside of an object so you can make the context more specific if desired:
Copy code
object BarCodeHelper {
  fun Int.toFriendlyName() = ...
}

with(BarCodeHelper) {
  someInt.toFriendlyName()
}
ā˜ļø 1
a
sounds more like you want the Ints that are actually barcodes to be wrapped in an
inline class
that defines the method - that’s the way to distinguish ā€œspecialā€ numbers/strings etc that have additional behaviour attached to them
r
He actually has ints that are BarcodeFormat enum (think "ISO" "BINARY" or whatever constants are there), and he wants to attach additional behaviour to it The problem here is the library just defines them as int constants and just anotates em with the BarcodeFormat annotations, instead of enum, for some godforsaken reason...
The library is problem here, and dunno if there is any good solution. The only really solid solution I can think of, would be to redefine the enum yourself properly, but dunno how maintanable that is, I havent looked that deeply into the library
a
Thanks so much for the responses folks, really appreciate it! @araqnid - inline classes appear to be in beta and unfortunately I wouldn't be able to use that in the project I am working on but I am going to keep an eye on it! Thanks šŸ’«
a
oh, looks like they want to be able to combine formats together into a bitmask but don’t want to use EnumSet šŸ™„
r
but you could do that with normal enums too ... even without EnumSet ... bit more work in library (eg, add the value to each enum entry, and get it when ya need it), but API would be much much more better
ā˜šŸ» 1