gpt4 book ai didi

scala - 如何转换成精致型?

转载 作者:行者123 更新时间:2023-12-05 02:06:38 24 4
gpt4 key购买 nike

我正在使用图书馆 https://github.com/fthomas/refined并希望将 java.util.UUID 转换为精炼的 Uuid
如何将java.util.UUID转换为refined的Uuid

更新

我有以下 http 路由:

  private val httpRoutes: HttpRoutes[F] = HttpRoutes.of[F] {
case GET -> Root / UUIDVar(id) =>
program.read(id)

读取函数定义如下:

  def read(id: Uuid): F[User] =
query
.read(id)
.flatMap {
case Some(user) =>
Applicative[F].pure(user)
case None =>
ApplicativeError[F, UserError].raiseError[User](UserNotRegistered)
}

编译器提示:

type mismatch;
[error] found : java.util.UUID
[error] required: eu.timepit.refined.string.Uuid
[error] program.read(id)
[error]

^

最佳答案

这里是将 java.util.UUID 转换为 eu.timepit.refined.api.Refined[String, eu.timepit.refined.string.Uuid]

import java.util.UUID    
import eu.timepit.refined.string.Uuid
import eu.timepit.refined.api.Refined

val uuid: UUID = UUID.fromString("deea44c7-a180-4898-9527-58db0ed34683")

val uuid1: String Refined Uuid = Refined.unsafeApply[String, Uuid](uuid.toString)

关于scala - 如何转换成精致型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62645127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com