Skip to content

Commit

Permalink
Expand syntax package to cover ZIO
Browse files Browse the repository at this point in the history
- Syntax now available with ZIO rather than IO
- Changed ZIO.require to return ZIO, not IO
- Fixed type parameters on RIO.require

Fixes #1445
  • Loading branch information
NeQuissimus committed Aug 25, 2019
1 parent 27311ea commit 2a80091
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 109 deletions.
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ object IO {
* @see See [[zio.ZIO.require]]
*/
final def require[E, A](error: E): IO[E, Option[A]] => IO[E, A] =
ZIO.require[E, A](error)
ZIO.require[Any, E, A](error)

/**
* @see See [[zio.ZIO.reserve]]
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/zio/RIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ object RIO {
/**
* @see See [[zio.ZIO.require]]
*/
final def require[R, A](error: Throwable): IO[Throwable, Option[A]] => IO[Throwable, A] =
ZIO.require(error)
final def require[A](error: Throwable): IO[Throwable, Option[A]] => IO[Throwable, A] =
ZIO.require[Any, Throwable, A](error)

/**
* @see See [[zio.ZIO.reserve]]
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/Task.scala
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ object Task {
* @see See [[zio.ZIO.require]]
*/
final def require[A](error: Throwable): Task[Option[A]] => Task[A] =
ZIO.require[Throwable, A](error)
ZIO.require[Any, Throwable, A](error)

/**
* @see See [[zio.ZIO.reserve]]
Expand Down
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2230,11 +2230,11 @@ private[zio] trait ZIOFunctions extends Serializable {
}

/**
* Requires that the given `IO[E, Option[A]]` contain a value. If there is no
* Requires that the given `ZIO[R, E, Option[A]]` contain a value. If there is no
* value, then the specified error will be raised.
*/
final def require[E, A](error: E): IO[E, Option[A]] => IO[E, A] =
(io: IO[E, Option[A]]) => io.flatMap(_.fold[IO[E, A]](fail[E](error))(succeed[A]))
final def require[R, E, A](error: E): ZIO[R, E, Option[A]] => ZIO[R, E, A] =
(io: ZIO[R, E, Option[A]]) => io.flatMap(_.fold[ZIO[R, E, A]](fail[E](error))(succeed[A]))

/**
* Acquires a resource, uses the resource, and then releases the resource.
Expand Down
66 changes: 0 additions & 66 deletions core/shared/src/main/scala/zio/syntax/IOSyntax.scala

This file was deleted.

39 changes: 39 additions & 0 deletions core/shared/src/main/scala/zio/syntax/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2017-2019 John A. De Goes and the ZIO Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package zio

import scala.language.implicitConversions

package object syntax {
import zio.syntax.ZIOSyntax._

implicit final def zioEagerCreationSyntax[A](a: A): EagerCreationSyntax[A] = new EagerCreationSyntax[A](a)
implicit final def zioLazyCreationSyntax[A](a: => A): LazyCreationSyntax[A] = new LazyCreationSyntax[A](() => a)
implicit final def zioIterableSyntax[R, E, A](zios: Iterable[ZIO[R, E, A]]): IterableSyntax[R, E, A] =
new IterableSyntax[R, E, A](zios)

implicit final def zioTuple2Syntax[R, E, A, B](zios: (ZIO[R, E, A], ZIO[R, E, B])): Tuple2Syntax[R, E, A, B] =
new Tuple2Syntax(zios)
implicit final def zioTuple3Syntax[R, E, A, B, C](
zios: (ZIO[R, E, A], ZIO[R, E, B], ZIO[R, E, C])
): Tuple3Syntax[R, E, A, B, C] =
new Tuple3Syntax(zios)
implicit final def zioTuple4Syntax[R, E, A, B, C, D](
zios: (ZIO[R, E, A], ZIO[R, E, B], ZIO[R, E, C], ZIO[R, E, D])
): Tuple4Syntax[R, E, A, B, C, D] =
new Tuple4Syntax(zios)
}
36 changes: 0 additions & 36 deletions core/shared/src/main/scala/zio/syntax/syntax.scala

This file was deleted.

64 changes: 64 additions & 0 deletions core/shared/src/main/scala/zio/syntax/zio.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2017-2019 John A. De Goes and the ZIO Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package zio.syntax

import zio.{ Fiber, ZIO }

object ZIOSyntax {
final class EagerCreationSyntax[A](val a: A) extends AnyVal {
def fail[R]: ZIO[R, A, Nothing] = ZIO.fail(a)
def require[R, AA]: ZIO[R, A, Option[AA]] => ZIO[R, A, AA] = ZIO.require(a)
def succeed[R, E]: ZIO[R, E, A] = ZIO.succeed(a)
}

final class LazyCreationSyntax[A](val a: () => A) extends AnyVal {
def effectTotal[R, E]: ZIO[R, E, A] = ZIO.effectTotal(a())
def sync[R]: ZIO[R, Throwable, A] = ZIO.effect(a())
}

final class IterableSyntax[R, E, A](val ios: Iterable[ZIO[R, E, A]]) extends AnyVal {
def collectAll: ZIO[R, E, Iterable[A]] = ZIO.collectAll(ios)
def collectAllPar: ZIO[R, E, Iterable[A]] = ZIO.collectAllPar(ios)
def forkAll: ZIO[R, Nothing, Fiber[E, Iterable[A]]] = ZIO.forkAll(ios)
def mergeAll[B](zero: B)(f: (B, A) => B): ZIO[R, E, B] = ZIO.mergeAll(ios)(zero)(f)
}

final class Tuple2Syntax[R, E, A, B](val ios2: (ZIO[R, E, A], ZIO[R, E, B])) extends AnyVal {
def map2[C](f: (A, B) => C): ZIO[R, E, C] = ios2._1.flatMap(a => ios2._2.map(f(a, _)))
}

final class Tuple3Syntax[R, E, A, B, C](val ios3: (ZIO[R, E, A], ZIO[R, E, B], ZIO[R, E, C])) extends AnyVal {
def map3[D](f: (A, B, C) => D): ZIO[R, E, D] =
for {
a <- ios3._1
b <- ios3._2
c <- ios3._3
} yield f(a, b, c)
}

final class Tuple4Syntax[R, E, A, B, C, D](
val ios4: (ZIO[R, E, A], ZIO[R, E, B], ZIO[R, E, C], ZIO[R, E, D])
) extends AnyVal {
def map4[F](f: (A, B, C, D) => F): ZIO[R, E, F] =
for {
a <- ios4._1
b <- ios4._2
c <- ios4._3
d <- ios4._4
} yield f(a, b, c, d)
}
}

0 comments on commit 2a80091

Please sign in to comment.