Building Fixed Size List of Nat N

倖福魔咒の 提交于 2019-12-11 06:24:26

问题


I tried to define a function that, given a N <: Nat type parameter, builds a List with exactly 3 N's.

import shapeless._
import shapeless.nat._

scala> def natNOfSize3[N <: Nat](n: Nat): Sized[List[N], _3] = 
     Sized[List, _3](List(n, n, n))
<console>:17: error: wrong number of type parameters for overloaded method value apply with alternatives:
  [CC[_]]()(implicit cbf: scala.collection.generic.CanBuildFrom[Nothing,Nothing,CC[Nothing]], implicit ev: shapeless.AdditiveCollection[CC[Nothing]])shapeless.Sized[CC[Nothing],shapeless._0] <and>
  [CC[_]]=> shapeless.SizedBuilder[CC]

       def natNOfSize3[N <: Nat](n: Nat): Sized[List[N], _3] = Sized[List, _3](List(n, n, n))             ^

But I don't understand why it failed.


回答1:


One issue is that your n is typed as Nat, not N—I assume that's just a typo. Once you've fixed that, you can write the method like this:

import shapeless._, nat._

def natNOfSize3[N <: Nat](n: N): Sized[List[N], _3] = Sized[List](n, n, n)

Note that Sized.apply takes a single type parameter of kind * -> *, and instead of providing a collection, you provide the elements.

If you really want to pass in a collection, you could use wrap:

def natNOfSize3[N <: Nat](n: N): Sized[List[N], _3] = Sized.wrap(List(n, n, n))

But then the compiler isn't going to be able to help you if you've lied about the number of elements.



来源:https://stackoverflow.com/questions/39876940/building-fixed-size-list-of-nat-n

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