-
-
Notifications
You must be signed in to change notification settings - Fork 117
Prepend "using" at the callsite when definition site has implicit parameter #933
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
|
I should add tests and only prepend "using" for scala3+ π . I'm not sure if this pr is the correct way to solve the issue tbh, if you have something particular in mind I'm willing to work on it. |
| object TwirlCompiler { | ||
|
|
||
| var isScala3 = false | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this var isScala3 is not necessary.
If you declare emitScala3Sources as a val, it can be referred to later as sc.emitScala3Sources.
| .mkString(",") + ")", | ||
| returnType, | ||
| val hasContextParameters = | ||
| params.flatten.exists(_.mods.exists(_.toString.contains("implicit"))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the following approach can be used to check whether implicit is present.
It might be cleaner than relying on toString.
import scala.meta.classifiers._
mod.is[Mod.Implicit]
| if (TwirlCompiler.isScala3 && hasContextParameters && idx == params.size - 1) | ||
| groupStr.replace("(", "(using ") | ||
| else groupStr | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be working well.
However, I thought it might be more readable if the processing were completed within the previous map { ... } block, rather than using the zipWithIndex.map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unable to avoid relying on zipWithIndex as the logic is not clear to me, however, I have reduced the number of transformations by moving it to the beginning of the said transformations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it wouldnβt be necessary to check whether a parameter is the last, since an implicit parameter is always the last parameter group.
However, I think your current implementation is fine. thanks.
|
@ajafri2001 |
|
@tototoshi Hi, I'm unable to get the CompilerSpec Tests to compile with scala3 instead of scala2, I have added the tests but they now fail. Edit - Also the parser does not recognize |
|
On my end, the tests seem to pass with Scala 3, but not with Scala 2
I had the same thought as well. As for the TwirlCompiler, I think your current fix could be extended slightly to support this case too. |
Yes, you were right this worked πββοΈ, twirl is now able to compile the syntax The test fails now because |
|
You can do it like this: or |
|
The reason the tests are failing on Scala 2 is not due to the callsite, but because there's an error at the definition site. So I believe it's unrelated to the changes in this PR. |
|
Ahh, the tests fail with scala2 but pass with scala3 because, as you mentioned, However, the test i've added namely How should i deal with this situation and make the test run ONLY with scala3 (since it's a scala3 specific feature)? I'm really grateful for your help so far π |
|
If you want a test to run only on Scala 3, you can place it under src/test/scala-3. Alternatively, a simpler way is to check the Scala version at runtime and decide whether to run the test.
|
|
Thanks the tests pass now |
β¦plicit parameter
mkurz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work!
Code looks good and also did some local testing.
Regarding the parser I also was unsure if we need some modifcation, but you as you mentioned correctly the parser does not check for keywords inside brackets, it's handling the content within (...) stupidly: https://github.com/playframework/twirl/blob/2.0.8/parser/src/main/scala/play/twirl/parser/TwirlParser.scala#L988-L994
We can backport this to the stable branches, pretty sure this is a non breaking addition.
Thanks!
|
@Mergifyio backport 2.0.x 1.6.x |
β Backports have been createdDetails
|
[1.6.x] Prepend "using" at the callsite when definition site has implicit parameter (backport #933) by @ajafri2001
[2.0.x] Prepend "using" at the callsite when definition site has implicit parameter (backport #933) by @ajafri2001
|
@tototoshi @mkurz |
Fixes issue-894, issue-839