當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Scala annotation.ConstantAnnotation用法及代碼示例

用法:

trait ConstantAnnotation extends StaticAnnotation

擴展此特征的注釋類僅接受常量值作為參數。

請注意,此 trait 擴展了 StaticAnnotation ,因此常量注釋保留在類文件中。

實現要求常量注釋的參數作為命名參數傳遞,除非有一個參數,然後定義名為 value 的注釋參數。

常量注解可以使用默認參數。請注意,注釋用法的內部表示(例如,對編譯器插件可見)僅包含顯式提供的參數。

常量注解不允許定義輔助構造函數,主構造函數需要有一個參數列表。

例子:

class Ann(value: Int, x: Int = 0) extends scala.annotation.ConstantAnnotation
class Test {
  def someInt = 0
  @Ann(value = 0, x = 1) def g = 0
  @Ann(0) def f = 0                 // Internal representation contains `@Ann(value = 0)`
  @Ann(someInt)                     // error: argument needs to be a compile-time constant
}

源碼:

ConstantAnnotation.scala

相關用法


注:本文由純淨天空篩選整理自scala-lang.org大神的英文原創作品 annotation.ConstantAnnotation。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。