3

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.

randombits
  • 47,058
  • 76
  • 251
  • 433

2 Answers2

7

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.

Community
  • 1
  • 1
gzm0
  • 14,752
  • 1
  • 36
  • 64
  • I have the same problem even with the knowledge that ~x would be x.unary_~. As in this case: f: Field[T] and f.value : Option[T] and as far as i see there is no unary operator ~on vanilla Options, so i was wondering if there was a missing implicit conversion at work in here. – Verneri Åberg Feb 07 '14 at 13:32
0

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...

Verneri Åberg
  • 390
  • 1
  • 9