Skip to content

Ident factory in Quotes #12237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cchantep opened this issue Apr 27, 2021 · 7 comments
Closed

Ident factory in Quotes #12237

cchantep opened this issue Apr 27, 2021 · 7 comments

Comments

@cchantep
Copy link
Contributor

The Ident type is exposed in Quotes.reflectModule API, but I don't see a function in IdentModule (or elsewhere) provided to create such a value for a term name (is there anything I missed?).

Some simple factory like Ident("foo", ...) would be nice (and seems possible: cchantep@a4c3dcf#diff-42ea4b504ef08605734e689ebe8e3aed413718879873e2eff4951dbed4dc8da0R430 ).

@nicolasstucki
Copy link
Contributor

An alternative is to use Ref.apply.

Before creating an ident it is necessary to resolve to what Symbol or TypeRef it point. That part if the logic belongs to Symbol or TypeRef and not to Ident.

Creating a new symbol an returning an ident will not work and this ident points to a symbol/definition that does not exist in the program (i.e. there is not Definition with that symbol).

@cchantep
Copy link
Contributor Author

Thanks for your feedback.
I don't see the point about Ref, except if Ref is an Ident under the hood.
For now for example, I don't see about to create a case foo: MyType using CaseDefModule as not being able to create the Ident for the pattern matching.

@nicolasstucki
Copy link
Contributor

Ref will return an Ident or a Select, whichever is the one that is needed.

For now for example, I don't see about to create a case foo: MyType using CaseDefModule as not being able to create the Ident for the pattern matching.

Which Ident?

@nicolasstucki
Copy link
Contributor

To create case foo: MyType => foo you will need something like

      val owner = Symbol.spliceOwner
      val tpe = TypeRepr.of[MyType]
      val bind = Symbol.newBind(owner, "foo", Flags.Case, tpe)
      CaseDef(Bind(bind, Typed(Ref(bind), Inferred(tpe))), None, Ref(bind)))

@cchantep
Copy link
Contributor Author

Thanks for your feedback. As far as I see, the Bind(..) is not required.
That's such a case that makes me miss quasiquotes ...

@cchantep
Copy link
Contributor Author

I think there is a similar lack of utility, to resolve a Ident (or whatever Term) for a object/companion (already known as TypeRepr or Symbol).

@cchantep
Copy link
Contributor Author

BTW it seems weird that to emit a case x: String => () it's not possible to use:

val tpr = TypeRepr.of[String]
val bind =
  Symbol.newBind(
    Symbol.spliceOwner,
    tpr.typeSymbol.name.toLowerCase,
    Flags.Case,
    tpr
  )

val br = Ref(bind)

CaseDef(Typed(br, Inferred(tpr)), guard = None, rhs = UnitConstant())

/* Compilation error:

While expanding a macro, a reference to value string was used outside the scope where it was defined
*/

Indeed it seems required to write the CaseDef as below.

CaseDef(
  Bind(bind, Typed(Wildcard(), Inferred(tpr))),
  guard = None,
  rhs = Block(Nil, Literal(UnitConstant()))
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants