Why does display through HList Option [T] not work?

This does not compile, and I do not understand why:

import shapeless._ import poly._ object option extends (Option ~> List) { def apply[T](t: Option[T]) = t.toList } val simple = Some(1) :: Some("hello") :: Some(true) :: HNil val opts: List[Int] :: List[String] :: List[Boolean] :: HNil = simple map option 

This is somewhat surprising because it compiles:

 import shapeless._ import poly._ object choose extends (Set ~> Option) { def apply[T](s: Set[T]) = s.headOption } val sets = Set(1) :: Set("foo") :: HNil val opts: Option[Int] :: Option[String] :: HNil = sets map choose 
+6
source share
1 answer

The problem is using Some instead of Option . The compiler complains that it cannot find anything to convert the Some list, although your Option . To fix this, you can:

Change the structure of the simple list to use Option instead of Some :

  object option extends (Option ~> List) { def apply[T](t: Option[T]) = t.toList } val simple = Option(1) :: Option("hello") :: Option(true) :: HNil val opts2: List[Int] :: List[String] :: List[Boolean] :: HNil = simple map option 

Use Some as the type of your mapper:

  object option extends (Some ~> List) { def apply[T](t: Some[T]) = t.toList } val simple = Some(1) :: Some("hello") :: Some(true) :: HNil val opts2: List[Int] :: List[String] :: List[Boolean] :: HNil = simple map option 

Or set the type of simple to be pressed before Option :

  object option extends (Option ~> List) { def apply[T](t: Option[T]) = t.toList } val simple: Option[Int] :: Option[String] :: Option[Boolean] :: HNil = Some(1) :: Some("hello") :: Some(true) :: HNil val opts2: List[Int] :: List[String] :: List[Boolean] :: HNil = simple map option 
+6
source

Source: https://habr.com/ru/post/976071/


All Articles