Scala.js-specific runtime library components for Scala 3, providing JavaScript-specific functionality and bridge components between Scala 3 and the Scala.js runtime environment
—
Comprehensive annotation system including experimental features, macro annotations, and compiler hints supporting both behavioral annotations and metaprogramming constructs.
Essential annotations for marking definitions and controlling compiler behavior.
/**
* Mark definitions as experimental
* Requires -Xsource-features flag or explicit import to use
*/
final class experimental extends StaticAnnotation
/**
* Define external names for definitions
* Useful for interoperability and avoiding naming conflicts
*/
final class targetName(name: String) extends StaticAnnotation
/**
* Mark fields as static (platform-specific behavior)
* Generates static fields on JVM, affects JS/Native compilation
*/
final class static extends StaticAnnotationUsage Examples:
import scala.annotation.experimental
// Experimental API
@experimental
def newFeature(): String = "This is experimental"
// External naming
class MathUtils:
@targetName("add")
def +(x: Int, y: Int): Int = x + y
@targetName("multiply")
def *(x: Int, y: Int): Int = x * y
// Static field
class Constants:
@static val PI = 3.14159
@static val E = 2.71828
// Usage requires experimental import
import scala.annotation.experimental.given
val result = newFeature() // Now allowed
// External names are used in bytecode/JS output
val math = MathUtils()
val sum = math.+(5, 3) // Compiled as "add"Annotations for macro and metaprogramming support.
/**
* Base trait for macro annotation implementations
* Experimental feature for compile-time code transformation
*/
trait MacroAnnotation extends StaticAnnotation:
/** Transform definition and optional companion */
def transform(definition: Any, companion: Option[Any]): List[Any]
/**
* Base trait for annotations that refine types
* Used for type-level transformations and refinements
*/
trait RefiningAnnotation extends StaticAnnotationUsage Examples:
import scala.annotation.MacroAnnotation
// Custom macro annotation
class toString extends MacroAnnotation:
def transform(definition: Any, companion: Option[Any]): List[Any] =
// Generate toString method for case classes
// Implementation details handled by compiler
List(definition) // Simplified
// Usage of macro annotation
@toString
case class Person(name: String, age: Int)
// Automatically generates custom toString method
// Refining annotation example
class refined[P] extends RefiningAnnotation
type PositiveInt = Int @refined[Positive]
def processPositive(n: PositiveInt): String = s"Processing $n"
// Type refinement (conceptual - actual implementation more complex)
val positive: PositiveInt = 42.asInstanceOf[PositiveInt]Annotations that modify compilation behavior and provide compiler hints.
/**
* Enable alpha renaming for bindings
* Allows same name to be used in nested scopes
*/
class alpha extends StaticAnnotation
/**
* Mark types as capabilities in the capability system
*/
class capability extends StaticAnnotation
/**
* Restrict annotation to constructor use only
*/
class constructorOnly extends StaticAnnotation
/**
* Mark parameters as initialization parameters
* Used in capability system for tracking initialization
*/
class init extends StaticAnnotation
/**
* Transform type A into B (contextual abstraction)
*/
class into[B] extends StaticAnnotation
/**
* Make private members visible in binary (for internal use)
*/
class publicInBinary extends StaticAnnotation
/**
* Indicate that a method retains/captures its parameters
*/
class retains extends StaticAnnotation
/**
* Indicate method retains by-name parameters
*/
class retainsByName extends StaticAnnotation
/**
* Mark code as thread-unsafe (optimization hint)
*/
class threadUnsafe extends StaticAnnotation
/**
* Mark traits as transparent (no virtual dispatch overhead)
*/
class transparentTrait extends StaticAnnotation
/**
* Hint compiler to unroll loops/recursion
*/
class unroll extends StaticAnnotationUsage Examples:
// Alpha renaming
def nested(): Unit =
val x = 1
@alpha def inner() =
val x = 2 // Allowed due to alpha annotation
x
// Capability annotation
@capability
trait FileSystem
// Constructor-only annotation
@constructorOnly
class InitOnly(value: String)
// Initialization parameter
class Service(@init connection: DatabaseConnection)
// Type transformation
type UserId = Int @into[String]
// Thread safety hint
@threadUnsafe
class FastCounter:
private var count = 0
def increment(): Unit = count += 1
// Transparent trait for performance
@transparentTrait
trait Additive[T]:
def add(x: T, y: T): T
// Loop unrolling hint
@unroll
def factorial(n: Int): Int =
if n <= 1 then 1 else n * factorial(n - 1)Annotations used internally by the Scala compiler (typically not used in user code).
// Internal annotations (simplified subset)
class Alias(aliasee: String) extends StaticAnnotation
class AnnotationDefault() extends StaticAnnotation
class AssignedNonLocally() extends StaticAnnotation
class Body() extends StaticAnnotation
class CaptureChecked() extends StaticAnnotation
class Child() extends StaticAnnotation
class ContextResultCount(count: Int) extends StaticAnnotation
class ErasedParam() extends StaticAnnotation
class InlineParam() extends StaticAnnotation
class MappedAlternative() extends StaticAnnotation
class ProvisionalSuperClass() extends StaticAnnotation
class Repeated() extends StaticAnnotation
class RuntimeChecked() extends StaticAnnotation
class SourceFile(path: String) extends StaticAnnotation
class WithPureFuns() extends StaticAnnotation
class WitnessNames() extends StaticAnnotationUsage Examples:
// These are typically generated by the compiler, not written by users
// Alias annotation (compiler-generated)
@Alias("originalName")
type TypeAlias = String
// Erased parameter tracking
def generic[T](@ErasedParam evidence: ClassTag[T]): Array[T] =
new Array[T](0)
// Inline parameter tracking
inline def inlineMethod(@InlineParam x: Int): Int = x + 1Annotations for preview features and development aids.
/**
* Mark definitions as preview features
* Subject to change in future versions
*/
class preview extends StaticAnnotation
/**
* Express reachability of capabilities (internal)
*/
class reachCapability extends StaticAnnotation
/**
* Require specific capabilities for usage
*/
class requiresCapability extends StaticAnnotation
/**
* Mark data as sharable between capability domains
*/
class sharable extends StaticAnnotation
/**
* Mark data as unshared (capability isolation)
*/
class unshared extends StaticAnnotationUsage Examples:
// Preview feature
@preview
def experimentalAPI(): Future[String] =
Future.successful("This API may change")
// Capability requirements
@requiresCapability
def secureOperation()(using Security): Unit =
// Requires Security capability
// Sharable data
@sharable
case class ImmutableData(value: String)
// Unshared data
@unshared
class MutableResource(var state: Int)Annotations for suppressing capability and safety checks.
/**
* Suppress capability leak warnings
* Use when you know capability leaks are safe
*/
class uncheckedCapabilityLeaks extends StaticAnnotation
/**
* Suppress capture checking warnings
* Use when you know captures are handled correctly
*/
class uncheckedCaptures extends StaticAnnotationUsage Examples:
import scala.annotation.unchecked.*
// Suppress capability leak warnings
@uncheckedCapabilityLeaks
def leakyFunction(resource: FileHandle): () => String =
() => resource.read() // Normally would warn about capability leak
// Suppress capture warnings
@uncheckedCaptures
def complexCapturing[C](f: () => C): C =
// Complex capability management that compiler can't verify
f()Common annotations from the standard annotation package.
// Re-exported from scala.annotation for convenience
class deprecated(message: String = "", since: String = "") extends StaticAnnotation
class throws[T <: Throwable] extends StaticAnnotation
class tailrec extends StaticAnnotation
class inline extends StaticAnnotation
class native extends StaticAnnotation
class strictfp extends StaticAnnotation
class volatile extends StaticAnnotation
class transient extends StaticAnnotationUsage Examples:
// Deprecation
@deprecated("Use newMethod instead", since = "1.2.0")
def oldMethod(): String = "deprecated"
// Exception documentation
@throws[IllegalArgumentException]
def validateInput(input: String): String =
if input.isEmpty then throw IllegalArgumentException("Empty input")
else input
// Tail recursion optimization
@tailrec
def factorial(n: Int, acc: Int = 1): Int =
if n <= 1 then acc else factorial(n - 1, n * acc)
// Inline expansion
@inline
def fastComputation(x: Int): Int = x * x + 1
// Platform-specific annotations
@native
def platformSpecific(): Unit
@volatile
var sharedCounter: Int = 0
@transient
val temporaryData: List[String] = List()// Core annotation types
final class experimental extends StaticAnnotation
final class targetName(name: String) extends StaticAnnotation
final class static extends StaticAnnotation
// Macro system
trait MacroAnnotation extends StaticAnnotation:
def transform(definition: Any, companion: Option[Any]): List[Any]
trait RefiningAnnotation extends StaticAnnotation
// Behavioral annotations
class alpha extends StaticAnnotation
class capability extends StaticAnnotation
class constructorOnly extends StaticAnnotation
class init extends StaticAnnotation
class into[B] extends StaticAnnotation
class publicInBinary extends StaticAnnotation
class retains extends StaticAnnotation
class retainsByName extends StaticAnnotation
class threadUnsafe extends StaticAnnotation
class transparentTrait extends StaticAnnotation
class unroll extends StaticAnnotation
// Preview and capability annotations
class preview extends StaticAnnotation
class reachCapability extends StaticAnnotation
class requiresCapability extends StaticAnnotation
class sharable extends StaticAnnotation
class unshared extends StaticAnnotation
// Unchecked annotations
class uncheckedCapabilityLeaks extends StaticAnnotation
class uncheckedCaptures extends StaticAnnotation
// Internal compiler annotations (subset)
class Alias(aliasee: String) extends StaticAnnotation
class ErasedParam() extends StaticAnnotation
class InlineParam() extends StaticAnnotation
class SourceFile(path: String) extends StaticAnnotationInstall with Tessl CLI
npx tessl i tessl/maven-org-scala-lang--scala3-library-sjs1-3