Skip to content

Add quotes.Type.valueOfConstant #11715

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
merged 1 commit into from
Mar 15, 2021

Conversation

nicolasstucki
Copy link
Contributor

With quote pattern matching it is possible to match over the structure of a type, this add to possility to extract the values from constant types.

This is esential to provide handle Mirror.{MirroredLabel, MirroredElemLabels} witthout needing reflection.

With quote pattern matching it is possible to match over the structure of a type, this add to possility to extract the values from constant types.

This is esential to provide handle `Mirror.{MirroredLabel, MirroredElemLabels}` witthout needing reflection.
@nicolasstucki nicolasstucki self-assigned this Mar 12, 2021
@nicolasstucki nicolasstucki marked this pull request as ready for review March 13, 2021 14:31
@nicolasstucki nicolasstucki added the release-notes Should be mentioned in the release notes label Mar 13, 2021
@liufengyun
Copy link
Contributor

This is essential to provide handle Mirror.{MirroredLabel, MirroredElemLabels} without needing reflection.

It seems to me using reflection in such cases agrees with the design philosophy of doing complex things in reflection. There are several concerns:

(1) adding ad-hoc methods is difficult to learn & remember.
(2) the approach does not scale, in contrast to the more principled approach based on reflection.
(3) it's not clear that Type.valueOfConstant is different from a utility reflection method in essence.

@nicolasstucki
Copy link
Contributor Author

This is essential to provide handle Mirror.{MirroredLabel, MirroredElemLabels} without needing reflection.

It seems to me using reflection in such cases agrees with the design philosophy of doing complex things in reflection. There are several concerns:

The design philosophy is to make simple thing easy and safe and complex things possible. The following points go against it.
Mirror should be simple to handle.

(1) adding ad-hoc methods is difficult to learn & remember.

It is in the same place all other methods on type are located. It is also the simplest part of the whole matching on Mirrors logic, if a user knows how to match against a mirror this operation will be trivial.

(2) the approach does not scale, in contrast to the more principled approach based on reflection.

Scale how? This is the leaf of the operations that we need, scaling to complex types is already handled by pattern match. The point is to have a safe and simple API. Using reflection is not a principled way to do it unless you redefine this operation in each project that needs it (i.e. every project matching on mirrors).

(3) it's not clear that Type.valueOfConstant is different from a utility reflection method in essence.

It is type-safe.

@liufengyun
Copy link
Contributor

Scale how?

It seems we are adding more methods that are based on reflection to achieve type-safety --- I'm wondering what is the criteria for doing/not-doing so. This is the concern about scalability in my mind.

This is the leaf of the operations that we need, scaling to complex types is already handled by pattern match.

Maybe valueOfConstant should be an extractor for literal types?

@nicolasstucki
Copy link
Contributor Author

Scale how?

It seems we are adding more methods that are based on reflection to achieve type-safety --- I'm wondering what is the criteria for doing/not-doing so. This is the concern about scalability in my mind.

Not sure how this could scale. We will probably not be adding more singleton constant type to the language any time soon.

This is the leaf of the operations that we need, scaling to complex types is already handled by pattern match.

Maybe valueOfConstant should be an extractor for literal types?

That was a mistake we previously did with the extraction of values out of expression. It made it harder to use and in many cases ended up calling the same extractor several times. The solution was to add Expr.value which does the same as this method.

@liufengyun
Copy link
Contributor

Scale how?

It seems we are adding more methods that are based on reflection to achieve type-safety --- I'm wondering what is the criteria for doing/not-doing so. This is the concern about scalability in my mind.

Not sure how this could scale. We will probably not be adding more singleton constant type to the language any time soon.

This is the leaf of the operations that we need, scaling to complex types is already handled by pattern match.

Maybe valueOfConstant should be an extractor for literal types?

That was a mistake we previously did with the extraction of values out of expression. It made it harder to use and in many cases ended up calling the same extractor several times. The solution was to add Expr.value which does the same as this method.

I think the context is different. For Expr.Value, it's a problem because there is the design choice whether we should introduce Const(x) or introduce a more general Unlift(v). There is no such concern here.

@nicolasstucki
Copy link
Contributor Author

nicolasstucki commented Mar 15, 2021

Still, we need this if we want people to write type class derivation using macros without them doing horrible workarounds.

Without this we would make the complex part simple and the trivial part hard. That isn't a good idea.

@nicolasstucki nicolasstucki added this to the 3.0.0-RC2 milestone Mar 15, 2021
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

I'll leave you to decide whether it's better to be an extractor or not.

@nicolasstucki nicolasstucki merged commit 77b0ae0 into scala:master Mar 15, 2021
@nicolasstucki nicolasstucki deleted the add-Type-valueOfConstant branch March 15, 2021 14:15
@Kordyjan Kordyjan modified the milestones: 3.0.0-RC2, 3.0.0 Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes Should be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants