11

I need something like this:

  def encryptBlock(arr: FixedArray[Size16]) = ???
  val blocks = originalArray.splitFixed[Size16]
  val encrypted = encryptBlock(blocks)
  

In order to be sure that I receive only 128-bit array as input.

1 Answer 1

11

Shapeless can do that for seqs:

import shapeless._
import nat._
import syntax.sized._

scala> def func(l: Sized[List[Int], _3]) = l
func: (l: shapeless.Sized[List[Int],shapeless.nat._3])shapeless.Sized[List[Int],shapeless.nat._3]

scala> List(1,2,3,4,5,6).grouped(3).map(_.sized(3).get).map(func)
res26: Iterator[shapeless.Sized[List[Int],shapeless.nat._3]] = non-empty iterator

scala> List(1,2,3,4,5,6).grouped(2).map(_.sized(2).get).map(func)
<console>:25: error: type mismatch;
 found   : shapeless.Sized[List[Int],shapeless.nat._3] => shapeless.Sized[List[Int],shapeless.nat._3]
 required: shapeless.Sized[List[Int],shapeless.Succ[shapeless.Succ[shapeless._0]]] => ?
              List(1,2,3,4,5,6).grouped(2).map(_.sized(2).get).map(func)

Parameter passed to .size should be Literal(Constant(n: Int)), so you can't pass some variable or expression.

It's also possible convert an Array to some IndexedSeq (.toSeq), like Vector (.toVector)

You can also specify some set of accepatable sizes using type disjunction:

def func[A <: Nat](l: Sized[List[Int], A])(implicit ev: (_2 with _3) <:< A) = l

func(List(1,2).sized(2).get)
res17: shapeless.Sized[List[Int],shapeless.Succ[shapeless.Succ[shapeless._0]]] = shapeless.Sized@3ac1111f

scala> func(List(1,2,3).sized(3).get)
res18: shapeless.Sized[List[Int],shapeless.Succ[shapeless.Succ[shapeless.Succ[shapeless._0]]]] = shapeless.Sized@17191095

scala> func(List(1,2,3,4).sized(4).get)
<console>:24: error: Cannot prove that shapeless.nat._2 with shapeless.nat._3 <:< nat_1.N.
              func(List(1,2,3,4).sized(4).get)
                  ^

Maximum-N restriction (from @DougC and @Miles Sabin):

import ops.nat._
import LT._
scala> def func[N <: Nat](l: Sized[List[Int], N])(implicit ev: N < _3) = l
func: [N <: shapeless.Nat](l: shapeless.Sized[List[Int],N])(implicit ev: shapeless.ops.nat.LT.<[N,shapeless.nat._3])shapeless.Sized[List[Int],N]

scala> func(List(1,2).sized(2).get)
res25: shapeless.Sized[List[Int],shapeless.Succ[shapeless.Succ[shapeless._0]]] = shapeless.Sized@3ac1111f

scala> func(List(1).sized(1).get)
res26: shapeless.Sized[List[Int],shapeless.Succ[shapeless._0]] = shapeless.Sized@73f49b57

scala> func(List(1,2,3).sized(3).get)
<console>:30: error: could not find implicit value for parameter ev: shapeless.ops.nat.LT[nat_1.N,shapeless.nat._3]
              func(List(1,2,3).sized(3).get)
                  ^
Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible to use shapeless to specify a seq that has a maximum size, rather than a fixed size?
Thanks. in fact, on gitter, Miles provided this as a building block def frob[N <: Nat](l: Sized[N])(implicit ev: LT[N, _3]) ... that, I think, points to a way to do what I'm after. The compiler will generate the implicit value for ev at the call sites. I'm pretty sure I can use that to build up a a FiniteSeq[T, N :< Nat] class or similar.
LessThan, cool, i'll add it to my answer. I suppose it's in ops.nat package
Yep. Along with some other very interesting type classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.