Skip to content

Homogenize Reflect Type constructors #9744

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

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Sep 7, 2020

  • Rename Reflect.typeOf to Reflect.Type.of
  • Rename Reflect.Type.apply to Reflect.Type.ofClass
  • Make Reflect.Type.of AnyKinded
  • Improve some other type uses in community build

@nicolasstucki nicolasstucki marked this pull request as draft September 7, 2020 13:35
@nicolasstucki nicolasstucki changed the title Improve macro implementation Improve macro implementations Sep 7, 2020
@nicolasstucki nicolasstucki self-assigned this Sep 7, 2020
@nicolasstucki nicolasstucki force-pushed the improve-macro-implementations branch from ac6fc35 to 553330b Compare September 8, 2020 09:36
@nicolasstucki nicolasstucki changed the title Improve macro implementations Homogenize Reflect Type constructors Sep 8, 2020
@nicolasstucki nicolasstucki force-pushed the improve-macro-implementations branch 3 times, most recently from 389b550 to d78262c Compare September 8, 2020 13:04
@nicolasstucki nicolasstucki force-pushed the improve-macro-implementations branch from b819291 to f5d1bf4 Compare September 8, 2020 14:54
@nicolasstucki nicolasstucki marked this pull request as ready for review September 8, 2020 15:44
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.

Otherwise, LGTM

qtype.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree.tpe

/** Returns the type or kind of the class of the type T */
def ofClass[T](using ct: scala.reflect.ClassTag[T]): Type =
Copy link
Contributor

Choose a reason for hiding this comment

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

The existence of both Type.of and Type.ofClass can cause confusion. What about change Type.of to Type.apply. We can also change return type of Type.of to Type[T]:

def apply[T <: AnyKind](using qtype: scala.quoted.Type[T]): Type[T] = qtype

// usage
Type[T]

I see in the test code we write Type.of[T].seal in many places. Without the change above, it's simpler.

Then we can rename Type.ofClass to Type.of.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can Type.of subsumes all usage of Type.ofClass?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not notice the Type.of[T].seal. That is just bad code, they should be replaced with '[T] unless that are tests on the seal operation. I will check all those occurrences.

Type.of does subsume most cases of Type.ofClass. The only case it does not is if you get a Class[_] at runtime and want to recover the Type from it. The second is also slightly more performant.

The apply is not good because it leads to easy mistakes as quoted.Type[T] and Reflect.Type[T] dot not return the same thing. Knowing that you need to use Type.of helps users find the correct version and autocompletion may help to find the other variants that are more specific/performant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually Type.of[T].seal is equivalent to '[T].asInstanceOf[Type[?]] which is relly bad code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I only see 2 tests files with Type.of[T].seal where we explicitly test the seal operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it takes an explicit argument no one will use it as they would have to write much more code for something that they should be encouraged to do. Type.ofClass[Foo] would become Type.ofClass(classOf[Foo]). Additionally, that would introduce extra redundancy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This concept is in the middle between Type.of and classOf and it makes sense to have a name that intuitively makes allusion to both those concepts.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I have exhausted my arguments ;)
I'm happy to hear what others think about it.
I'm open to any agreed decision in order to move on.

/cc: @anatoliykmetyuk @bishabosha

Copy link
Member

@bishabosha bishabosha Sep 10, 2020

Choose a reason for hiding this comment

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

I don't mind the name but I am not sure what the benefit is except to conveniently have an unapplied polymorphic type (when generating ClassTag still requires a statically known type)

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 unfortunately unavoidable for any ClassTag or classOf.

* Rename Reflect.typeOf to Reflect.Type.of
* Rename Reflect.Type.apply to Reflect.Type.ofClass
* Make Reflect.Type.of AnyKinded
* Improve some other type uses in community build
@nicolasstucki
Copy link
Contributor Author

Replaced with #9796 and #9798

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