scala.compiletime

Members list

Type members

Experimental classlikes

trait Erased

A marker trait for erased values. vals or parameters whose type extends Erased get an implicit erased modifier.

A marker trait for erased values. vals or parameters whose type extends Erased get an implicit erased modifier.

Attributes

Experimental
true
Source
Erased.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class CanThrow[E]
trait Precise

Value members

Concrete methods

def byName[T](x: => T): T

Assertion that an argument is by-name. Used for nullability checking.

Assertion that an argument is by-name. Used for nullability checking.

Type parameters

T

the result type of the by-name argument

Value parameters

x

the by-name argument to evaluate

Attributes

Source
package.scala
transparent inline def codeOf(arg: Any): String

Returns the string representation of argument code:

Returns the string representation of argument code:

import scala.compiletime.*
inline def logged(inline p1: Any) =
  ("code: " + codeOf(p1), p1)

logged(identity("foo"))
// above is equivalent to:
// ("code: scala.Predef.identity("foo")", identity("foo"))

The formatting of the code is not stable across version of the compiler.

Value parameters

arg

the expression whose source code representation is returned

Attributes

Returns

the string representation of the argument's source code

Note

only inline arguments will be displayed as "code". Other values may display unintutively.

Source
package.scala
transparent inline def constValue[T]: T

Given a constant, singleton type T, convert it to a value of the same singleton type. For example: assert(constValue[1] == 1).

Given a constant, singleton type T, convert it to a value of the same singleton type. For example: assert(constValue[1] == 1).

Type parameters

T

the constant singleton type to convert to a value

Attributes

Source
package.scala
transparent inline def constValueOpt[T]: Option[T]

Same as constValue but returns a None if a constant value cannot be constructed from the provided type. Otherwise returns that value wrapped in Some.

Same as constValue but returns a None if a constant value cannot be constructed from the provided type. Otherwise returns that value wrapped in Some.

Type parameters

T

the constant singleton type to attempt to convert to a value

Attributes

Source
package.scala
inline def constValueTuple[T <: Tuple]: T

Given a tuple type (X1, ..., Xn), returns a tuple value (constValue[X1], ..., constValue[Xn]).

Given a tuple type (X1, ..., Xn), returns a tuple value (constValue[X1], ..., constValue[Xn]).

Type parameters

T

the tuple type whose element types are constant singleton types

Attributes

Source
package.scala

Used as the right hand side of a given in a trait, like this

Used as the right hand side of a given in a trait, like this

import scala.compiletime.*
trait DeferredHolder:
  type T
  given T = deferred

This signifies that the given will get a synthesized definition in all classes that implement the enclosing trait and that do not contain an explicit overriding definition of that given.

Attributes

Source
package.scala
def erasedValue[T]: T

Use this method when you have a type, do not have a value for it but want to pattern match on it. For example, given a type `Tup ??? case _: (h *: t) => ??? } //{ } //}

Use this method when you have a type, do not have a value for it but want to pattern match on it. For example, given a type `Tup ??? case _: (h *: t) => ??? } //{ } //}

This value can only be used in an inline match and the value cannot be used in
the branches.
@syntax markdown

@tparam T the type to match against in an inline match expression

Attributes

Source
package.scala
inline def error(inline msg: String): Nothing

The error method is used to produce user-defined compile errors during inline expansion. If an inline expansion results in a call error(msgStr) the compiler produces an error message containing the given msgStr.

The error method is used to produce user-defined compile errors during inline expansion. If an inline expansion results in a call error(msgStr) the compiler produces an error message containing the given msgStr.

import scala.compiletime.*
error("My error message")

or

import scala.compiletime.*
inline def errorOnThisCode(inline x: Any) =
  error("My error of this code: " + codeOf(x))

Value parameters

msg

the error message to display at compile time

Attributes

Returns

this method never returns; it always produces a compile-time error

Source
package.scala
inline def requireConst(inline x: Boolean | Byte | Short | Int | Long | Float | Double | Char | String): Unit

Checks at compiletime that the provided values is a constant after inlining and constant folding.

Checks at compiletime that the provided values is a constant after inlining and constant folding.

Usage:

import scala.compiletime.*
inline def twice(inline n: Int): Int =
  requireConst(n) // compile-time assertion that the parameter `n` is a constant
  n + n

twice(1)
val m: Int = ???
twice(m) // error: expected a constant value but found: m

Value parameters

x

the value that must be a compile-time constant after inlining

Attributes

Source
package.scala
inline def summonAll[T <: Tuple]: T

Given a tuple T, summons each of its member types and returns them in a Tuple.

Given a tuple T, summons each of its member types and returns them in a Tuple.

Type parameters

T

the tuple containing the types of the values to be summoned

Attributes

Returns

a tuple of the summoned given instances corresponding to the element types of T

Source
package.scala
transparent inline def summonFrom[T](f: Nothing => T): T

Summons first given matching one of the listed cases. E.g. in

Summons first given matching one of the listed cases. E.g. in

import scala.compiletime.*
type A
trait B
type C
inline def f = {
given B with { }

summonFrom {
  case given A => 1
  case given B => 2
  case given C => 3
  case _ => 4
}
}

the returned value would be 2.

Type parameters

T

the result type of the match expression

Value parameters

f

a match block with cases that summon givens of specified types

Attributes

Returns

the result of the first matching case

Source
package.scala
transparent inline def summonInline[T]: T

Summon a given value of type T. Usually, the argument is not passed explicitly. The summoning is delayed until the call has been fully inlined.

Summon a given value of type T. Usually, the argument is not passed explicitly. The summoning is delayed until the call has been fully inlined.

Type parameters

T

the type of the value to be summoned

Attributes

Returns

the given value typed as the provided type parameter

Source
package.scala

Used as the initializer of a mutable class or object field, like this:

Used as the initializer of a mutable class or object field, like this:

import scala.compiletime.*
type T
var x: T = uninitialized

This signifies that the field is not initialized on its own. It is still initialized as part of the bulk initialization of the object it belongs to, which assigns zero values such as null, 0, 0.0, false to all object fields.

Attributes

Source
package.scala

Extensions

Extensions

extension [T](x: T)
transparent inline def asMatchable: x.type & Matchable

Casts a value to be Matchable. This is needed if the value's type is an unconstrained type parameter and the value is the scrutinee of a match expression. This is normally disallowed since it violates parametricity and allows to uncover implementation details that were intended to be hidden. The asMatchable escape hatch should be used sparingly. It's usually better to constrain the scrutinee type to be Matchable in the first place.

Casts a value to be Matchable. This is needed if the value's type is an unconstrained type parameter and the value is the scrutinee of a match expression. This is normally disallowed since it violates parametricity and allows to uncover implementation details that were intended to be hidden. The asMatchable escape hatch should be used sparingly. It's usually better to constrain the scrutinee type to be Matchable in the first place.

Attributes

Source
package.scala