scala隐式提取模式匹配中的值?
发布时间:2020-12-16 18:30:19 所属栏目:安全 来源:网络整理
导读:我们经常需要传递代码上下文信息,例如执行操作的用户.我们将此上下文用于授权检查等各种事务.在这些情况下,隐含值可以证明对减少锅炉板代码非常有用. 假设我们传递了一个简单的执行上下文:case class EC(initiatingUser:User) 我们可以有方便的守卫: def
我们经常需要传递代码上下文信息,例如执行操作的用户.我们将此上下文用于授权检查等各种事务.在这些情况下,隐含值可以证明对减少锅炉板代码非常有用.
假设我们传递了一个简单的执行上下文:case class EC(initiatingUser:User) 我们可以有方便的守卫: def onlyAdmins(f: => T)(implicit context:EC) = context match{ case EC(u) if(u.roles.contain(Role.ADMIN)) => f case _ => throw new UnauthorizedException("Only admins can perform this action") } val result = onlyAdmins{ //do something adminy } 我最近发现自己需要在与Akka演员合作时这样做,但他们使用模式匹配,我还没有找到一个很好的方法来使implicits与提取器一起工作. 首先,您需要使用每个命令传递上下文,但这很简单: case class DeleteCommand(entityId:Long)(implicit executionContext:EC) //note that you need to overwrite unapply to extract that context 但是接收函数看起来像这样: class MyActor extends Actor{ def receive = { case DeleteCommand(entityId,context) => { implicit val c = context sender ! onlyAdmins{ //do something adminy that also uses context } } } } 如果提取的变量可以标记为隐式但是我没有看到这个特性会更简单: def receive = { case DeleteCommand(entityId,implicit context) => sender ! onlyAdmins{ //do something adminy (that also uses context) } } 您是否了解任何其他编码方式,以减少样板代码? 解决方法
我认为你正在为case类添加多个param集和implicits,并且还必须添加一个新的unapply这一事实可能表明你将走上一条不那么好的道路.虽然这些类型的东西是可能的,但它们可能不是一个好主意,也许像案例类的多个参数集(和暗示)可能会在某一天消失.我用更标准的东西重写了你的例子.我不是说这是一个完美的解决方案,但它更符合标准路径:
trait ContextCommand{ def context:EC } case class DeleteCommand(entityId:Long,context:EC) extends ContextCommand def onlyAdmins[T](cmd:ContextCommand)(f: => T) = cmd.context match { case EC(u) if(u.roles.contain(Role.ADMIN)) => f case _ => throw new UnauthorizedException("Only admins can perform this action") } class MyActor extends Actor{ def receive = { case cmd @ DeleteCommand(entityId,ctx) => { sender ! onlyAdmins(cmd){ //do something adminy that also uses context //Note,ctx available here via closure } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |