首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >F[T1]:F[Tn] ::HNil到类型T1 : Tn ::HNil (类型级排序)

F[T1]:F[Tn] ::HNil到类型T1 : Tn ::HNil (类型级排序)
EN

Stack Overflow用户
提问于 2014-10-21 20:39:46
回答 2查看 333关注 0票数 8

我正在构建一个泛型函数,它接受表单HListF[T1] :: ... :: F[Tn] :: HNil,将其转换为F[T1 :: ... :: Tn :: HNil],然后需要将其传递到传入的块中。但是,为了使其工作,我需要提取F[_]中的F[_]类型。我在无形的“hlistconstraints”下发现了一些远程相关的东西

代码语言:javascript
复制
/**
 * Type class witnessing that every element of `L` has `TC` as its outer type constructor. 
 */
trait UnaryTCConstraint[L <: HList, TC[_]]

...but --它只能用于验证传入的hlist确实是由F[_]组成的;但是,似乎没有办法提取出该_位--也就是说,对它自己的hlist。

我应该在哪里寻找做这项工作的东西呢?或者,我不应该期望自己发现任何东西,而是自己构建类型计算?

披露:在我看来,这个问题是https://stackoverflow.com/questions/26487430/generic-transform-fold-map-over-tuple-hlist-containing-some-f的辅助问题,但至少和一个独立的问题一样有用。

EN

回答 2

Stack Overflow用户

发布于 2014-10-22 15:53:22

看起来定序器已经这样做了:

代码语言:javascript
复制
import scala.language.higherKinds

class Wrap[TC[_]] {
  def foo[L1 <: HList, L2 <: HList](xs: L1)(implicit
    seq: Sequencer.Aux[L1, TC[L2]] // L2 is the type we're looking for
  ): L2 = ???
}

val ret = new Wrap[Option].foo(1.some :: 2.some :: HNil)
// ret now has type Int :: Int :: HNil

...but,我现在想不出一种办法让这件事变得更好

  • 摆脱包装类;
  • 让Scala推断TCOption

注意:我认为这是一个有点有用的答案,但我不会接受它-希望有人会想出一个更通用和更好看的解决方案。

票数 3
EN

Stack Overflow用户

发布于 2018-11-15 16:48:27

为了实现这一点,F至少具有应用能力。一旦确保了这一点,就很有可能如下面的代码所示:

代码语言:javascript
复制
trait HApplicative[Eff[_], InL <: HList] extends {
  type OutL <: HList
  def product: InL => Eff[OutL]
}

object HApplicative {
  case class HAppAux[Eff[_], InL <: HList, OutL0 <: HList](zipper: InL => Eff[OutL0]) extends HApplicative[Eff, InL] {
    type OutL = OutL0

    override def product: InL => Eff[OutL0] = zipper
  }

  implicit def nilHApp[Eff[_]](implicit app: Applicative[Eff]): HApplicative[Eff, HNil] {type OutL = HNil} =
    HAppAux[Eff, HNil, HNil](_ => app.pure(HNil))


  implicit def consHApp[Eff[_], InH, InT <: HList](
    implicit tailHApp: HApplicative[Eff, InT],
    app: Applicative[Eff]
  ): HApplicative[Eff, Eff[InH] :: InT] {type OutL = InH :: tailHApp.OutL} = HAppAux[Eff, Eff[InH] :: InT, InH :: tailHApp.OutL] {
    case (inH: Eff[InH]) :: inT => app.map2(inH, tailHApp.product(inT)) {
      case (head, tail) => head :: tail
    }
  }

  def product[Eff[_],InL<:HList](inL:InL)(implicit happ:HApplicative[Eff,InL]):happ.OutL = happ.product(inL)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26495891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档