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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:16:24+00:00 2026-06-08T04:16:24+00:00

I am trying to understand how scoping works in the REPL. I tried following

  • 0

I am trying to understand how scoping works in the REPL. I tried following Section 5.1.1 of Joshua Suereth’s book Scala in depth.This is on Windows XP, Java 7 and Scala 2.9.1. I declare a class Dinner in the REPL. The binding Dinner exists in the local scope. Then I instantiate because it is locally bound.

scala> class Dinner {
 | val veggie="broccoli"
 | def announceDinner(veggie: String){
 | println("Dinner happens to be tasteless " + veggie + " soup")
 | }
 | }
defined class Dinner

scala> new Dinner
res1: Dinner = Dinner@27fb77

So far so good. The name Dinner was bound locally and we could also construct a val x that could hold a reference to new Dinner.

From what I know so far, The REPL will wrap the above code in objects internally. Okay, my knowledge of Scala is not quite as deep yet and I am trying to understand how Class might be internally wrapped by the REPL.

Is there an REPL command that can help me evaluate these objects?

  • 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-08T04:16:25+00:00Added an answer on June 8, 2026 at 4:16 am

    Here goes a very quick and dirty way to what is going on in the REPL.

    Invoke the REPL with scala -Xprint:typer

    scala> class Dinner {
         |   val veggie="broccoli"
         |   def announceDinner(veggie: String){
         |   println("Dinner happens to be tasteless " + veggie + " soup")
         |   }
         |  }
    [[syntax trees at end of typer]]// Scala source: <console>
    package $line1 {
      final object $read extends java.lang.Object with ScalaObject {
        def this(): object $line1.$read = {
          $read.super.this();
          ()
        };
        final object $iw extends java.lang.Object with ScalaObject {
          def this(): object $line1.$read.$iw = {
            $iw.super.this();
            ()
          };
          final object $iw extends java.lang.Object with ScalaObject {
            def this(): object $line1.$read.$iw.$iw = {
              $iw.super.this();
              ()
            };
            class Dinner extends java.lang.Object with ScalaObject {
              def this(): $line1.$read.$iw.$iw.Dinner = {
                Dinner.super.this();
                ()
              };
              private[this] val veggie: java.lang.String = "broccoli";
              <stable> <accessor> def veggie: java.lang.String = Dinner.this.veggie;
              def announceDinner(veggie: String): Unit = scala.this.Predef.println("Dinner happens to be tasteless ".+(veggie).+(" soup"))
            }
          }
        }
      }
    }
    
    [[syntax trees at end of typer]]// Scala source: <console>
    package $line1 {
      final object $eval extends java.lang.Object with ScalaObject {
        def this(): object $line1.$eval = {
          $eval.super.this();
          ()
        };
        private[this] val $print: String = {
          $read.$iw.$iw;
          "defined class Dinner\012"
        };
        <stable> <accessor> def $print: String = $eval.this.$print
      }
    }
    
    defined class Dinner
    

    As you can check above Dinner ends up wrapped into $line1.$read.$iw.$iw. Now let’s see what happens next:

    [[syntax trees at end of typer]]// Scala source: <console>
    package $line2 {
      final object $read extends java.lang.Object with ScalaObject {
        def this(): object $line2.$read = {
          $read.super.this();
          ()
        };
        final object $iw extends java.lang.Object with ScalaObject {
          def this(): object $line2.$read.$iw = {
            $iw.super.this();
            ()
          };
          import $line1.$read.$iw.$iw.Dinner;
          final object $iw extends java.lang.Object with ScalaObject {
            def this(): object $line2.$read.$iw.$iw = {
              $iw.super.this();
              ()
            };
            private[this] val res0: $line1.$read.$iw.$iw.Dinner = new $line1.$read.$iw.$iw.Dinner();
            <stable> <accessor> def res0: $line1.$read.$iw.$iw.Dinner = $iw.this.res0
          }
        }
      }
    }
    
    [[syntax trees at end of typer]]// Scala source: <console>
    package $line2 {
      final object $eval extends java.lang.Object with ScalaObject {
        def this(): object $line2.$eval = {
          $eval.super.this();
          ()
        };
        lazy private[this] var $result: $line1.$read.$iw.$iw.Dinner = {
          $eval.this.$print;
          $line2.$read.$iw.$iw.res0
        };
        private[this] val $print: String = {
          $read.$iw.$iw;
          "res0: $line1.$read.$iw.$iw.Dinner = ".+(scala.runtime.ScalaRunTime.replStringOf($line2.$read.$iw.$iw.res0, 1000))
        };
        <stable> <accessor> def $print: String = $eval.this.$print
      }
    }
    

    Basically the same thing as before but using $line2 instead of $line1. Notice the import $line1.$read.$iw.$iw.Dinner right before $line2.$read.$iw.$iw.

    This way we can see why defining companion objects in two different lines doesn’t work, they end up wrapped into different objects and companions need to be defined in the same scope/source file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having terrible trouble trying to understand python scoping rules. With the following script:
Trying to understand why this doesn't work. I keep getting the following errors: left
Trying to understand how CoffeeScript instance and class variable works I came with this
I am trying to understand how the following code is able to do this:
Trying to understand PNG format. Consider this PNG Image: The Image is taken from
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
Im trying to understand how class generics work and this bit just doesnt make
Trying to understand what Sql Profiler means by emitting sp_reset_connection. I have the following,
Trying to understand resources in java-land. I believe the following is true: Resources loaded

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.