Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@myazinn
Copy link
Contributor

@myazinn myazinn commented Oct 19, 2020

Closes #3971

The purpose of this PR is to add @accessibleM macro that will support HKT and should work as follows

@accessibleM[Task]
object MacroTest {
  trait Service[F[_]] {
    def foo: F[Int]
  }
  
  // generated by @accessibleM macro
  def foo: ZIO[Has[Service[Task]], Throwable, Int] = ZIO.accessM(_.get[Service[UIO]].foo)
}

Same for the @accessibleMM macro

@accessibleMM[URIO]
object MacroTest {
  trait Service[F[_, _], R] {
    def foo: F[R, Int]
  }
  
  // generated by @accessibleMM macro
  def foo[R]: ZIO[Has[Service[URIO]] with R, Nothing, Int] = ZIO.accessM(_.get[Service[URIO, R]].foo)
}

Both @accessibleM and @accessibleMM support polymorphic functions and traits as good as existing @accessible does.
For simplicity, for now it is only possible to use type aliases defined in ZIO (i.e. you can't use custom type alias nor type lambda).

@myazinn myazinn force-pushed the add-accessibleM-macro branch from 82fb2f2 to 664909e Compare October 19, 2020 12:51
@myazinn myazinn force-pushed the add-accessibleM-macro branch from 664909e to 7b3beed Compare October 27, 2020 15:06
neko-kai
neko-kai previously approved these changes Oct 27, 2020
Copy link
Member

@neko-kai neko-kai left a comment

Choose a reason for hiding this comment

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

Aside from the 2 comments I left, I think you've done a fantastic job and it would be great to see this merged soon! 👍

private val typeParamToInject = {
val candidates =
moduleInfo.serviceTypeParams.filter { tp =>
tp.tparams.forall(_.name == `_`) && tp.tparams.size == expectedTypeParams
Copy link
Member

Choose a reason for hiding this comment

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

tp.tparams.forall(_.name == _)

That's an odd check. It's legal to name type parameters in Scala, as in trait Service[F[e, a]]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it was really too strict a requirement. Fixed.

"""
@accessibleMM[RIO]
object Module {
trait Service[-A, F[_, _]] {
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to add tests also for variance annotations inside the F parameter as in:

trait Service[F[+_, _]] // should work and variance should be preserved in output code (e.g. Service[UIO] <: Service[Task])
trait Service[F[_, +_]] // should work and variance should be preserved in output code (e.g. Service[UIO] <: Service[Task])
trait Service[F[+_, +_]] // should work and variance should be preserved in output code (e.g. Service[UIO] <: Service[Task])

trait Service[F[-_, _]] // should fail because IO is not contravariant
trait Service[F[_, -_]] // should fail because IO is not contravariant

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've added 4 more test at the end of AccessibleMMSpec and AccessibleMSpec. They don't cover all cases due to limitations of typechecking in tests, but I hope this is better than nothing.
Thank you for your help!

@myazinn myazinn force-pushed the add-accessibleM-macro branch 5 times, most recently from 042718f to 1d70037 Compare October 28, 2020 17:35
neko-kai
neko-kai previously approved these changes Oct 28, 2020
Copy link
Member

@neko-kai neko-kai left a comment

Choose a reason for hiding this comment

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

LGTM

@myazinn
Copy link
Contributor Author

myazinn commented Oct 28, 2020

I had to push a small fix, as I remembered that ZIO streams are not included in ZIO.

@myazinn myazinn force-pushed the add-accessibleM-macro branch 4 times, most recently from 33f7edf to 74a0599 Compare October 29, 2020 08:44
@myazinn myazinn force-pushed the add-accessibleM-macro branch from 74a0599 to f2a7a9e Compare October 29, 2020 09:28
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.

zio-macros: support higher-kinded polymorphic services

3 participants