-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from djspiewak/feature/scala3
Added Scala 3 support
- Loading branch information
Showing
9 changed files
with
116 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ jobs: | |
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
scala: [2.12.14, 2.13.6] | ||
scala: [2.12.14, 2.13.6, 3.0.0] | ||
java: [[email protected]] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2021 Typelevel | ||
* | ||
* 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 cats.effect | ||
|
||
import _root_.cps.{async, await, CpsAsyncMonad, CpsAwaitable, CpsMonad, CpsMonadPureMemoization} | ||
|
||
import cats.effect.kernel.{Async, Concurrent, Sync} | ||
|
||
import scala.util.Try | ||
|
||
object cps { | ||
|
||
transparent inline def async[F[_]](using inline am: CpsMonad[F], F: Sync[F]): InferAsyncArg[F] = | ||
new InferAsyncArg[F] | ||
|
||
final class InferAsyncArg[F[_]](using am: CpsMonad[F], F: Sync[F]) { | ||
transparent inline def apply[A](inline expr: A) = | ||
F.defer(_root_.cps.Async.transform[F, A](expr)(using am)) | ||
} | ||
|
||
final implicit class AwaitSyntax[F[_], A](val self: F[A]) extends AnyVal { | ||
transparent inline def await(using inline am: CpsAwaitable[F]): A = | ||
_root_.cps.await[F, A](self) | ||
} | ||
|
||
implicit def catsEffectCpsMonadPureMemoization[F[_]](implicit F: Concurrent[F]): CpsMonadPureMemoization[F] = | ||
new CpsMonadPureMemoization[F] { | ||
def apply[A](fa: F[A]): F[F[A]] = F.memoize(fa) | ||
} | ||
|
||
// TODO we can actually provide some more gradient instances here | ||
implicit def catsEffectCpsConcurrentMonad[F[_]](implicit F: Async[F]): CpsAsyncMonad[F] = | ||
new CpsAsyncMonad[F] { | ||
|
||
def adoptCallbackStyle[A](source: (Try[A] => Unit) => Unit): F[A] = | ||
F.async_(cb => source(t => cb(t.toEither))) | ||
|
||
def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B] = F.flatMap(fa)(f) | ||
|
||
def map[A, B](fa: F[A])(f: A => B): F[B] = F.map(fa)(f) | ||
|
||
def pure[A](a: A): F[A] = F.pure(a) | ||
|
||
def error[A](e: Throwable): F[A] = F.raiseError(e) | ||
|
||
def flatMapTry[A,B](fa: F[A])(f: Try[A] => F[B]): F[B] = | ||
F.flatMap(F.attempt(fa))(e => f(e.toTry)) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
core/shared/src/test/scala-2/cats/effect/AsyncAwaitCompilationSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cats.effect | ||
|
||
import org.specs2.mutable.Specification | ||
|
||
class AsyncAwaitCompilationSpec extends Specification { | ||
|
||
"async[F]" should { | ||
"prevent compilation of await[G, *] calls" in { | ||
val tc = typecheck("async[OptionTIO](IO(1).await)").result | ||
tc must beLike { | ||
case TypecheckError(message) => | ||
message must contain("expected await to be called on") | ||
message must contain("cats.data.OptionT") | ||
message must contain("but was called on cats.effect.IO[Int]") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters