Understanding `Monomorphism` Example of Shapeless

心已入冬 提交于 2019-12-04 05:46:52

The important difference here is, that choose is a function that can be passed around as value. You cannot make a (reasonable) value out of f, since Scala does not support polymorphic function values:

scala> val fun = f _
fun: Set[Nothing] => Option[Nothing] = <function1>

As you see, Scala fixes the element type to Nothing rendering the function useless for non-empty sets:

scala> fun(Set(1))
<console>:10: error: type mismatch;
 found   : Int(1)
 required: Nothing
              fun(Set(1))
                      ^

This works as you would expect with the Shapeless approach.

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