Macros

scalus.utils.Macros
object Macros

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Macros.type

Members list

Value members

Concrete methods

def fieldAsDataMacro[A : Type](e: Expr[A => Any])(implicit evidence$1: Type[A], Quotes): Expr[Data => Data]

Build a runtime Data => Data getter from a field-selection expression.

Build a runtime Data => Data getter from a field-selection expression.

This is a convenience wrapper that forwards to the term-level implementation. Accepts a quoted lambda selecting nested fields of A and returns a plain function that reads nested Data values.

Type parameters

A

root type of selection

Value parameters

e

quoted selection A => Any

Attributes

Returns

a Data => Data function performing the extraction

def fieldAsDataMacroTerm(using q: Quotes)(e: q.reflect.Term): Expr[Data => Data]

Term-level implementation of the field-as-Data getter macro.

Term-level implementation of the field-as-Data getter macro.

This function performs the reflection on the quoted Term and builds an Expr[Data => Data] that navigates the builtin Data representation according to the nested selects in e.

Supported shapes are nested Select nodes; other shapes will abort compilation.

Value parameters

e

reflected term representing the selection

Attributes

Returns

Expr[Data => Data] that extracts the nested field

Throws
scala.quoted.Report.errorAndAbort

on unsupported tree shapes or missing fields

def fieldAsExprDataMacro[A : Type](e: Expr[A => Any])(implicit evidence$1: Type[A], Quotes): Expr[(Expr[Data]) => Expr[Data]]

Create a quoted getter function of type Exp[Data] => Exp[Data] from a field selection lambda (e.g. (_.txInfo.id)).

Create a quoted getter function of type Exp[Data] => Exp[Data] from a field selection lambda (e.g. (_.txInfo.id)).

The macro accepts an expression selecting nested case-class fields and generates an expression that, at runtime, extracts the corresponding Data from an Exp[Data] value.

Supported input shapes: chain of Select/Ident nodes corresponding to nested fields.

Type parameters

A

the root type of the selection

Value parameters

e

quoted function A => Any that selects a field path (e.g. _.a.b)

Attributes

Returns

quoted function Exp[Data] => Exp[Data] extracting the selected nested field

Throws
scala.quoted.Report.errorAndAbort

on unsupported shapes or missing fields

def generateCompileCall(code: Expr[Any])(using Quotes): Expr[SIR]

Compile the provided quoted AST into a SIR using the project's Compiler.

Compile the provided quoted AST into a SIR using the project's Compiler.

Value parameters

code

quoted code/term to be compiled

Attributes

Returns

quoted SIR representation

def generateCompileCall(options: Expr[Options], code: Expr[Any])(using Quotes): Expr[SIR]

Compile the provided quoted AST into a SIR using Compiler with options.

Compile the provided quoted AST into a SIR using Compiler with options.

Value parameters

code

quoted code/term to be compiled

options

quoted Compiler.Options

Attributes

Returns

quoted SIR representation

def inlineResource(using Quotes)(name: Expr[String]): Expr[String]

Read a textual resource from the project sources at compile time.

Read a textual resource from the project sources at compile time.

This macro reads the resource file named name from the compile-time source root (defaults to src/main/resources) and returns its contents as a quoted String.

Value parameters

name

quoted resource filename

Attributes

Returns

quoted resource contents

Throws
java.lang.IllegalArgumentException

if the resource file is not found

def lamMacro[A : Type, B : Type](f: Expr[(Expr[A]) => Expr[B]])(implicit evidence$1: Type[A], evidence$2: Type[B], Quotes): Expr[Expr[A => B]]

Converts a quoted lambda value of type Exp[A] => Exp[B] into a quoted UPLC lambda expression of type Exp[A => B].

Converts a quoted lambda value of type Exp[A] => Exp[B] into a quoted UPLC lambda expression of type Exp[A => B].

This macro extracts the parameter name from the provided lambda and creates a Trm.LamAbs term wrapping the body. It expects a simple lambda value (e.g. lam(x => ...)).

Type parameters

A

the input Exp type

B

the output Exp type

Value parameters

f

quoted function value of type Exp[A] => Exp[B]

Attributes

Returns

a quoted Exp representing a UPLC lambda of A => B

Throws
scala.quoted.Report.errorAndAbort

if the supplied expression does not match the expected lambda shape

def lamTermMacro(f: Expr[Term => Term])(using Quotes): Expr[Term]

Converts a lambda value of type Term => Term into a UPLC LamAbs term expression.

Converts a lambda value of type Term => Term into a UPLC LamAbs term expression.

This macro extracts the parameter name from the provided lambda and creates a Term.LamAbs term wrapping the body. It expects a simple lambda value (e.g. lam(x => x)).

Value parameters

f

quoted lambda expression

Attributes

def mkClassFieldsFromSeqIsoImpl[A : Type](implicit evidence$1: Type[A], Quotes): Expr[(A => Seq[Long], (Seq[Long]) => A)]

Generates a pair of functions to convert a class to a sequence of longs and vice versa. The generated code is equivalent to the following:

Generates a pair of functions to convert a class to a sequence of longs and vice versa. The generated code is equivalent to the following:

 (
   (m: A) => Seq(m.field1, m.field2, ...),
   (seq: Seq[Long]) => {
     val params = new A()
     params.field1 = if idx < seq.size then seq(0) else 0L
     ...
     params
   }
 )

Type parameters

A

the type of the class

Attributes

Returns

a pair of functions

def mkGetBuiltinRuntime(bm: Expr[BuiltinsMeaning])(using Quotes): Expr[DefaultFun => BuiltinRuntime]

Generates match expression on DefaultFun ordinals that should be efficiently compiled to table switch (and it is).

Generates match expression on DefaultFun ordinals that should be efficiently compiled to table switch (and it is).

 fun.ordinal() match {
   case 0 => bm.AddInteger
   // ...
 }

Attributes

def mkReadWriterImpl[A : Type](implicit evidence$1: Type[A], Quotes): Expr[ReadWriter[A]]
def questionMark(using Quotes)(x: Expr[Boolean]): Expr[Boolean]

Return quoted boolean expression that traces when condition is false.

Return quoted boolean expression that traces when condition is false.

This helper expands to an if-expression that returns true when the input is true; otherwise it calls Builtins.trace with a diagnostic string and returns false.

Value parameters

x

quoted boolean expression

Attributes

Returns

quoted boolean that is identical to x when true, and traces + returns false otherwise

inline def readResource(using Quotes)(name: String, resPath: String = ...): String

Read a resource file from disk using the compile-time source root.

Read a resource file from disk using the compile-time source root.

This inline helper computes the path to the resource and returns its contents.

Value parameters

name

the resource file name

resPath

the resources subpath relative to the source root (default "resources")

Attributes

Returns

file contents as string

Throws
java.lang.IllegalArgumentException

if the file is missing

inline def sourcesRoot(using Quotes)(srcRoot: String = ...): Path

Compute the project's source root Path at compile time.

Compute the project's source root Path at compile time.

The function locates SourceFile.current.path and searches backward for srcRoot to determine the project root. Defaults to "/src/main/".

Value parameters

srcRoot

path fragment to locate the project source root (default: "/src/main/")

Attributes

Returns

Path pointing to the located source root directory

Throws
java.lang.IllegalArgumentException

if the fragment is not found in current path