Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/scala.quoted/Quotes/reflectModule/SymbolModule

SymbolModule

scala.quoted.Quotes.reflectModule.SymbolModule

Methods of the module objectval Symbol

Attributes

Source
Quotes.scala
Graph
Supertypes
classObject
traitMatchable
classAny
Self type
Quotes.this.reflectModule.this.Symbol.type

Members list

Value members

Abstract methods

The class Symbol of a global class definition

The class Symbol of a global class definition

Attributes

Source
Quotes.scala
defnewBind(parent:Quotes.this.reflectModule.this.Symbol,name:String,flags:Quotes.this.reflectModule.this.Flags,tpe:Quotes.this.reflectModule.this.TypeRepr):Quotes.this.reflectModule.this.Symbol

Generates a pattern bind symbol with the given parent, name and type.

Generates a pattern bind symbol with the given parent, name and type.

This symbol starts without an accompanying definition. It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing this symbol to the BindDef constructor.

Value parameters

flags

extra flags to with which the symbol should be constructed.Case flag will be added. Can beCase

name

The name of the binding

parent

The owner of the binding

tpe

The type of the binding

Attributes

Note

As a macro can only splice code into the point at which it is expanded, all generated symbols must be direct or indirect children of the reflection context's owner.

Source
Quotes.scala

Generates a new method symbol with the given parent, name and type.

Generates a new method symbol with the given parent, name and type.

To define a member method of a class, use thenewMethod within thedecls function ofnewClass.

Value parameters

name

The name of the method

parent

The owner of the method

tpe

The type of the method (MethodType, PolyType, ByNameType) This symbol starts without an accompanying definition. It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing this symbol to the DefDef constructor.

Attributes

Note

As a macro can only splice code into the point at which it is expanded, all generated symbols must be direct or indirect children of the reflection context's owner.

Source
Quotes.scala
defnewMethod(parent:Quotes.this.reflectModule.this.Symbol,name:String,tpe:Quotes.this.reflectModule.this.TypeRepr,flags:Quotes.this.reflectModule.this.Flags,privateWithin:Quotes.this.reflectModule.this.Symbol):Quotes.this.reflectModule.this.Symbol

Works as the other newMethod, but with additional parameters.

Works as the other newMethod, but with additional parameters.

To define a member method of a class, use thenewMethod within thedecls function ofnewClass.

Value parameters

flags

extra flags to with which the symbol should be constructed.Method flag will be added. Can bePrivate | Protected | Override | Deferred | Final | Method | Implicit | Given | Local | JavaStatic | Synthetic | Artifact

name

The name of the method

parent

The owner of the method

privateWithin

the symbol within which this new method symbol should be private. May be noSymbol.

tpe

The type of the method (MethodType, PolyType, ByNameType)

Attributes

Source
Quotes.scala
defnewVal(parent:Quotes.this.reflectModule.this.Symbol,name:String,tpe:Quotes.this.reflectModule.this.TypeRepr,flags:Quotes.this.reflectModule.this.Flags,privateWithin:Quotes.this.reflectModule.this.Symbol):Quotes.this.reflectModule.this.Symbol

Generates a new val/var/lazy val symbol with the given parent, name and type.

Generates a new val/var/lazy val symbol with the given parent, name and type.

This symbol starts without an accompanying definition. It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing this symbol to the ValDef constructor.

Note: Also see ValDef.let

Value parameters

flags

extra flags to with which the symbol should be constructed. Can bePrivate | Protected | Override | Deferred | Final | Param | Implicit | Lazy | Mutable | Local | ParamAccessor | Module | Package | Case | CaseAccessor | Given | Enum | JavaStatic | Synthetic | Artifact

name

The name of the val/var/lazy val

parent

The owner of the val/var/lazy val

privateWithin

the symbol within which this new method symbol should be private. May be noSymbol.

tpe

The type of the val/var/lazy val

Attributes

Note

As a macro can only splice code into the point at which it is expanded, all generated symbols must be direct or indirect children of the reflection context's owner.

