scala - How can I match case with shapeless variable? -
how can match case shapeless variable ?
let's have variable of following type shapeless.::[string,shapeless.::[string,shapeless.hnil]]
currently have this
authheaders.hrequire(shape_value => { val (client_id, client_secret) = value.tupled isauthorized(client_id, client_secret) } ) can somehow unwind string :: string :: hnil string pair don't have in separate statement ?
there method unapply in object shapeless.:::
def unapply[h, t <: hlist](x: h :: t): option[(h, t)] so match on hlist this:
scala> val ::(a, ::(b, hnil)) = "1" :: "x" :: hnil a: string = 1 b: string = x or alternative syntax unapply method tuple2 result: a :: b instead of ::(a, b):
scala> val :: b :: hnil = "1" :: "x" :: hnil a: string = 1 b: string = x scala> "1" :: "x" :: hnil match { | case :: b :: hnil => s"$a :: $b :: hnil" | } res0: string = 1 :: x :: hnil in case:
authheaders.hrequire{ case client_id :: client_secret :: hnil => isauthorized(client_id, client_secret) } alternative
you use tupled method convert function of n arguments function of single tuplen argument.
for function:
val isauthorized: (string, string) => boolean = ??? authheaders.hrequire{ isauthorized tupled _.tupled } for method:
def isauthorized(s1: string, s2: string): boolean = ??? authheaders.hrequire{ (isauthorized _) tupled _.tupled }
Comments
Post a Comment