810 questions
0
votes
0
answers
28
views
staging.Compiler for experimental code in Scala 3
Consider the following code. When I run it, I see Flag -experimental set repeatedly message (the functionality seems correct otherwise). What should I do so it won't be displayed? (without (using ...
0
votes
0
answers
62
views
How to suppress deprecated warnings in code generated for type classes?
In Scala 3 macros how to suppress deprecated warnings in code generated for type classes of types defined like here:
@deprecated("some reason") case class C(x: Int) derives SomeTypeClass
...
2
votes
0
answers
93
views
How to create `lazy val` with parametized name and value in Scala 3 macros?
It is so easy in Scala 2, just:
q"lazy val $name = $value"
I need it for local values, so I don't have multi-thread requirements
I've tried just adding Flags.Lazy to the val definition ...
1
vote
1
answer
148
views
Shouldn't be a straightforward way to evaluate a `scala.quoted.Expr` to a literal constant?
In the updated section of the answer to the question How to reduce a Term that is reducible to a constant, @DmytroMitin used an interesting approach to reduce an Expr[T] to a Literal(<T>Constant(...
0
votes
1
answer
87
views
Replacement for `x.asType match { case '[type t <: S; t] => ...` in Scala 3.3 (LTS)
Do we have any replacement for the following Scala 3.7 code:
x.asType match {
case '[type t <: S; t] =>
'{ apply[t] }.asExprOf[Any]
case _ =>
report.errorAndAbort(s"...
1
vote
1
answer
83
views
Why does quoted expression raise errors while equivalent AST does not?
In the following example, using a quoted expression ('{ ... }) raises a compiler error, while the equivalent AST construction works fine. Why does this happen?
Code Example
// Using Scala 3.7.0
trait ...
2
votes
1
answer
231
views
How to reduce a `Term` that is reducible to a constant, in a scala 3 macro?
I want to reduce a Term instance, that I know is reducible to a constant, to an equivalent Term that can be used as the pattern of a CaseDef.
Specifically, I need to implement the reduceTerm method of ...
1
vote
1
answer
150
views
How to call an inline method from within a scala 3.6.4 macro?
In a scala 3.6.4 serialization library I am developing there is the DiscriminatorCriteria type-class that allows the user to determine which discriminator value to use for each variant P of a sum-type ...
0
votes
1
answer
58
views
How to avoid calls to `scala.quoted.Quotes.reflectModule.reportModule.info` to be ignored in compiler output?
For debugging a scala 3.6.4 recursive macro I added many calls to quotes.reflect.report.info.
But only a few are shown in the compiler output despite all the messages are different.
Apparently, the ...
1
vote
1
answer
102
views
In scala 3.6.4, how to call a polymorphic method with type bounds from inside a quoted expression?
The method to call is the SAM of the following type class:
trait DiscriminatorCriteria[A] {
def getFor[V <: A]: Int
}
And the headType to pass as type argument (to type parameter V) was ...
0
votes
0
answers
58
views
"Missing outer accessor" in macro expansion
I am attempting to write a Scala 3 macro that calls a function by name, to be resolved according to Scala's usual lexical scope (as if the user had written that function name themselves).
As an ...
0
votes
0
answers
51
views
Calling generic trait function from dotty macro
Suppose I have a trait Foo as follows:
trait Foo {
def foo[T](x: T): Etc = someDefaultImpl(x)
}
This may be extended:
trait SubFoo extends Foo {
override def foo[T](x: T): Etc = differentImpl(x)
}...
1
vote
1
answer
68
views
Scala 2 annotation macro weirdness
I have these three macros all defined in the same file (and with the same exact definition):
class client(val port: Int = 9000) extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = ...
1
vote
0
answers
63
views
How to match a Lambda function literal expression
I am trying to transform the code of literal lambda expressions in a Scala-3 macro.
I have defined a macro like this
def rCode(formal: Expr[Int => Boolean])(using Quotes): Expr[Int => Boolean] =
...
1
vote
1
answer
65
views
Passing string as the parameter to ValDef left-hand-side's method invoke inside quote block in Scala 3 macro annotation
I am trying implementing a macro annotation with such effect:
@MarkNode
val dataAbc = new Data()
which generates
val dataAbc = new Data()
dataAbc.setCodegenSuffix("dataAbc")
And, some of ...
0
votes
0
answers
61
views
Scala 3 Extend a type from a type parameter in macro
Consider the following (broken) code:
def makeClass[T: Type](using q: Quotes): Expr[T] = '{
class MyTest extends T { def foo: Int = 4 }
new MyTest
}
This fails with the following error:
A class ...
1
vote
1
answer
67
views
How to match sub-expression of type Array[Int]?
I'm learning Scala multi-stage metaprogramming. I'm currently working on an exercise that asks me to implement the dot-product (sum-of-product) between two vectors of the same length.
Writing the code,...
0
votes
1
answer
61
views
Scala 3 Macro Not able to find given in scope
I am trying to write a macro in Scala 3, which will resolve the types defined in the case class and then find for the given/implict method for those type.
below is the code for macro.
import scala....
2
votes
1
answer
73
views
Can a scala3 macro introduce an implicit value around an existing block?
I am working on a library for use in tests, where I want to be able to have variables that can be redefined in a particular scope. This is inspired by let in rspec.
I have something working, by ...
0
votes
1
answer
50
views
How to ge the `Expr` of a regular class's field declared in its primary constructor with scala 3.5.1 macros?
This is probably a very easy question to answer. And maybe also easy to find the answer. However, I couldn't find it after several hours. The documentation for the Scala API scala.quoted.Quotes....
0
votes
1
answer
145
views
Scala 3 macro: how to get the return type of a method by its `Symbol`?
In a Scala 3 macro, given a Symbol for a method, what would be the most straightforward way to get the return type of that method?
More concretely, suppose I have a type name Foo that corresponds to a ...
0
votes
1
answer
90
views
Scala 3 macro: creating a path-dependent method signature
In Scala 3, I'm trying to generate a method within a macro that has a path-dependent type, but I can't find an API that lets me create such a signature.
Given the following definition:
trait Foo:
...
1
vote
1
answer
173
views
Scala 3 macros: "dynamically" instantiating singleton objects at compile-time
I'm trying to create a macro that makes use of some objects.
Suppose I have the following definitions:
trait Foo:
def doStuff(): Unit
// in other files
object Bar extends Foo:
def doStuff() = ......
1
vote
0
answers
129
views
How to generate a companion object in Scala 3?
I have several enums where there companion object code is identical to one another.
How can I generate these companion objects where the difference between them is just the enum name?
enum Variants:
...
0
votes
1
answer
149
views
Using upickle read in Scala 3 macro
Try to write a generic macro for deserialising case classes using uPickle read in Scala 3:
inline def parseJson[T:Type](x: Expr[String])(using Quotes): Either[String, Expr[T]] = '{
try
Right(...
0
votes
1
answer
81
views
Scala 3 macro Quotes: when to use apply vs copy
I'm trying to learn how to write macros with the new Scala 3 Quotes API. In this API the various AST elements usually have two methods defined to introduce them: apply and copy where copy takes a non-...
1
vote
1
answer
116
views
how to generate fresh singleton literal type in scala using macros
I need to generate random signleton type on every macro invocation in scala 2.13
I tried something like this, but I can't change macro def return type
def randomSingletonInt: Int = macro randomImpl
...
1
vote
1
answer
199
views
exception during macro expansion: type T is not a class, play json
I'm facing this error:
exception during macro expansion:
scala.ScalaReflectionException: type T is not a class
at scala.reflect.api.Symbols$SymbolApi.asClass(Symbols.scala:284)
at scala....
1
vote
1
answer
459
views
How do I update a multi project sbt build that depends on macro libraries to scala 3 in stages?
I am trying to update a large multi module repository to Scala 3 in stages.
According to the Scala 3 migration guide, there is good forward and backward runtime compatiblity, so I should be able to ...
1
vote
1
answer
80
views
Get an implicit value during macro generation for a Type
Macro generates an empty instance of a case class based on implicitly provided values for all the properties.
The current state of the solution can be found on GH:
https://github.com/atais/empty/blob/...
2
votes
1
answer
384
views
How do I hang onto an izumi.reflect.Tag?
I've been using izumi.reflect.Tag to hold onto type info, and it works great. Except when I lose that type info at some point, and then I'm not sure how to get it back. Here's what I want to do:
case ...
3
votes
0
answers
107
views
Why does this Scala 3 macros reference example of HOAS fail with "Type must be fully defined" error?
I'm new to scala 3 macros, and am trying to learn my way around them. In trying to learn how to use expression pattern matching, I tried to plug in this example from the reference [1]:
'{ ((x: Int) =&...
3
votes
0
answers
128
views
Why does this "Aux" pattern work for Shapeless but not for me?
I am trying to generate a compile-time tuple type out of a case class.
I think, I am doing exactly the same thing shapeless does with generics:
class TupleProvider[T <: Product] {
type Repr
def ...
1
vote
1
answer
203
views
How can I create an enum value dynamically in Scala 3 macros?
I'm creating an enum value dynamically. I have a type of the enum (not the object itself) and a string representing a valid value, e.g. "Red" for Color.Red.
// T is Color (enum). I've ...
0
votes
0
answers
68
views
Internal error accessing enclosing macro object via a symbol of a superclass member
Sorry, this title is probably not very clear ... but that seems rather appropriate when talking about scala macros :)
So, I have a class (the ApplicationInfo thingy doesn't really matter - can be ...
0
votes
2
answers
150
views
Scala 3: why does `inline` fix stack overflow
I have this code that results in a stack overflow.
// Lexer.Token defined elsewhere
object BadParser extends Parsers {
import Lexer.Token
type Elem = Token
private def PUNCT = Token.PUNCT
...
0
votes
0
answers
46
views
What would cause ClassNotFound in a Scala 3 macro?
Doing reflection in a macro. Sometimes, only for a few classes (seems to happen for a select few Java classes in a mixed project) Class.forName() doesn't work when testing.
For example this works:
...
2
votes
1
answer
134
views
How to extract comments in scala 3 source files by using a tasty inspector
Say I am parsing the tasty file of some scala source file like this source file:
/** Some comments
*/
trait LsFunctions:
...
I would like to extract the "Some comments" comment and ...
0
votes
1
answer
192
views
How can I get the correctly applied types in the body of a class in Scala 3 reflection?
Let's say I'm reflecting on this class using Scala 3 reflection:
class Foo[T](val thing: T):
var store: Option[T] = None
Let's say I reflect with given type Foo[Boolean], and I get the TypeRef for ...
0
votes
1
answer
48
views
How can I get the equivalent of foo.asInstanceOf[SomeType] in a Scala 3 macro?
I have this in my macro impl:
def renderJsonFn(rt: RType): Expr[(Any,StringBuilder) => StringBuilder] =
val reflectedClassFieldInfo = ???
val typeMaker = reflectedClassFieldInfo.fieldType....
0
votes
1
answer
258
views
How can I get a Quotes instance into an Expr in Scala 3 macros?
I have this:
def myMacroImpl[T:Type](t: Expr[T])(using quotes:Quotes): Expr[String] = {
def fooFn(): Expr[(Any,StringBuilder) => StringBuilder] =
// quotes visible here
val qq = Expr(...
1
vote
1
answer
260
views
How can I convert an Expr to a Tree in Scala 3?
I need to convert an Expr to a Tree doing Scala 3 macro development, and I did not yet find an appropriate conversion mechanism for that.
In Scala 2 there existed a tree method on Expr which made this ...
0
votes
1
answer
187
views
How to match a TypeDef in a Scala 3 macro annotation correctly?
I created an example macro annotation to test if I can properly match a case class TypeDef to later modify it, but it does not match, though all seems fine and compiles, heres how it looks:
import ...
1
vote
0
answers
70
views
Extract all case classes from pattern matching using macro in Scala3
In Scala2 project I have macro to extract all cases classes from pattern matching.
sealed trait Adt
case class A(i:Int) extends Adt
case class B(i:Int) extends Adt
def extract(pf: PartialFunction[Adt,...
1
vote
0
answers
115
views
How can I define a refinement type that return this.type in a Scala 3 Macro?
I have been trying to figure out how to create the following refinement within a Scala 3 Macro:
{
def foo: this.type
}
Note: I know that self reference in refinement is deprecated but there are ...
1
vote
1
answer
305
views
Scala 3 constructor inheritance with macros
Every class implementing a trait must declare a constructor that sets trait's fields:
sealed trait WithPayload:
def description: String
def payload1: Int
def payload2: Long
// All ...
3
votes
1
answer
434
views
What can you do with MacroAnnotaiton that you cannot do with Macros in Scala 3?
Scala 3.3.0-RC2 has added MacroAnnotation but it has a ton of caveats. The main on is that "new definitions will not be visible from outside the macro expansion".
There are a lot of examples ...
1
vote
1
answer
622
views
macro implementation not found (scala 2.13.3)
I'm using the RunningStrategies class in my Scala code to define a set of running strategies. To create an instance of RunningStrategies, I'm using the apply method along with a variable number of ...
1
vote
2
answers
370
views
Does anybody know, how to properly print case class type hierarchy without creating one?
I faced a problem. I'd like to make a printer for any type in scala.
for example i have a case class
class AAA(i: Int, s: String, o: Option[Int], bbb: BBB)
class BBB(l: List[Int])
def explainType[...
7
votes
2
answers
1k
views
Is there a simple Scala 3 example of how to use `quoted.Type` as replacement for `TypeTag`?
Martin Odersky said
Scala 3 has the quoted package, with quoted.Expr as a representation of expressions and quoted.Type as a representation of types. quoted.Type esentially replaces TypeTag. It does ...