How to convert a generic HList to a List

david.perez

I have these:

trait A[T]
class X
class Y

object B {
   def method[H :< HList](h: H) = h.toList[A[_]]
}

Parameter h of method will always be a HList of A[T], like new A[X] :: new A[Y] :: HNil.

I would like to convert the HList to a List[A[_]].

How can I get this with generic code, because trait HList doesn't have the toList method()?

Travis Brown

The compiler error should tell you something about wanting an implicit value of type shapeless.ops.hlist.ToList[H, A[_]]. You can provide one of these by adding an implicit argument list to your method signature:

object B {
  def method[H <: HList](h: H)(implicit ev: ToList[H, A[_]]) = h.toList[A[_]]
}

Now you can write the following:

val someAs = new A[Int] {} :: new A[String] {} :: HNil

And then:

scala> B.method(someAs)
res0: List[A[_]] = List($anon$1@5dd508ef, $anon$2@4d3db309)

Almost every operation on an HList will require this kind of implicit evidence.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to convert a generic List to a generic array in Java?

From Dev

Can one convert an "Either" list to an "HList" in Haskell?

From Dev

How can I convert a generic list to an array?

From Dev

How to Convert DataTable to Generic List in C#

From Dev

Convert scala List[String]/List[Object] into model/HList/tuple

From Dev

How convert a non-generic List to a List<String>?

From Dev

scala - generic unzip for HList

From Dev

Map for generic HList

From Dev

How to map HList to List[Type], List[TypeTag], or List[String]

From Dev

How to write a generic method to convert List<T> to xml string

From Dev

Convert generic list to BindingList<T>

From Dev

Convert Generic Nested List to Datatable

From Dev

Convert generic list to BindingList<T>

From Dev

Convert Generic Nested List to Datatable

From Dev

How to convert object to generic

From Dev

Scala shapeless: how to convert a hlist.Mapper to a case class?

From Dev

leftReduce Shapeless HList of generic types

From Dev

leftReduce Shapeless HList of generic types

From Dev

Convert Generic.List<int?> to Generic.List<int>

From Dev

Parse List[String] into HList

From Dev

How to serialise generic list?

From Dev

How to convert a string to generic Type

From Dev

How convert a method to generic variant?

From Dev

Convert Short? to System.Collection.Generic.List

From Dev

How to convert List<T> to Array t[] (for primitive types) using generic-method?

From Dev

How to convert List<T> to Array t[] (for primitive types) using generic-method?

From Dev

How to create a generic method to convert List<Object> into ObservableList<S> and Map<T, S>?

From Dev

How to transform an HList to another HList with foldRight/foldLeft

From Dev

How to append an element to HList

Related Related

  1. 1

    how to convert a generic List to a generic array in Java?

  2. 2

    Can one convert an "Either" list to an "HList" in Haskell?

  3. 3

    How can I convert a generic list to an array?

  4. 4

    How to Convert DataTable to Generic List in C#

  5. 5

    Convert scala List[String]/List[Object] into model/HList/tuple

  6. 6

    How convert a non-generic List to a List<String>?

  7. 7

    scala - generic unzip for HList

  8. 8

    Map for generic HList

  9. 9

    How to map HList to List[Type], List[TypeTag], or List[String]

  10. 10

    How to write a generic method to convert List<T> to xml string

  11. 11

    Convert generic list to BindingList<T>

  12. 12

    Convert Generic Nested List to Datatable

  13. 13

    Convert generic list to BindingList<T>

  14. 14

    Convert Generic Nested List to Datatable

  15. 15

    How to convert object to generic

  16. 16

    Scala shapeless: how to convert a hlist.Mapper to a case class?

  17. 17

    leftReduce Shapeless HList of generic types

  18. 18

    leftReduce Shapeless HList of generic types

  19. 19

    Convert Generic.List<int?> to Generic.List<int>

  20. 20

    Parse List[String] into HList

  21. 21

    How to serialise generic list?

  22. 22

    How to convert a string to generic Type

  23. 23

    How convert a method to generic variant?

  24. 24

    Convert Short? to System.Collection.Generic.List

  25. 25

    How to convert List<T> to Array t[] (for primitive types) using generic-method?

  26. 26

    How to convert List<T> to Array t[] (for primitive types) using generic-method?

  27. 27

    How to create a generic method to convert List<Object> into ObservableList<S> and Map<T, S>?

  28. 28

    How to transform an HList to another HList with foldRight/foldLeft

  29. 29

    How to append an element to HList

HotTag

Archive