Tilde in Scala found in Scalatra example code

六眼飞鱼酱① 提交于 2020-01-14 13:34:24

问题


Just ran into this sample code learning about Commands in Scalatra:

 protected def handle: Handler  = {
    case c: CreateTodoCommand => 
      add(newTodo(~c.name.value))
  }

In this particular case, what exactly is the relevance of ~ in ~c.name.value? Not sure where to find more documentation on this particular symbol.


回答1:


In Scala:

~x

translates to

x.unary_~

(this also applies to +,- and ! as explained in this post). So your example translates to:

add(newTodo(c.name.value.unary_~))

The documentation can hence be found at the type of value.




回答2:


it seems to be related to the block of code commented out in here: https://github.com/scalatra/scalatra/blob/2.2.x_2.9/core/src/main/scala/org/scalatra/package.scala

that is the only unary tilde operator if found that could be working here. the others seem to mainly be bitwise not operators

It actually seems that this might also be some import from scalaz library, tho the import statements are missing. similar uses of ~Option[_] can be found elsewhere as well...



来源:https://stackoverflow.com/questions/17156582/tilde-in-scala-found-in-scalatra-example-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!