Source
Quotes.scala

Definition not available

Definition not available

Attributes

Source
Quotes.scala

Get class symbol if class is either defined in current compilation run or present on classpath.

Get class symbol if class is either defined in current compilation run or present on classpath.

Attributes

Source
Quotes.scala

Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload.

Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload.

Attributes

Source
Quotes.scala

Get module symbol if module is either defined in current compilation run or present on classpath.

Get module symbol if module is either defined in current compilation run or present on classpath.

Attributes

Source
Quotes.scala

Get package symbol if package is either defined in current compilation run or present on classpath.

Get package symbol if package is either defined in current compilation run or present on classpath.

Attributes

Source
Quotes.scala

Symbol of the definition that encloses the current splicing context.

Symbol of the definition that encloses the current splicing context.

For example, the following call tospliceOwner would return the symbolx.

val x = ${ ... Symbol.spliceOwner ... }

For a macro splice, it is the symbol of the definition where the macro expansion happens.

Attributes

Source
Quotes.scala

Experimental methods

A fresh name for class or member symbol names.

A fresh name for class or member symbol names.

Fresh names are constructed using the following formatprefix + "$macro$" + freshIndex. ThefreshIndex are unique within the current source file.

Examples: Seescala.annotation.MacroAnnotation

Value parameters

prefix

Prefix of the fresh name

Attributes

Experimental
true
Source
Quotes.scala
defnewBoundedType(parent:Quotes.this.reflectModule.this.Symbol,name:String,flags:Quotes.this.reflectModule.this.Flags,tpe:Quotes.this.reflectModule.this.TypeBounds,privateWithin:Quotes.this.reflectModule.this.Symbol):Quotes.this.reflectModule.this.Symbol

Generate a new type symbol for a type bounds with the given parent, name and type

Generate a new type symbol for a type bounds with the given parent, name and type

This symbol starts without an accompanying definition. It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing this symbol to the TypeDef constructor.

Value parameters

flags

extra flags to with which symbol can be constructed.Deferred flag will be added. Can bePrivate |Protected |Override |Deferred |Final |Infix |Local

name

The name of the type

parent

The owner of the type

privateWithin

the symbol within which this new type symbol should be private. May be noSymbol.

tpe

The bounds of the type

Attributes

Note

As a macro can only splice code into the point at which it is expanded, all generated symbols must be direct or indirect children of the reflection context's owner.

Experimental
true
Source
Quotes.scala
defnewClass(owner:Quotes.this.reflectModule.this.Symbol,name:String,parents:List[Quotes.this.reflectModule.this.TypeRepr],decls:Quotes.this.reflectModule.this.Symbol=>List[Quotes.this.reflectModule.this.Symbol],selfType:Option[Quotes.this.reflectModule.this.TypeRepr]):Quotes.this.reflectModule.this.Symbol

Generates a new class symbol for a class with a public parameterless constructor. For more settings, look to the other newClass methods.

Generates a new class symbol for a class with a public parameterless constructor. For more settings, look to the other newClass methods.

Example usage:

