What does the ~ do in this bit of Scala?
For example:
scala> val apple = 1
apple: Int = 1
scala> ~apple
res0: Int = -2
What did that worm do to my apple?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Firstly, some meta-advice. Any time you’re wondering how the compiler expands some syntactic sugar, infers a type, or applies an implicit conversion, use
scala -Xprint:typer -e <expr>to show you what happened.Okay, a prefix
~is expanded to a regular method invocation ofunary_~.From the language specification:
That means that the prefix operators aren’t restricted to built in types, they can be used on your own types (although it’s not a good idea to go crazy with this power!)
So, what of your question? You can checkout the index of the ScalaDoc for the standard library for methods starting with
u. The nightly ScalaDoc has some recently added documentation for this method.