question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Macros missing for Scala 3

See original GitHub issue

The following code compiles and works as expected on Scala 2.13.x, but fails on 3.0.0-RC1:

import eu.timepit.refined.numeric._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._

val x: Int Refined Positive = 5

[error] – [E007] Type Mismatch Error [error] |Found: (5 : Int) [error] |Required: eu.timepit.refined.api.Refined[Int, eu.timepit.refined.numeric.Positive]

It looks like the 2.x macros haven’t been ported to Scala 3 yet.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:9
  • Comments:17 (12 by maintainers)

github_iconTop GitHub Comments

6reactions
notecommented, May 5, 2021

I took a look at what it would take to migrate macros to Scala 3.

The biggest issue is stumbled upon is lack of equivalent of Evals.eval in Scala 3. eval is used here in refined. On this page that is considered a major blocker for migrating macros in a few projects. You can more for example here and here.

Anyway, I managed to create a very incomplete draft (https://github.com/fthomas/refined/pull/959), it supports only one use case and almost everything is hardcoded there. It shows the basic idea though - since we cannot evaluate the tree we have to manually pattern match against each possible predicate which is very cumbersome. Also, I don’t see a way to support user defined custom predicates in this solution. Implementing non-leaf predicates (as e.g. And) might be tricky.

It’s definitely a problematic solution, I would love to see something simpler.

The above attempt assumed we are constrained by current encoding. #921 suggests that @fthomas was experimenting with new encodings. I played around with Scala 3 features that seems relevant and you can see the outcome here. Predicates for Ints can be written exclusively in terms of scala.compiletime - link. With Strings writing macros are required but they are actually very straightforward - link. All in all, it looks promising. The biggest limitation of that encoding is that it uses closed world assumption - I don’t see an easy way to support custom user-defined predicates.

A friend of mine hinted me about multi-stage programming. I’ve had no idea about it but it sounds like something worth looking into as a way of overcoming limitations of Scala 3 macros.

4reactions
armanbilgecommented, Jun 8, 2022

Polite bump on this 😃 if folks are happy with this Predicate strategy I can work on a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Macros - Scala 3
Macros : Quotes and Splices ... Macros are built on two well-known fundamental operations: quotation and splicing. Quotation is expressed as '{...} for...
Read more >
Quoted Code | Macros in Scala 3
Here, we will get an error telling us that we are missing a contextual Type[T] . Therefore, we can easily fix it by...
Read more >
Scala 3: Macros - Medium
Last time I introduced one of the new metaprogramming features in Scala 3, the inline keyword and how it affects source code.
Read more >
Get enclosing module in Scala 3 macros - Stack Overflow
1 Answer 1 ... Symbol.spliceOwner was the trick I was missing. It's very painful to navigate the API right now. Metals doesn't show...
Read more >
Scala 3 inline: macro-like superpowers
But, the gains tend to disappear with loops containing a huge number of iterations. References. “Macros in Scala 3 – Inline”. docs –...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found