unapply
对象中有方法shapeless.::
:
def unapply[H, T <: HList](x: H :: T): Option[(H, T)]
所以你可以像这样匹配HList
:
scala> val ::(a, ::(b, HNil)) = "1" :: "x" :: HNil
a: String = 1
b: String = x
或者使用带有结果 的unapply
方法的替代语法:而不是:Tuple2
a :: b
::(a, b)
scala> val a :: b :: HNil = "1" :: "x" :: HNil
a: String = 1
b: String = x
scala> "1" :: "x" :: HNil match {
| case a :: b :: HNil => s"$a :: $b :: HNil"
| }
res0: String = 1 :: x :: HNil
在你的情况下:
authHeaders.hrequire{
case client_id :: client_secret :: HNil => isAuthorized(client_id, client_secret)
}
选择
您可以使用tupled
方法将 N 个参数的函数转换为单个TupleN
参数的函数。
对于功能:
val isAuthorized: (String, String) => Boolean = ???
authHeaders.hrequire{ isAuthorized tupled _.tupled }
对于方法:
def isAuthorized(s1: String, s2: String): Boolean = ???
authHeaders.hrequire{ (isAuthorized _) tupled _.tupled }