Search code examples
scalashapeless

Building Fixed Size List of Nat N


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.


Solution

  • 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.