from HList[Codec[Int] :: ... :: Codec[Int]] to Attempt[String]

瀏覽次數:37 次
跳到第一則未讀訊息

Olivier Girardot

未讀,
2015年8月7日 上午10:28:352015/8/7
收件者:Typelevel Users & Development List
Hi everyone, 
i'm using scodec and shapeless and still struggling to make a working example of decoding multiple int4.
I understood, if I'm not mistaken, that I can't create "arbitrary sized" HList of codec at runtime so I created (like tuple2... tuple22) some functions with constant-sized HList :

My first draft is  :

protected def decodeMe24(input: ByteVector): Attempt[String] = HList.fill(24)(uint4).toCodec.decode(input.bits).map(_.value.mkString("", "", ""))

So, for example with a size-2 decoder, I have a  Codec[Int] :: Codec[Int] :: HNil which with *toCodec* is supposed to transform into Codec[Int :: Int :: HNil] then I decode my payload and get a DecodeResult[HList] where I call mkString from HList...

It works but I'd like to create a more generic version where only the "non-variable part" is written multiple times and add a transformation (decodedIntDecimalToString) :

final implicit class EnrichHList[L <: HList](val self: L) {

def toResult(input: ByteVector)(implicit to: codecs.ToHListCodec[L]): Attempt[String] = {
to(self)
.decode(input.bits)
.map(_.value.toList[Int].map(decodedIntDecimalToString).mkString)
}
}

But this does not compile because the toList[Int] call does not find its implicit :

could not find implicit value for parameter toTraversableAux: shapeless.ops.hlist.ToTraversable.Aux[to.L,List,Int]

[error]         .map(_.value.toList[Int].map(decodedIntDecimalToString).mkString)


And I can't seem to find the proper definition...

Any help appreciated.


Regards, 


Olivier.

回覆所有人
回覆作者
轉寄
0 則新訊息