Function that retrieves element from HList (while preserving its type)

别等时光非礼了梦想. 提交于 2019-12-25 03:27:23

问题


I have this type which will be generated via shapeless:

type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil

Basically I have a bunch of case objects extending a trait so I managed to create a method which gives me instances of all case objects as an HList

Then using import shapeless.ops.hlist.Last and init I wrote a method to retrieve one of the nodes in the HList if the value is equal to the string "student":

def getLast(hl:hlistt) = {
  val last0=Last[hlistt]
  val la=last0(hl)

  if (la.value == "student") la
  else init(hl)
}

The issue is that if I call this method I will not get the correct node type from the HList.

getLast(STUDENT :: AUTO_LOANS :: HNil)

The method works and returns the node but the type is off:

Product with Serializable = STUDENT :: HNil

Do I need some Witness/Aux implicits to return the correct type?


回答1:


la is of type AUTO_LOANS.type, init(hl) is of type STUDENT.type :: HNil, so

if (la.value == "student") la
else init(hl)

is of type Any (or Product with Serializable).

If you would like to return values of different types from different branches you need a Poly.

import shapeless.{Poly1, Witness}

object myPoly extends Poly1 {
  implicit def studentCase: Case.Aux[Witness.`"student"`.T, STUDENT.type] = 
    at(_ => STUDENT)
  implicit def autoLoansCase: Case.Aux[Witness.`"auto-loans"`.T, AUTO_LOANS.type] = 
    at(_ => AUTO_LOANS)
}

import shapeless.syntax.singleton._
println(
  myPoly("student".narrow)
) // STUDENT

println(
  myPoly("auto-loans".narrow)
) // AUTO_LOANS

// println(
//   myPoly("abc".narrow)
// )  // doesn't compile

This approach works if the string is known at compile time.




回答2:


I am not quite sure what you want to do. Given:

type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil

Last[hlistt] will resolve to AUTO_LOANS.type (your true if branch) while init will resolve to STUDENT :: HNil (your false if branch)

the LUB (least upper bound) of these types will be Product with Serializable so that's why you see that.

If you want to check a runtime property of a member of the hlist you'll have to thread the appropriate typebounds and result types through by deriving them with the appropriate machinery. In this case that is already given by shapeless.

https://scalafiddle.io/sf/fdtn3cz/0

Is this what you wanted?

EDIT: I also just read

I have this type which will be generated dynamically:

what do you exactly mean by "dynamically"? because unless you can specify some compile time properties, shapeless might not be the solution you're looking for.



来源:https://stackoverflow.com/questions/53388226/function-that-retrieves-element-from-hlist-while-preserving-its-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!