Creating a list of fixed sizes Nat N

I tried to define a function that, when setting a type parameter, N <: Natbuilds a list with exactly 3 N.

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 do not understand why this failed.

+1
source share
1 answer

One of the problems is that your nprinted as Nat, and not n- I assume this is just a typo. Once you have fixed this, you can write a method as follows:

import shapeless._, nat._

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

Note that it Sized.applyaccepts a single type type * -> *, and instead of providing a collection, you provide elements.

, wrap:

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

, .

+2

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


All Articles