Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7548549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:43:36+00:00 2026-05-30T09:43:36+00:00

What does the ~ do in this bit of Scala? For example: scala> val

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:43:37+00:00Added an answer on May 30, 2026 at 9:43 am

    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.

    scala -Xprint:typer -e "val a = 2; ~a"
    
    ...
    private[this] val a: Int = 2;
    private <stable> <accessor> def a: Int = $anon.this.a;
    $anon.this.a.unary_~
    

    Okay, a prefix ~ is expanded to a regular method invocation of unary_~.

    From the language specification:

    6.12.1 Prefix Operations

    A prefix operation op e consists of a prefix operator op, which must be one of the identifiers +, -, ! or ~.
    The expression op e is equivalent to the postfix method application
    e.unary_op.

    Prefix operators are different from normal function applications in
    that their operand expression need not be atomic. For instance, the
    input sequence -sin(x) is read as -(sin(x)), whereas the function
    application negate sin(x) would be parsed as the application of the
    infix operator sin to the operands negate and (x).

    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!)

    scala> object foo { def unary_~ = "!!!" }
    defined module foo
    
    scala> ~foo
    res0: java.lang.String = !!!
    

    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.

    the bitwise negation of this value
    Example:
    ~5 == -6
    // in binary: ~00000101 ==
    //             11111010
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running this scala code on a 32-bit quad-core Core2 system: def job(i:Int,s:Int):Long =
What does this bit of code represent? I know it's some kind of if
What does the PCGPRLEN-1..28 means here?? Where does this 4 bit comes from? alt
Why does this.. DECLARE @SkyBlue Bit SET @SkyBlue = 1 IF @SkyBlue Select 'the
Does this code cause a memory leak: int main(){ int * a = new
I am reading Scala and I am wondering ... Why val capacity : Int
Does this pattern: setTimeout(function(){ // do stuff }, 0); Actually return control to the
Does this look like it should work? I'm wanting to generate directions from one
Does this smell? I have a few properties you can only set once. They
Does this seem right, the dataFilePath is on disk and contains the right data,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.