【发布时间】:2022-01-16 16:18:27
【问题描述】:
对于以下使用猫效果的随机 UUID 生成器:
import java.util.UUID
import cats.effect.Sync
import cats.ApplicativeThrow
trait UuidGen[F[_]]:
def make: F[UUID]
def read(string: String): F[UUID]
object UuidGen:
def apply[F[_]: UuidGen]: UuidGen[F] = implicitly
implicit def forSync[F[_]: Sync]: UuidGen[F] =
new UuidGen[F]:
def make: F[UUID] =
Sync[F].delay(UUID.randomUUID)
def read(string: String): F[UUID] =
ApplicativeThrow[F].catchNonFatal(UUID.fromString(string))
scala 3 given 语法中 implicit def 的等价物是什么?
【问题讨论】:
-
你自己尝试过什么?遇到错误?
-
官方文档应该可以帮助您直截了当:docs.scala-lang.org/scala3/reference/contextual/givens.html
-
类似:
given RandomUuid[F](using F: Sync[F]) with和下一行def make: F[UUID] = F.delay(UUID.randomUUID) -
谢谢。我已经发布了我的实现作为答案。不幸的是,scala-lang.org 昨天下线了。 github.com/scala/scala-lang/issues/1309
标签: scala implicit scala-3 cats-effect