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

The Archive Base Latest Questions

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

I’m writing a project in Scala. This project involves a set of features and

  • 0

I’m writing a project in Scala. This project involves a set of features and a set of configurations, both extensible. By “extensible” I mean that I’m going to add new features in the hierarchy later, and they have to work with any configuration without recompilation. Here’s the feature hierarchy for the following illustration:

trait Feature {
    def apply(board: Board)
}

class Foo extends Feature {
    def apply(board: Board) {
        println(board formatted "Foo: %s")
    }
}

class Bar extends Feature {
    def apply(board: Board) {
        println(board formatted "Bar: %s")
    }
}

A configuration basically just defines a lot of parameters for a Board, including initial feature count for each Feature. There are several possible strategies to create a configuration at run-time: predefined, random, with user-provided values, etc. In theory, I want to be able to write something like this (not a valid Scala code!):

abstract class Config(val param: Int) {
    val ConfigParameter: Int
    def featureCount[T <: Feature]: Int
}

object Config {
    def makeBasic(param: Int) = new Config(param) {
        val ConfigParameter = param
        def featureCount[Foo] = 3
        def featureCount[Bar] = 7
    }
    def makeRandom(param: Int) = new Config(param) { ... }
    def makeWithUserValues(param: Int, ...) = new Config(param) { ... }
    def makeByStandardISO1234567(param: Int) = new Config(param) { ... }
}

class Board(val config: Config) { ... }

Obviously, it doesn’t compile. My question is: what’s the best way to represent this extensible system in Scala? I can always include something like Map[Class, Int] in the Config, but it isn’t type-safe: a programmer can insert classes in such Map that aren’t Features. So, is there a way in the Scala type system to represent something like Map[Class[T <: Feature], Int] where different keys in the Map could be of different Feature subtypes? Alternatively, maybe there’s some way to move all this behavior to the Feature hierarchy?

Thank you.

  • 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-16T12:30:02+00:00Added an answer on June 16, 2026 at 12:30 pm

    You can use ClassManifest (ClassTag in Scala 2.10) to improve your map solution:

    package object features {
      type FeatureMap = Map[Class[_ <: Feature], Int]
    }
    
    abstract class Config(val param: Int) {
        def ConfigParameter: Int
        def featureMap: FeatureMap
        def featureCount[T<:Feature]( implicit man: ClassManifest[T] ): Int = 
          featureMap( man.erasure )
    }
    
    object Config {
        def makeBasic(param: Int) = new Config(param) {
            val ConfigParameter = param
            lazy val featureMap: FeatureMap = Map(
                classOf[Foo] -> 3,
                classOf[Bar] -> 7
            )
        }
    }
    

    Each time you call featureCount the compiler will use the right classManifest for the type you passed between brackets. The erasure method returns the coresponding class.

    Remark: Avoid abstract vals, it has annoying effects and can break binary compatibility.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I am writing an app with both english and french support. The app requests
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.