Open9

Kotlin のアノテーションの意味がわからん!

tkttkt
/**
 * Suppresses the given compilation warnings in the annotated element.
 * @property names names of the compiler diagnostics to suppress.
 */
@Target(CLASS, ANNOTATION_CLASS, TYPE_PARAMETER, PROPERTY, FIELD, LOCAL_VARIABLE, VALUE_PARAMETER,
        CONSTRUCTOR, FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER, TYPE, EXPRESSION, FILE, TYPEALIAS)
@Retention(SOURCE)
public annotation class Suppress(vararg val names: String)

なんか、警告を無視するものっぽい?

tkttkt
@Suppress("RedundantSuspendModifier")

このアノテーションは何の警告を消すのか、

/**
 * Abstracted Repository as promoted by the Architecture Guide.
 * https://developer.android.com/topic/libraries/architecture/guide.html
 */
class WordRepository(private val wordDao: WordDao) {

    // Room executes all queries on a separate thread.
    // Observed Flow will notify the observer when the data has changed.
    val allWords: Flow<List<WordEntity>> = wordDao.getAlphabetizedWords()

    // By default Room runs suspend queries off the main thread, therefore, we don't need to
    // implement anything else to ensure we're not doing long running database work
    // off the main thread.
    @Suppress("RedundantSuspendModifier")
    @WorkerThread
    suspend fun insert(wordEntity: WordEntity) {
        wordDao.insert(wordEntity)
    }
}

クラス全体はこんな感じ

tkttkt

実行を想定しているスレッドを指定できるらしい。
@WorkerThread はワーカースレッド意外(例えばメインスレッド)で実行しようとしたら警告を出す

tkttkt

RedundantSuspendModifier とは、何のための警告なのか…

tkttkt

最近はアノテーションを見かけていない