Skip to content

Instantly share code, notes, and snippets.

@jlareck
Last active September 2, 2021 14:15
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jlareck/fc1d6fb511c37e6e1c326a68bc190d2c to your computer and use it in GitHub Desktop.
How to share Quotes entities between multiple methods

Problem: we need to share same symbols between two methods. How we can do this? The first idea of solution: We can create a class like Helper with [Q <: Quotes, T: Type](using val q: Q) and in this case we avoid duplication of (using Quotes) for each entitiy.

class Helper[Q <: Quotes, T: Type](using val q: Q) {
  import q.reflect.*

  val classDynamicSymbol = Symbol.requiredClass("me.shadaj.scalapy.py.Dynamic")
  val classReaderSymbol = Symbol.requiredClass("me.shadaj.scalapy.readwrite.Reader")
  val classWriterSymbol = Symbol.requiredClass("me.shadaj.scalapy.readwrite.Writer")
  val classAnySymbol = Symbol.requiredClass("me.shadaj.scalapy.py.Any")
 }

object FacadesImpl {
  def native_impl[T: Type](using q: Quotes): Expr[T] = {
      import q.reflect.*
      val helper = new Helper[q.type, T]
      ....
  }
  def native_named_impl[T: Type](using q: Quotes): Expr[T] = {
      import q.reflect.*
      val helper = new Helper[q.type, T]
      ....
  }
}

The second idea of solution: We can create an object with defs like this:

object Shared {
  def classReaderSymbol(using Quotes) = {
    import quotes.reflect.*
    Symbol.requiredClass("me.shadaj.scalapy.readwrite.Reader")
  }

  def classWriterSymbol(using Quotes) = {
    import quotes.reflect.*
    Symbol.requiredClass("me.shadaj.scalapy.readwrite.Writer")
  }
}

But here we have duplication of using Quotes and import quotes.reflect.* .

The third idea of solution: we can reduce the lines in the second solution by changing using Quotes to using q: Quotes and rewrite in this way:

object Shared {
  def classDynamicSymbol(using q: Quotes) = 
    q.reflect.Symbol.requiredClass("me.shadaj.scalapy.py.Dynamic")
 }

What solution is better? Is there any other solution for sharing entities from Quotes?

@nicolasstucki
Copy link

The first one is the only one that should not be used. If there are errors with the paths it may be quite cryptic why it fails.

@jlareck
Copy link
Author

jlareck commented Sep 2, 2021

Okay, I see, thank you a lot! I guess I will use the 4th option then

@nicolasstucki
Copy link

Good choice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment