nisrulz
05/26/2021, 12:31 PM@OptIn
annotation.
My usecase is that when we start using non-propagating opt-in, lets say internal to library in a multi-module library project, then there is no ability to provide reasoning for why we are @OptIn
. In this particular case the reasoning would be “Internal to main library” for example. The reason can vary though. The actual annotation of say ShinyNewAPI
does have the message via the @RequireOptIn
annotation, like so
@RequiresOptIn(message = "This API is experimental. It may be changed in the future without notice.")
However this message is for clients who try to use the class marked with the annotation. When writing code for internal modules, the reason to opt in could vary. Maybe it could be like “Donot want to propagate to client code, but want to use code from another sub module”. This can be fixed by adding a code comment, but I was thinking if it could be part of the Annotation somehow, so one can wrap a new annotation with it and then use that, thus not requiring to add the message everytime.
// Function doSomething uses API marked with ShinyNewAPI and is also
// required to opt in. Here, we choose a non-propagating opt-in, because the
// API is used in the function body and it should not concern our clients
@OptIn(ShinyNewAPI::class)
fun doSomething() {
val foo = Foo()
foo.bar()
}
Please follow more in thread.nisrulz
05/26/2021, 12:32 PMelizarov
05/26/2021, 1:21 PMnisrulz
05/26/2021, 1:57 PMRequireOptIn
. Something like a warning that shows up when running from the terminal.
An example that comes to mind is highlighting usage of this annotation in code.elizarov
05/26/2021, 1:58 PMnisrulz
05/26/2021, 2:01 PMelizarov
05/26/2021, 2:03 PMelizarov
05/26/2021, 2:04 PMnisrulz
05/26/2021, 2:07 PMAnnotationRetention
would be set to SOURCE
. So yes, still internal nothing going out to clients.nisrulz
05/26/2021, 2:10 PMgildor
05/27/2021, 9:34 AMnisrulz
05/27/2021, 11:05 AMmessage
field in the OptIn
annotation that describes why did they choose to Suppress the RequiresOptIn
warning. Afterall they can just the say @ShinyNewAPI
on that same class to stop the error/warning. However the usage of OptIn
is used to suppress the warning from bubbling up to Clients. With no field for reason/message it just goes down to guess work/siloed knowledge with a team member.
I believe the best way for me to do it is add a specific kind of a comment, // REASON_FOR_OPTING_IN <messsage>
and then run a bash command to grep for it in code, so I can highlight it part of my code base health check within AndroidStudio (using a gradle task that ingest output of this bash script) and on terminal/CI I can just run the bash script.nisrulz
05/27/2021, 11:11 AM@RequiresOptIn
to reference classes that live in one module by other module, but these shouldn’t be used by our Clients of Library. So like an Internal Class, that is public but marked as @RequiresOptIn