Skip to content

Warn users on non canonical type references #9952

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

Merged

Conversation

nicolasstucki
Copy link
Contributor

Help users write the correct variant of the quoted type encoding.
Explicit construction of a scala.quoted.Type[_] should be done with its constructor.
Explicit construction of types in not encouraged as it should be created implicitly.
Creating an Type explicitly is only useful for debugging purposes.
Likewise, we encourage the use of the explicit inner type reference.

@nicolasstucki nicolasstucki force-pushed the warn-users-on-non-canonical branch from 146bffc to fdd06df Compare October 6, 2020 20:20
@nicolasstucki nicolasstucki marked this pull request as ready for review October 7, 2020 05:28
@@ -1,6 +1,6 @@
import scala.quoted._

def summonTypedType[T : Type](using QuoteContext): String = '[T] match {
def summonTypedType[T : Type](using QuoteContext): String = Type[T] match {
case '[Boolean] => "Boolean"
case '[Byte] => "Byte"
case _ => "Other"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems '[T] is more symmetric here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, and that is why it should not be used. The syntactic symmetry is what lead to most conceptual misunderstandings of how Type works.

This constructor should also never be used explicitly and giving it a shorter alias does not help users avoid the explicit use.

It is also uncertain that the case '[...] pattern will be kept. We might remove them and use only a single matching abstraction as we do with inline match.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the usage is bad, what about disallowing it in syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is my intent as I explain in #9486. This is a first step to help migration. We can drop the syntax later on once users have adapted. I would prefer to have at least one release with a warning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to bring this up in the meeting, as it's syntax related.

Copy link
Contributor Author

@nicolasstucki nicolasstucki Oct 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This specific case is a corner case that should be avoided. It should have been written explicitly as

def summonTypedType[T : Type](using QuoteContext): String = summon[Type[T]] match { ...
// or 
def summonTypedType[T](using tp: Type[T])(using QuoteContext): String = tp match { ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rewrite this test to remove the '[Byte] syntax?

@nicolasstucki nicolasstucki force-pushed the warn-users-on-non-canonical branch from fdd06df to 16eeae3 Compare October 12, 2020 13:26
@nicolasstucki nicolasstucki force-pushed the warn-users-on-non-canonical branch from 16eeae3 to d543a89 Compare October 19, 2020 14:14
Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -1,6 +1,6 @@
import scala.quoted._

def summonTypedType[T : Type](using QuoteContext): String = '[T] match {
def summonTypedType[T : Type](using QuoteContext): String = Type[T] match {
case '[Boolean] => "Boolean"
case '[Byte] => "Byte"
case _ => "Other"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rewrite this test to remove the '[Byte] syntax?

@nicolasstucki nicolasstucki force-pushed the warn-users-on-non-canonical branch from d543a89 to d88b066 Compare October 20, 2020 07:03
Help users write the correct variant of the quoted type encoding.
Explicit construction of a `scala.quoted.Type[_]` should be done with its constructor.
Explicit construction of types in not encouraged as it should be created implicitly.
Creating an `Type` explicitly is only useful for debugging purposes.
Likewise, we encourage the use of the explicit inner type reference.
@nicolasstucki nicolasstucki force-pushed the warn-users-on-non-canonical branch from d88b066 to 580b6fd Compare October 20, 2020 12:24
@nicolasstucki
Copy link
Contributor Author

Can we rewrite this test to remove the '[Byte] syntax?

I added tests/patmat/i9489b.scala which uses '{...} to do the same.

@liufengyun
Copy link
Contributor

Can we rewrite this test to remove the '[Byte] syntax?

I added tests/patmat/i9489b.scala which uses '{...} to do the same.

The alternative approach does not look as nice as the previous version. Will it be simpler to do it using reflect API? Or, users should use if/else instead of match? I think if/else is acceptable if it works.

@nicolasstucki
Copy link
Contributor Author

We could add =:= to support if/else for that precise use case. But the pattern match case '[...] will still be needed as it is more expressive. I'm not sure why you wanted to try without the case '[...] in the first place.

@liufengyun
Copy link
Contributor

I'm not sure why you wanted to try without the case '[...] in the first place.

Just for consistency, if we remove the '[T] syntax, we would not want users to use them as patterns.

@nicolasstucki
Copy link
Contributor Author

Not sure is we will remove '[..] for patterns. The asymmetric nature of quoted types demands some asymmetry in the API. We might rework the syntax later if we see the need.

This PR in combination with #10045 aims to make users of t: Type[X] use X directly everywhere and never a t.T. Unless they really want to.

@nicolasstucki nicolasstucki merged commit f081910 into scala:master Oct 20, 2020
@nicolasstucki nicolasstucki deleted the warn-users-on-non-canonical branch October 20, 2020 15:39
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
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.

3 participants