Skip to content

Latest commit

 

History

History
44 lines (34 loc) · 1.37 KB

accessible.md

File metadata and controls

44 lines (34 loc) · 1.37 KB

@accessible annotation

Generates helpers to access service capabilities from anywhere.

To use simply add "dev.zio" %% "zio-macros-core" % "<version>" to your libraryDependencies.

The @accessible annotation can be used on modules following the module pattern.

When applied to the module it will autogenerate the container with a given name in the module's companion object with helpers to access service capabilities:

import zio.macros.annotation.accessible

@accessible(">")
trait Example {
  val example: Example.Service[Any]
}

object Example {
  trait Service[R] {
    def foo()                 : ZIO[R, Nothing, Unit]
    def bar(v1: Int, v2: Int) : ZIO[R, Nothing, Int]
    def baz(v1: Int)(v2: Int) : ZIO[R, Nothing, String]
  }

  // -- below code is autogenerated -- //
  object > extends Service[Example] {
    def foo(): ZIO[Example, Nothing, Unit]                   = ZIO.accessM(_.example.foo)
    def bar(v1: Int, v2: Int): ZIO[Example, Nothing, Int]    = ZIO.accessM(_.example.bar(v1, v2))
    def baz(v1: Int)(v2: Int): ZIO[Example, Nothing, String] = ZIO.accessM(_.example.baz(v1)(v2))
  }
  // -- end of autogenerated code -- //
}

You can use these helpers to refer to service capabilities like:

val myProgram =
  for {
    _ <- Example.>.foo
    _ <- Example.>.bar(1, 2)
  } yield ()