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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:01:50+00:00 2026-06-16T18:01:50+00:00

I have been struggling with this annoying problem for a while without finding an

  • 0

I have been struggling with this annoying problem for a while without finding an elegant solution to it.

Let’s say I have such a class hierarchy :

class StatWithBounds[A](val min: A, val max: A, val currentValue: A)
class StatBetween0And20(initialValue: Int) extends StatWithBounds[Int](0, 20, initialValue)
class PositiveStatOnly(initialValue: Int) extends StatWithBounds[Int](0, Integer.MAX_VALUE, initialValue)
class UncappedPercentage(initialValue: Int) extends StatWithBounds[Int](0, Integer.MAX_VALUE, initialValue)

Copy/paste of initialValue is overly verbose. Furthermore, if I want to do something like this:

class Strength(initialValue: Int) extends StatBetween0And20(initialValue)
class Intelligence(initialValue: Int) extends StatBetween0And20(initialValue)
class Piety(initialValue: Int) extends StatBetween0And20(initialValue)

What a hell of copy/paste (and imagine I have 10 more subclass)!

Is there an elegant way to solve this cluttering problem ?

  • 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-16T18:01:52+00:00Added an answer on June 16, 2026 at 6:01 pm

    You can use traits instead if you don’t need the original class:

    trait StatWithBounds[A] {
      def min: A
      def max: A
      def currentValue: A
    }
    
    trait StatBetween0And20 extends StatWithBounds[Int] {
      def min = 0
      def max = 20
    }
    
    class Strength(val currentValue: Int) extends StatBetween0And20
    class Intelligence(val currentValue: Int) extends StatBetween0And20
    ...
    

    Alternatively, you can simply not use such long variable names!

    class Stat[A](val min: A, val max: A, val current: A)
    class Stat20(i: Int) extends Stat[Int](0, 20, i)
    class Strength(i: Int) extends Stat20(i)
    class Intelligence(i: Int) extends Stat20(i)
    ...
    

    See how much less noisy that is? Long variable names are a form of boilerplate. Sometimes you need it for clarity, but not, usually, just to pass on an argument to a constructor.

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

Sidebar

Related Questions

I have been struggling to find a solution to this problem. In my code,
I have been struggling with this problem for a while now, so any help
I've been struggling with this annoying problem. I have a Model, Editor and controller:
I have been struggling with this for a while now. given a set of
I have been struggling with this seeminly easy problem for 48 hours, and I
I have been struggling with this for a while and could really use some
I have been struggling with this for a while and just can't work it
I have been struggling with this a while now. I implemented a basic IHttpHandler
I have been struggling with this for a while now. I have a Master
I have been struggling with this problem on hostgator shared hosting server for the

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.