コンテンツにスキップ

KSPアノテーションリファレンス

monaka-transitionsプロセッサーは、ソースリテンションアノテーションを2つ読み取り、そこからKotlin拡張関数を生成します。どちらもdev.gmvalentino.monaka.coreに属しています。


@Transition

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class Transition(vararg val to: KClass<out State> = [])

ステートのクラスまたはデータクラスに配置し、toの各ターゲットに対して1つのtoXxx()拡張関数を生成します。

パラメーター

パラメーター 説明
to 1つ以上のターゲットステートKClass値。ターゲットごとに1つの関数が生成されます。

生成される関数の命名

レシーバー ターゲット 生成名
LoginState.Typing LoginState.Submitting toSubmitting()
Auth.SignedOut Auth.SigningIn toSigningIn()
Loading Auth.SigningIn toAuthSigningIn()

@SelfTransition

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class SelfTransition

シールドクラスまたはシールドインターフェースに配置し、単一のtoSelf()拡張関数を生成します。toSelf()は型の共有プロパティ(すべての直接シールドサブクラスに同名・同型で存在するプロパティ)を名前付きパラメーターとして受け取り、whenディスパッチで同じサブタイプの新しいインスタンスを返します。

共有プロパティ

@SelfTransition
sealed interface TimerState : State {
    val autoPause: Boolean

    data class Idle(override val autoPause: Boolean = false, ) : TimerState
    data class Running(override val autoPause: Boolean, ) : TimerState
}

生成:

fun TimerState.toSelf(autoPause: Boolean = this.autoPause): TimerState = when (this) {
    is TimerState.Idle    -> copy(autoPause = autoPause)
    is TimerState.Running -> copy(autoPause = autoPause)
}