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


Scala any.IsConst用法及代碼示例

IsConst 方法(或屬性)屬於 scala.compiletime.ops.any 對象(object),其相關用法說明如下。

用法:

@experimental
type IsConst[X] <: Boolean

測試類型是否為常量。

package scala.compiletime.ops
trait Snippet0 { self: any.type =>
  val c1: IsConst[1] = true
  val c2: IsConst["hi"] = true
  val c3: IsConst[false] = true
  val c4: IsConst[Any] = false
}

如果類型還不知道,那麽IsConst 將保持未求值,並且僅在其具體類型應用程序中求值。例如:

package scala.compiletime.ops
trait Snippet0 { self: any.type =>
  //def `isConst`` returns the type `IsConst[X]`, since `X` is not yet known.
  def isConst[X] : IsConst[X] = ???
  val c5 : true = isConst[1] //now the type is known to be a constant
  val c6 : false = isConst[Any] //now the type is known to be not a constant
}

源碼:

any.scala

相關用法


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