Skip to content

Add isExpr, asExpr, asExprOf, asTerm, asTypeTree and asQuotedType #9497

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

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Aug 5, 2020

These replace the Expr methods seal, sealOpt and unseal with equivalent methods with more intuitive names.
Instead of sealOpt we provide a more flexible isExpr. All these methods are defined directly on Tree
instead of Term to avoid the need to perform type test to a Term first.

We similarly replace quoted.Type.seal with asTypeTree and reflect.Type.seal with asQuotedType.

Note: #8940 would remove the asTerm and asTypeTree as there we have Expr[T] <:< Term and something similar for Type, but the others would still be relevant.

@nicolasstucki nicolasstucki self-assigned this Aug 5, 2020
These replace the methods `seal`, `sealOpt` and `unseal` with equivalent method with more intuitive names.
Insead of `sealOpt` we provide a more flexible `isExpr`. All these methods are defined directly on `Tree`
insead of `Term` to avoid the need to perform type test to a `Term` first.
@nicolasstucki nicolasstucki force-pushed the add-quoted-asExpr-methods branch from dae7622 to 2722c3d Compare August 5, 2020 08:51
@nicolasstucki nicolasstucki changed the title Add isExpr, asExpr, asExprOf and asTerm Add isExpr, asExpr, asExprOf, asTerm, asTypeTree and asQuotedType Aug 5, 2020
@nicolasstucki nicolasstucki marked this pull request as ready for review August 5, 2020 14:09
@liufengyun
Copy link
Contributor

The names seal and unseal look good to me, meta-programmers get to know them better in usage.

That said, I'm open to changes, the only worry is about compatibility -- it seems the change is pervasive and will break most macros written for Dotty.

@nicolasstucki
Copy link
Contributor Author

The old version will still be supported for some time with a deprecation warning

@nicolasstucki
Copy link
Contributor Author

The names seal and unseal also look good to me because I have used them for years. But not to a newcomer that wants to convert an Expr to a Term, Term to an Expr, and so on ... The new names tell exactly what is the conversion and can be easily found by autocompletion.

The toExprOf is the most important one as it gives a way to get an Expr[T] from any Expr or Tree while checking that it is an expression and that it is in fact of type T. I saw too many instances of term.seal.cast[T] and term.seal.asInstanceOf[Expr[T]]; the second one is particularly dangerous as the it is an unchecked cast that leads to unsoundness that shows in -Ycheck in later phases.

@AdamPaynter
Copy link

I can confirm that, as a very new newcomer, this makes more sense. In fact, I hadn't even figured out what seal and unseal were until I read this PR. Even now, I still haven't grokked the relationship between Term and Expr (and why you would switch between them). Time for some more reading...

@nicolasstucki nicolasstucki force-pushed the add-quoted-asExpr-methods branch from e2e76e9 to 0f05f66 Compare August 7, 2020 13:35
@nicolasstucki
Copy link
Contributor Author

Replaced with #9514. Will do this in smaller parts

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

Successfully merging this pull request may close these issues.

4 participants