Skip to content

Properly desugar inline given .. with .. #14284

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 5 commits into from
Jan 27, 2022

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Jan 17, 2022

inline given Foo with { .. }

now becomes

inline given def given_Foo: given_Foo = new given_Foo
class given_Foo extends Foo with { ... }

There are no creation of lazy vals when we have inline.
We inline the new givne_Foo only, the class does not get inlined.
This is useful to remove the instatiation of the given instance when
the methods are inline themselfs.

Fixes #14282
Fixes #14177

@nicolasstucki
Copy link
Contributor Author

Replaces #14207

```scala
inline given Foo with { .. }
```

now becomes
```scala
inline given def given_Foo: Foo with {..} = new Foo
class given_Foo extends Foo with {  }
```

There are no creation of `lazy vals` when we have `inline`.
We inline the `new givne_Foo` only, the class does not get inlined.
This is useful to remove the instatiation of the given instance when
the methods are inline themselfs.

Fixes scala#14282
Fixes scala#14177
@nicolasstucki nicolasstucki marked this pull request as ready for review January 17, 2022 15:38
@nicolasstucki nicolasstucki requested a review from smarter January 17, 2022 15:39
Copy link
Member

@smarter smarter left a comment

Choose a reason for hiding this comment

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

This would need to be documented in the "Given Instance Initialization" section of https://dotty.epfl.ch/docs/reference/contextual/givens.html : If I understand this correctly, if the trait extended by the given instance has side effects in its constructor, they will now be executed at each invocation (we should have tests for that too).

I think this looks reasonable but it's still a spec change so /cc @odersky (and it will need to be in the release notes of a minor version presumably)

@@ -0,0 +1,15 @@
class T

inline given fail1: T with
Copy link
Member

@smarter smarter Jan 17, 2022

Choose a reason for hiding this comment

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

Does transparent inline work / do anything useful 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.

It makes sure that we do not hit the previous issue where the summonAll was not inlined due to the inline flag that was kept by mistake on the class fail1.

Copy link
Member

@smarter smarter Jan 18, 2022

Choose a reason for hiding this comment

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

I mean, there are no testcases with transparent in this pr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I completely missed the transparent. I will add some test cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Transparent is failing for the wrong reason. I do not see how transparent would be useful here because we already have the precise type. I will disallow this combination and make sure the error message is correct.

@nicolasstucki nicolasstucki requested a review from smarter January 19, 2022 09:07
Comment on lines +111 to +113
inline given Show[Foo] with {
/*transparent*/ inline def show(x: Foo): String = ${ ... }
}
Copy link
Member

Choose a reason for hiding this comment

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

This example explains inline given but also uses inline on the method definition even though these two concepts are orthogonal, couldn't the example use a non-inline def to be kept simple?

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 could not come up with a useful example of inlining those givens that did not involve also inlining the method.

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, the only reason why we did not disallow inline given .. with .. is for the case where it contains an inline method (as I originally intended in #14207).

Copy link
Member

Choose a reason for hiding this comment

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

Could the doc describe a concrete situation where it's useful then? I imagine it's needed for the method call to actually be inlined but that should be shown explicitly with an example because it's not obvious

Copy link
Contributor

Choose a reason for hiding this comment

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

I imagine it's needed for the method call to actually be inlined but that should be shown explicitly with an example because it's not obvious

The use case of the issue at the origin of this PR was compile-time typeclass. The goal is to allow, as you said, to fully inline the method call. For example, I use it for type refinements/constraints.

@nicolasstucki nicolasstucki merged commit 6353ed9 into scala:master Jan 27, 2022
@nicolasstucki nicolasstucki deleted the fix-#14282 branch January 27, 2022 09:47
@Kordyjan Kordyjan added this to the 3.1.3 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.

Facilitate compile-time typeclass creation inline given ... with not compiling properly
4 participants