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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:08:59+00:00 2026-05-24T14:08:59+00:00

I’m practicing my Scala, namely traits, and I’m a bit stuck. I’ve decided to

  • 0

I’m practicing my Scala, namely traits, and I’m a bit stuck. I’ve decided to map out the X-men species in Classes and Traits.

I’m stuck on the Power properties. I want a singleton for a List of Power

What I want is basically:

var Rogue = new HomosapienSuperior()
Rogue.addPower("Super Human Strength")

Using addPower will check against the singleton list of Power to see if the power is added, if it isn’t added then throw an exception and tell user to add it to the singleton list of Power first.

object Power {
  val powers = new HashMap[String, String] // power -> description, list of Power!
  val secondaryPowers = new HashMap[String, String]  // "Fly" -> "Can fly"
}

import Power._
import scala.collection.mutable.HashMap

class Power(power: String) {
 //How do I refer to the object/singleton Power list?
}

abstract class Homosapien {
  def name:HashMap[String, String]
  def sex: Sex
}

class HomosapienSuperior(
  val name:HashMap[String, String],
  val sex:Sex,
  val power: Power
) extends Homosapien

So I’m thinking of doing:

class Power(power: String) {
  var power = List[String]
  def addPower(power: String): List = {
    //check the object Power.powers(power)
    // if not in the object Power throw exception else
    // new list (power) prepend this.power 
  }
 //How do I refer to the object/singleton Power list?
}

But I don’t know how to refer to the variable in object Power.

My Code so far:

object Sex extends Enumeration {
  type Sex = Value
  val male = Value("male")
  val female = Value("female")
  val other = Value("other")
}

object Power {
  val powers = new HashMap[String, String] // power -> description
  val secondaryPowers = new HashMap[String, String] 
}

import Sex._
import Power._
import scala.collection.mutable.HashMap

class Power(power: String) {

}

abstract class Homosapien {
  def name:HashMap[String, String]
  def sex: Sex
}

class HomosapienSuperior(
  val name:HashMap[String, String],
  val sex:Sex
  /*
  val power:HashMap[String, String],
  val secondaryPower:HashMap[String, String] */
) extends Homosapien with power with secondaryPower

class HomosapienSuperiorSuperior(
  val name:HashMap[String, String],
  val sex:Sex
  //val power:HashMap[String, String]
) extends Homosapien with power

trait power {
  val power = List[Power] //power name and description
}

trait secondaryPower {
  val secondaryPower = List[Power] //power name and description
}

Note: I haven’t gotten up to the chapters on case classes yet, reading the Programming in Scala 2ed Book from Altima.

Thank you for your time.

  • 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-24T14:09:01+00:00Added an answer on May 24, 2026 at 2:09 pm

    To access an object via Class:

    Power.powers
    

    Answers provided by agilesteel

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an XML file, the creators of it stuck in a bunch social
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.