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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:46:49+00:00 2026-05-28T02:46:49+00:00

The following code is taken from Programming in Scala book by Martin Odersky et

  • 0

The following code is taken from Programming in Scala book by Martin Odersky et al. which defines a rational type:

class Rational(n: Int, d: Int) { 
  require(d != 0)
  private val g = gcd(n.abs, d.abs)
  val numer = n / g 
  val denom = d / g
  ...
  private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
}

Here the value g is only used when the implicit constructor initializes fields numer and denom. Suppose programmer knows that it wont be used anywhere else. In the above case it is still accessible after construction of the Rational object. Which means it will also occupy space since is a private field rather than being a local variable to the constructor.

My question is how do I change this code so that g is only used while construction and then thrown away?

  • 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-28T02:46:50+00:00Added an answer on May 28, 2026 at 2:46 am

    In this case, how about something like this?

    class Rational(n: Int, d: Int) {
      require(d != 0)
      val (numer, denom) = {
        val g = gcd(n.abs, d.abs)
        (n / g, d / g)
      }
      private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
    }
    

    EDIT: This also creates an extra field that holds a tuple, as shown by running javap on the compiled class (thanks, Alexey):

    public class Rational extends java.lang.Object implements scala.ScalaObject{
        private final scala.Tuple2 x$1; // actually unwanted!
        private final int numer;
        private final int denom;
        public int numer();
        public int denom();
        private int gcd(int, int);
        public Rational(int, int);
    }
    

    In other cases, I sometimes use the locally block to avoid turning every val into a field:

    class A {
      locally {
        val value1 = // ...
        val value2 = // ...
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is taken from here . I removed all Windows NT part
I have the following piece of code taken from the PHP manual on the
Is this possible to do the following in ExpressionEngine: (code taken from here )
I've used the following code, taken from here , to create my own custom
How should I understand $(select option:selected) in the following code ? (taken from here
Hey all, the following is a snippet of code taken from the unix ptx
i have the following code, taken partly from a Red Black Tree Java implementation.
I am executing the following code: def takeN(s : String, n : Int): String
In a beginner's programming book (free licence) there was the following code, dynamically creating
The following is an excerpt from a code sample in K&R's The C Programming

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.