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

The Archive Base Latest Questions

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

In the code below I get the correct type of the property ( PropertyA

  • 0

In the code below I get the correct type of the property (PropertyA) when I get it straight from the hashmap.

When I proxy this call through the get method in ClassAbstract the type is PropertyAbstract[_ <: A]

Is there a way to proxy the call to the hashmap and keep the correct type?

Another question is how can I add objects to the revs array with type checking?

class A
class B extends A
class C extends A

abstract class PropertyAbstract[T] {
  val revs = new java.util.ArrayList[T]
}

class PropertyA extends PropertyAbstract[B]
class PropertyB extends PropertyAbstract[C]

abstract class ClassAbstract {
  val props: scala.collection.immutable.HashMap[String, PropertyAbstract[_ <: A]]
  def get(prop: String) = props.get(prop).get
}

class Class extends ClassAbstract {
  val props = collection.immutable.HashMap(
      "prop1" -> new PropertyA,
      "prop2" -> new PropertyB
  )
}

object Test extends App {
  val the_class = new Class
  val proxied_prop = the_class.get("prop1")
  val direct_prop =  the_class.props.get("prop1").get

  // wont compile (found: B     required: _$1 <: A)
  proxied_prop.revs.add(new B) 
  // wont compile (found: B     required: C with B)
  direct_prop.revs.add(new B)
}

The wanted result is that I could add an element of type B to prop1, but not an element of type C

  • 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-28T22:49:11+00:00Added an answer on May 28, 2026 at 10:49 pm

    It seems that what you want is essentially a typed map. It’s pretty obvious that what you are trying to do simply cannot work. When you call get, you receive a PropertyAbstract[X] for an unknown X (except in that it is a subtype of A). How can you then assume it takes Bs?

    The solution is for your PropertyAbstract to be contravariant but this means that it cannot be a mutable collection in any sensible manner (it can be mutable of course, but what you can get out would be an A).

    scala> class A; class B extends A; class C extends A
    defined class A
    defined class B
    defined class C
    
    scala> abstract class PropertyAbstract[-T] { val revs = new java.util.ArrayList[AnyRef] }
    defined class PropertyAbstract
    
    scala> class PropertyA extends PropertyAbstract[B]; class PropertyB extends PropertyAbstract[C]
    defined class PropertyA
    defined class PropertyB
    
    scala> abstract class ClassAbstract {
       | val props: Map[String, PropertyAbstract[_ <: A]]
       | def get(prop: String) = (props get prop).get
       | }
    defined class ClassAbstract
    
    scala> class Class extends ClassAbstract { val props = Map("prop1" -> new PropertyA, "prop2" -> new PropertyB) }
    defined class Class
    
    scala>  val the_class = new Class
    the_class: Class = Class@298508
    
    scala> val proxied_prop = the_class.get("prop1")
    proxied_prop: PropertyAbstract[_ <: A] = PropertyA@a269e2
    
    scala> val direct_prop =  the_class.props.get("prop1").get
    direct_prop: PropertyAbstract[C with B] = PropertyA@a269e2
    

    The following compile:

    scala> proxied_prop.revs.add(new B)
    res0: Boolean = true
    
    scala> direct_prop.revs.add(new B)
    res1: Boolean = true
    

    But of course you could put anything in there!

    Perhaps you should take a look at Miles Sabin’s shapeless for the sort of stuff you can do in terms of heterogeneously-typed collections.

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

Sidebar

Related Questions

I get the following error with the code below. expected constructor, destructor, or type
any idea how i can get the code below to produce this output? 1
I have the below code but for some reason I cannot get the correct
Which one below is correct? First code has no quotes in the $_GET array
When I try to use the code below I get a duplicate variable error
The code below is what I am trying to use in order to get
I can't get the code below to compile (see errors). Advice on correction would
Is there a way to get the code below to return null if no
I had used the below code to get the row index of the UIPickerView
I'm having some problems trying to get the code below to output the data

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.