简体   繁体   English

如何转换成精炼型?

[英]How to convert into refined type?

I am using the library https://github.com/fthomas/refined and would like to convert java.util.UUID to refined's Uuid .我正在使用库https://github.com/fthomas/refined并想将java.util.UUID转换为精制的Uuid
How to convert java.util.UUID to refined's Uuid ?如何将java.util.UUID转换为细化的Uuid

Update更新

I have the following http routes:我有以下 http 路线:

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

and the read function is defined as follows:读取的 function 定义如下:

  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)
      }

The compiler complains:编译器抱怨:

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

       ^

Here is transformation java.util.UUID into eu.timepit.refined.api.Refined[String, eu.timepit.refined.string.Uuid]这是将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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将类型投影应用于精炼类型 - Apply type projection to a refined type 为“精炼”类型获取正确的类型构造函数参数 - Getting proper type constructor parameters for a “refined” type 使用相同谓词进行细化时如何使用 Scala 的 Refined 库确保类型安全 - How to ensure type safety with Scala's Refined library when using the same predicate for refinement Scala 中的类型细化但不使用细化 - Type refinements in Scala but without using refined 如何使用精炼来表达约束大于22的约束 - How using refined to express constraints with constants > 22 通过匹配与类型成员相对应的成员值来获取精炼类型 - Getting a refined type by matching a member value corresponding to a type member 使用 circe 和 Http4s 时精炼类型的解码器 - decoder for refined type when using circe with Http4s Scala:如何将`MatchesRegex` 细化与包含反引号(细化库)的正则表达式一起使用? - Scala: How to use a `MatchesRegex` refinement with a regex containing a backtick (refined library)? 为什么我们需要为某些类型计算的输出指定精炼类型(或其等效的Aux)? - Why do we need to specify a refined type (or its equivalent Aux) for the output of certain type computations? 如何覆盖 ScalaCheck 的一些生成器以强制(自动)生成精炼类型? 仅非空列表,例如 - How to override some generators for ScalaCheck to force to (automatically) generate refined types? Non empty lists only, for example
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM