Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - Set @Annotations . to function return types?

Is there a way to use annotations to declare a return condition for kotlin functions?

For example, I want to tell lint that my function will return an Android resource id at:

fun getImageId(): Int

I would want something as:

fun getImageId(): @DrawableRes Int

which fails

I thought it would make sense (I believe is possible in Java), because I may have something as:

 fun setImage(@DrawableRes res: Int) {
        myImageView.setImageResource(res)
    }

and call it as:

setImage(getImageId())

So that it has a chain of validations that the given int is actually a Res Id

like image 973
htafoya Avatar asked Dec 12 '19 02:12

htafoya


People also ask

How do I specify a return type of a function in Kotlin?

Kotlin function return values To return values, we use the return keyword. In the example, we have two square functions. When a funcion has a body enclosed by curly brackets, it returns a value using the return keyword. The return keyword is not used for functions with expression bodies.

What is the default return type of a function in Kotlin?

I know unit is the default return type in kotlin.

What is @GET annotation in Kotlin?

By prefixing the annotation with @get: the resulting bytecode will have the annotation on the generated getter function: // Decompiled from the resulting Kotlin Bytecode @SomeAnnotation @Nullable public final String getSomeProperty() { return this. someProperty; }

What is type annotation in Kotlin?

In Java, an annotation type is a form of an interface, so you can implement it and use an instance. As an alternative to this mechanism, Kotlin lets you call a constructor of an annotation class in arbitrary code and similarly use the resulting instance.

How to annotate the properties of class in Kotlin?

We can annotate the properties of class by adding an annotation to the properties. In below example, we assume that an Lang instance is valid if the value of the name is either Kotlin or Java. Kotlin also provides certain in-built annotations, that are used to provide more attributes to user-defined annotations.

How to mark the return type of a function in Kotlin?

Kotlin does not infer return types for functions with block bodies because such functions may have complex control flow in the body, and the return type will be non-obvious to the reader (and sometimes even for the compiler). You can mark a parameter of a function (usually the last one) with the vararg modifier: Copied!

How to return a specific field from getoffer function in Kotlin?

UPD. There is alternative implementation that uses a bit of kotlin magic, specifically, reified types. With this approach instead of having separate entities to define the field that you want to be returned from getOffer function you just specify explicit type parameter. Alternatively you can rely on implicit type inference:

How many values can an array return in Kotlin?

Moreover, we can use arrays and collections to return up to five values of the same type: The Kotlin limit is five because it has defined five componentN extension functions on arrays and collections. Sometimes, it’s better to define a custom type with a meaningful name.


1 Answers

You can do it with:

@DrawableRes
fun getImageId(): Int

like image 170
Yang Liu Avatar answered Oct 11 '22 18:10

Yang Liu