Kotlin Annotation IntDef

joh*_*crq 26 annotations kotlin

我有这个代码示例:

class MeasureTextView: TextView {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

    companion object{
        val UNIT_NONE = -1
        val UNIT_KG = 1
        val UNIT_LB = 0            
    }

    fun setMeasureText(number: Float, unitType: Int){

        val suffix = when(unitType){
            UNIT_NONE -> {
                EMPTY_STRING
            }
            UNIT_KG -> {
                KG_SUFIX
            }
            UNIT_LB -> {
                LB_SUFIX
            }
            else -> throw IllegalArgumentException("Wrong unitType passed to formatter: MeasureTextView.setMeasureText")
        }

        // set the final text
        text = "$number $suffix"
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望能够在编译时使用自动完成功能和IntDef注释,因此当我调用时setMeasureText(...),静态变量显示为此方法的参数的选项.

我已经搜索了这个,我无法找到Kotlin是否支持这种java风格的注释(例如intdef).所以我尝试了它,并为此做了一个注释,但它不会在自动完成中显示.

我的问题: - Kotlin是否支持Java注释IntDef(最新版本)

  • 如果是,我如何在Android Studio IDE中打开(如果它可以工作,我无法让编译器建议它).

  • 如果不是,是否有任何Kotlin方式进行此编译时间检查

Dim*_*ira 37

奇怪的是,但这个问题在搜索之前与正确答案相同

在这里复制:

import android.support.annotation.IntDef
public class Test {

    companion object {

         @IntDef(SLOW, NORMAL, FAST)
         @Retention(AnnotationRetention.SOURCE)
         annotation class Speed

         const val SLOW = 0
         const val NORMAL = 1
         const val FAST = 2
    }

    @Speed
    private var speed: Int=SLOW

    public fun setSpeed(@Speed speed: Int) {
        this.speed = speed
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这没有解决任何问题.您可以像使用Java一样创建注释,但是您无法在Kotlin上使用IntDef,因为编译器不知道如何解释它.IntDef的优点在于,它会在自动完成时显示,因此,大多数情况下,您可以使用IntDef联系人而不是枚举.拿走编译器部分,注释本身是无用的. (16认同)
  • 我不认为这是真的.我已经使用@StringDef作为我的Kotlin类的构造函数参数的一部分进行了测试,并且当我向其传递无效类型时,从未抛出编译时警告/错误. (8认同)
  • @ user3806331不同意.自动填充不是唯一的一点.它很有用,因为在编译时检查了带注释的参数,所以你不能用`-1L`错误地调用这个函数 (2认同)

yol*_*ole 22

从Kotlin 1.0.3开始,@IntDef不支持注释,但计划在以后的版本中提供支持.

进行这些编译时间检查的Kotlin方法是使用enum class而不是一系列Int常量.

  • @Raeglan这个建议不再适用于ART; 它现在已从您链接到的文档页面中删除. (14认同)
  • Android上的枚举效果不是很低吗? (13认同)
  • "例如,枚举通常需要的内存是静态常量的两倍以上.你应该严格避免在Android上使用枚举." - https://developer.android.com/topic/performance/memory.html#Overhead (9认同)
  • 不,他们不是. (8认同)
  • @yole它已从内存通知中删除,但不是从这里删除:https://developer.android.com/topic/performance/reduce-apk-size (5认同)
  • 我想知道Kotlin处理枚举的方式是否有所不同. (3认同)
  • @yole 绝对正确!现在使用枚举就可以了。观看 GoogleIO 视频:https://www.youtube.com/watch?v=IrMw7MEgADk&feature=youtu.be&t=857 (3认同)
  • 确实,与整数相比,枚举具有更大的内存占用空间,但这不再是什么大问题了。无论如何,您都没有 100 个枚举,而且 ART 现在优化了枚举内存使用。但即使您确实在内存方面受到限制,请注意,如果您使用 Proguard(或 D8/R8)优化,它可能会将简单枚举转换为整数,因此如果您没有任何额外的枚举属性,这将最有效很可能是开箱即用的。https://www.guardsquare.com/en/products/proguard/manual/usage/optimizations 查找“class/unboxing/enum”。 (2认同)

evi*_*evi 9

我在 Kotlin 中使用 IntDef 的首选方法是使用顶级声明:

package com.example.tips


const val TIP_A = 1
const val TIP_B = 2
const val TIP_C = 3

@IntDef(TIP_A, TIP_B, TIP_C)
@Retention(AnnotationRetention.SOURCE)
annotation class TipId


class TipsDataProvider {

    fun markTip(@TipId tipId: Int) {
    ...
    }
}
Run Code Online (Sandbox Code Playgroud)

不需要额外的类或对象!有关顶级声明的更多信息请参见此处