val name: String = "myClass"val parents = List(TypeTree.of[Object], TypeTree.of[Foo])def decls(cls: Symbol): List[Symbol] =  List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit])))val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfType = None)val fooSym = cls.declaredMethod("foo").headval fooDef = DefDef(fooSym, argss => Some('{println(s"Calling foo")}.asTerm))val clsDef = ClassDef(cls, parents, body = List(fooDef))val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo])Block(List(clsDef), newCls).asExprOf[Foo]

constructs the equivalent to

'{  class myClass() extends Object with Foo {    def foo(): Unit = println("Calling foo")  }  new myClass(): Foo}

Value parameters

decls

The member declarations of the class provided the symbol of this class

name

The name of the class

owner

The owner of the class

parents

The parent classes of the class. The first parent must not be a trait.

selfType

The self type of the class if it has one This symbol starts without an accompanying definition. It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing this symbol to the ClassDef constructor.

Attributes

Note

As a macro can only splice code into the point at which it is expanded, all generated symbols must be direct or indirect children of the reflection context's owner.

Experimental
true
Source
Quotes.scala
defnewClass(owner:Quotes.this.reflectModule.this.Symbol,name:String,parents:Quotes.this.reflectModule.this.Symbol=>List[Quotes.this.reflectModule.this.TypeRepr],decls:Quotes.this.reflectModule.this.Symbol=>List[Quotes.this.reflectModule.this.Symbol],selfType:Option[Quotes.this.reflectModule.this.TypeRepr],clsFlags:Quotes.this.reflectModule.this.Flags,clsPrivateWithin:Quotes.this.reflectModule.this.Symbol,conParams:List[(String,Quotes.this.reflectModule.this.TypeRepr)]):Quotes.this.reflectModule.this.Symbol

Generates a new class symbol for a class with a public single term clause constructor.

Generates a new class symbol for a class with a public single term clause constructor.

Example usage:

val name = "myClass"def decls(cls: Symbol): List[Symbol] =  List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit])))val parents = List(TypeTree.of[Object])val cls = Symbol.newClass(  Symbol.spliceOwner,  name,  parents = _ => parents.map(_.tpe),  decls,  selfType = None,  clsFlags = Flags.EmptyFlags,  Symbol.noSymbol,  List(("idx", TypeRepr.of[Int]), ("str", TypeRepr.of[String])))val fooSym = cls.declaredMethod("foo").headval idxSym = cls.fieldMember("idx")val strSym = cls.fieldMember("str")val fooDef = DefDef(fooSym, argss =>  Some('{println(s"Foo method call with (${${Ref(idxSym).asExpr}}, ${${Ref(strSym).asExpr}})")}.asTerm))val clsDef = ClassDef(cls, parents, body = List(fooDef))val newCls = Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), List('{0}.asTerm, '{string}.asTerm))Block(List(clsDef), Apply(Select(newCls, cls.methodMember("foo")(0)), Nil)).asExprOf[Unit]

construct the equivalent to

'{  class myClass(idx: Int, str: String) extends Object {    def foo() =      println(s"Foo method call with $idx, $str")  }  new myClass(0, "string").foo()}

Value parameters

clsFlags

extra flags with which the class symbol should be constructed.

clsPrivateWithin

the symbol within which this new class symbol should be private. May be noSymbol.

conParams

constructor parameter pairs of names and types. Parameters assigned by the constructor can be obtained viaclassSymbol.memberField. This symbol starts without an accompanying definition. It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing this symbol to the ClassDef constructor.

name

The name of the class

owner

The owner of the class

parents

Function returning the parent classes of the class. The first parent must not be a trait. Takes the constructed class symbol as an argument. Callingcls.typeRef.asType as part of this function will lead to cyclic reference errors.

Attributes

Note

As a macro can only splice code into the point at which it is expanded, all generated symbols must be direct or indirect children of the reflection context's owner.

Experimental
true
Source
Quotes.scala
defnewClass(owner:Quotes.this.reflectModule.this.Symbol,name:String,parents:Quotes.this.reflectModule.this.Symbol=>List[Quotes.this.reflectModule.this.TypeRepr],decls:Quotes.this.reflectModule.this.Symbol=>List[Quotes.this.reflectModule.this.Symbol],selfType:Option[Quotes.this.reflectModule.this.TypeRepr],clsFlags:Quotes.this.reflectModule.this.Flags,clsPrivateWithin:Quotes.this.reflectModule.this.Symbol,clsAnnotations:List[Quotes.this.reflectModule.this.Term],conMethodType:Quotes.this.reflectModule.this.TypeRepr=>Quotes.this.reflectModule.this.MethodOrPoly,conFlags:Quotes.this.reflectModule.this.Flags,conPrivateWithin:Quotes.this.reflectModule.this.Symbol,conParamFlags:List[List[Quotes.this.reflectModule.this.Flags]],conParamPrivateWithins:List[List[Quotes.this.reflectModule.this.Symbol]]):Quotes.this.reflectModule.this.Symbol

Generates a new class symbol with a constructor of the shape signified by a passed PolyOrMethod parameter.

Generates a new class symbol with a constructor of the shape signified by a passed PolyOrMethod parameter.

Example usage:

val name = "myClass"def decls(cls: Symbol): List[Symbol] =  List(Symbol.newMethod(cls, "getParam", MethodType(Nil)(_ => Nil, _ => cls.typeMember("T").typeRef)))val conMethodType =  (classType: TypeRepr) => PolyType(List("T"))(_ => List(TypeBounds.empty), polyType =>    MethodType(List("param"))((_: MethodType) => List(polyType.param(0)), (_: MethodType) =>      AppliedType(classType, List(polyType.param(0)))    )  )val cls = Symbol.newClass(  Symbol.spliceOwner,  name,  parents = _ => List(TypeRepr.of[Object]),  decls,  selfType = None,  clsFlags = Flags.EmptyFlags,  clsPrivateWithin = Symbol.noSymbol,  clsAnnotations = Nil,  conMethodType,  conFlags = Flags.EmptyFlags,  conPrivateWithin = Symbol.noSymbol,  conParamFlags = List(List(Flags.EmptyFlags), List(Flags.EmptyFlags)),  conParamPrivateWithins = List(List(Symbol.noSymbol), List(Symbol.noSymbol)))val getParamSym = cls.declaredMethod("getParam").headdef getParamRhs(): Option[Term] =  val paramValue = This(cls).select(cls.fieldMember("param")).asExpr  Some('{ println("Calling getParam"); $paramValue }.asTerm)val getParamDef = DefDef(getParamSym, _ => getParamRhs())val clsDef = ClassDef(cls, List(TypeTree.of[Object]), body = List(getParamDef))val newCls =  Apply(    Select(      Apply(        TypeApply(Select(New(TypeIdent(cls)), cls.primaryConstructor), List(TypeTree.of[String])),        List(Expr("test").asTerm)      ),      cls.methodMember("getParam").head    ),    Nil  )Block(List(clsDef), newCls).asExpr

constructs the equivalent to

'{  class myClass[T](val param: T) extends Object {    def getParam: T =      println("Calling getParam")      param  }  new myClass[String]("test").getParam()}

Value parameters

clsAnnotations

annotations of the class

clsFlags

extra flags with which the class symbol should be constructed. Can bePrivate |Protected |PrivateLocal |Local |Final |Trait |Abstract |Open

clsPrivateWithin

the symbol within which this new class symbol should be private. May be noSymbol

conFlags

extra flags with which the constructor symbol should be constructed. Can beSynthetic |Method |Private |Protected |PrivateLocal |Local

conMethodType

Function returning MethodOrPoly type representing the type of the constructor. Takes the result type as parameter which must be returned from the innermost MethodOrPoly and have type parameters applied if those are used. PolyType may only represent the first clause of the constructor.

conParamFlags

extra flags with which the constructor parameter symbols should be constructed. Must match the shape ofconMethodType. For type parameters those can beParam |Deferred |Private |PrivateLocal |Local. For term parameters those can beParamAccessor |Private |Protected |PrivateLocal |Local

conParamPrivateWithins

the symbols within which the constructor parameters should be private. Must match the shape ofconMethodType. Can consist of noSymbol. Term and type parameters assigned by the constructor can be obtained viaclassSymbol.memberField/classSymbol.memberType. This symbol starts without an accompanying definition. It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing this symbol to the ClassDef constructor.

conPrivateWithin

the symbol within which the constructor for this new class symbol should be private. May be noSymbol.

decls

The member declarations of the class provided the symbol of this class

name

The name of the class

owner

The owner of the class

parents

Function returning the parent classes of the class. The first parent must not be a trait Takes the constructed class symbol as an argument. Callingcls.typeRef.asType as part of this function will lead to cyclic reference errors.

selfType

The self type of the class if it has one

Attributes

Note

As a macro can only splice code into the point at which it is expanded, all generated symbols must be direct or indirect children of the reflection context's owner.

Experimental
true
Source
Quotes.scala
defnewModule(owner:Quotes.this.reflectModule.this.Symbol,name:String,modFlags:Quotes.this.reflectModule.this.Flags,clsFlags:Quotes.this.reflectModule.this.Flags,parents:Quotes.this.reflectModule.this.Symbol=>List[Quotes.this.reflectModule.this.TypeRepr],decls:Quotes.this.reflectModule.this.Symbol=>List[Quotes.this.reflectModule.this.Symbol],privateWithin:Quotes.this.reflectModule.this.Symbol):Quotes.this.reflectModule.this.Symbol

Generates a new module symbol with an associated module class symbol, this is equivalent to anobject declaration in source code. This method returns the module symbol. The module class can be accessed callingmoduleClass on this symbol.

Generates a new module symbol with an associated module class symbol, this is equivalent to anobject declaration in source code. This method returns the module symbol. The module class can be accessed callingmoduleClass on this symbol.

Example usage:

given Quotes = ???import quotes.reflect.*val moduleName: String = Symbol.freshName("MyModule")val parents = List(TypeTree.of[Object])def decls(cls: Symbol): List[Symbol] =  List(Symbol.newMethod(cls, "run", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit]), Flags.EmptyFlags, Symbol.noSymbol))val mod = Symbol.newModule(Symbol.spliceOwner, moduleName, Flags.EmptyFlags, Flags.EmptyFlags, _ => parents.map(_.tpe), decls, Symbol.noSymbol)val cls = mod.moduleClassval runSym = cls.declaredMethod("run").headval runDef = DefDef(runSym, _ => Some('{ println("run") }.asTerm))val modDef = ClassDef.module(mod, parents, body = List(runDef))val callRun = Apply(Select(Ref(mod), runSym), Nil)Block(modDef.toList, callRun)

constructs the equivalent to

given Quotes = ???import quotes.reflect.*'{  object MyModule$macro$1 extends Object:    def run(): Unit = println("run")  MyModule$macro$1.run()}

Value parameters

clsFlags

extra flags with which the module class symbol should be constructed

decls

A function that takes the symbol of the module class as input and return the symbols of its declared members

modFlags

extra flags with which the module symbol should be constructed

name

The name of the class

parent

The owner of the class

parents

A function that takes the symbol of the module class as input and returns the parent classes of the class. The first parent must not be a trait.

privateWithin

the symbol within which this new method symbol should be private. May be noSymbol. This symbol starts without an accompanying definition. It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing this symbol toClassDef.module.

Attributes

Note

As a macro can only splice code into the point at which it is expanded, all generated symbols must be direct or indirect children of the reflection context's owner.

Experimental
true
Source
Quotes.scala
defnewTypeAlias(parent:Quotes.this.reflectModule.this.Symbol,name:String,flags:Quotes.this.reflectModule.this.Flags,tpe:Quotes.this.reflectModule.this.TypeRepr,privateWithin:Quotes.this.reflectModule.this.Symbol):Quotes.this.reflectModule.this.Symbol

Generate a new type symbol for a type alias with the given parent, name and type

Generate a new type symbol for a type alias with the given parent, name and type

This symbol starts without an accompanying definition. It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing this symbol to the TypeDef constructor.

Value parameters

flags

extra flags to with which symbol can be constructed. Can bePrivate |Protected |Override |Final |Infix |Local

name

The name of the type

parent

The owner of the type

privateWithin

the symbol within which this new type symbol should be private. May be noSymbol.

tpe

The rhs the type alias

Attributes

Note

As a macro can only splice code into the point at which it is expanded, all generated symbols must be direct or indirect children of the reflection context's owner.

Experimental
true
Source
Quotes.scala
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp