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 8908785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:16:18+00:00 2026-06-15T03:16:18+00:00

What’s the exact deal with scope regarding variables in Scala? When I open curly

  • 0

What’s the exact deal with scope regarding variables in Scala?

When I open curly brackets I can still access the value of the outer vars (and modify them if vars):

scala> var mmm = 4
mmm: Int = 4

scala> {
     | println(mmm)
     | mmm += 2
     | println(mmm)
     | }
4
6

scala> println(mmm)
6

But Odersky says on page 180 or his book that

In a Scala program, an inner variable is said to shadow a like-named outer variable,
because the outer variable becomes invisible in the inner scope.

This seems even weirder:

scala> val a = 4
a: Int = 4

scala> {
     | println(a)
     | }
4

So do I get a copy of it created in the inner scope?

scala> val a = 4
a: Int = 4

scala> {
     | val a = 8
     | }

Why can I say val again if its immutable?

scala> val a = 4
a: Int = 4

scala> {
     | println(a)
     | val a = 8
     | println(a)
     | }

But for this one I get an error:

error: forward reference extends over definition of value a
              println(a)
  • 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-06-15T03:16:19+00:00Added an answer on June 15, 2026 at 3:16 am

    If you create a new one in the block, the new one shadows the old (outer) one. If you don’t, you refer to the outer one.

    It doesn’t matter where in the block the new one is created; if it appears anywhere, it shadows the outer one.

    So,

    val a = 5
    { println(a) }  // This is outer a
    
    val a = 5
    { val a = 8; println(a) }  // This is inner a
    
    val a = 5
    { println(a); val a = 8 }  // This is broken
                               // you try to print the inner a
                               // but it doesn't exist yet
    

    Edit: let’s unpack the last one

    val a = 5
    // Okay, I have something called a
    
    {  // Oh, new block beginning!  Maybe there are local variables
      println(a)
      val a = 8    // Yeah, there's one!
                   // And it's got the same name as the outer one
                   // Oh well, who needs the outer one anyway?
    }
    
    // Waitaminute, what was that block supposed to do?
    {
      println(a)   // Inner block has an a, so this must be the inner a
      val a = 8    // Which is 8
    }
    
    // Hang on, operations happen in order
    {
      println(a)   // What inner a?!
    
    // Abort, abort, abort!!!!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has

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.