Book Image

Programming Kotlin

Book Image

Programming Kotlin

Overview of this book

Quickly learn the fundamentals of the Kotlin language and see it in action on the web. Easy to follow and covering the full set of programming features, this book will get you fluent in Kotlin for Android.
Table of Contents (20 chapters)
Programming Kotlin
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Annotations


Annotations allow developers to add extra meaning to classes, interfaces, parameters, and so on at compile time. They are a form of meta-programming in that respect. Annotations can then be used by the compiler or by your own code via reflection at runtime. Depending on the annotation value, the meaning of the program or data can change.

Annotations are present in Java as well as Kotlin, and so the most common annotations are those that are provided as part of the Kotlin or Java standard libraries. Some annotations you may be familiar with already are @SuppressWarnings and @tailrec.

To define your own annotation, simply prefix a class with the keyword annotation:

    annotation class Foo 

This annotation can then be used in classes, functions, parameters, and so on. In fact, annotations can pretty much be used anywhere, as the following table shows:

Target

Example

Class

@Foo class MyClass

Interface

@Foo interface MyInterface

Object

@Foo object MyObject

Parameter...