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

  • Home
  • SEARCH
  • 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 3242174
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:17:03+00:00 2026-05-17T18:17:03+00:00

I need to intersect types of incoming objects with a set of predefined ones.

  • 0

I need to intersect types of incoming objects with a set of predefined ones.

The raw approach is to scan an incoming collection for each predefined type:

trait Field
class Field1 extends Field
class Field2 extends Field
class Field3 extends Field
...

class FieldManager(shownFields:Iterable[Field]) {
  var hiddenFields = new ArrayBuffer[Field]
  var found = false
  for (sf <- shownFields) {
    if (sf.isInstanceOf[Field1]) {
      found = true
      break
    }
  if (!found)
    hiddenFields+=new Field1
  for (sf <- shownFields) {
    if (sf.isInstanceOf[Field2]) {
      found = true
      break
    }
  if (!found)
    hiddenFields+=new Field2
  for (sf <- shownFields) {
    if (sf.isInstanceOf[Field3]) {
      found = true
      break
    }
  if (!found)
    hiddenFields+=new Field3
  ...
}

Wow, this is so verbose for Scala! There should be a shorter way. Like function template in C++:

class FieldManager {
  vector<Field*> hiddenFields, shownFields;
  template<class T>
  void fillHiddenType() {
    FOR_EACH(Field *, field, shownFields) {
      if (dynamic_cast<T*>(field))
        return
      hiddenFields.push_back(new T)
    }
  }
  void fillHidden() {
    fillHiddenType<Field1>();
    fillHiddenType<Field2>();
    fillHiddenType<Field3>();
    ...
  }
};

In this C++ example we mention each type to be scanned for only once.

Ok, Scala is also good for its type parameters, lets try:

def fillHiddenType[T] {
  for (sf <- shownFields)
    if (sf.isInstanceOf[T])
      return
  hiddenFields+=new T  
}

But compiler don’t like creating T instance: class type required but T found. Try passing instance as argument and the next problem appears warning: abstract type T in type is unchecked since it is eliminated by erasure and isInstanceOf[] is always true! Type T is abstarcted so good, that is erased out completly even [T <: Field] doesn’t allow to compare it to types from our collection shownFields.

The question is:
How to test presence of an object of a given type in a collection in a compact fashion?

Update
The following working code was tested on Scala 2.7.7 Both answers were useful. Thank you, guys!

//Scala 2.7.7 misses toSet members
implicit def toSet[T](i:Iterable[T]): Set[T] = {
    val rv = new HashSet[T]
    rv ++= i
    rv
}

def filterOut[T](all:Iterable[T], toRemove:Set[T]) = {
    all.filter(x => ! toRemove.contains(x))
}

def filterOutByType[T <: AnyRef](all:Iterable[T], toFilter:Set[T]):Iterable[T] = {
    def toClass(x:AnyRef) = x.getClass.asInstanceOf[Class[T]]
    val allTypes = all map toClass
    val extraTypes = toSet(filterOut(allTypes, toFilter map toClass))
    all.filter(extraTypes contains toClass(_))
}
  • 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-17T18:17:03+00:00Added an answer on May 17, 2026 at 6:17 pm

    You can maintain a set of known subclasses of Field, like so:

    val allFieldClasses = Set[Class[_ <: Field]](
      classOf[Field1], 
      classOf[Field2],
      classOf[Field3],
      ...)
    

    Then creating the hidden fields is simply a matter of filtering out the shown field classes from this set and constructing instances of the remaining ones:

    def createHiddenFields(shownFields: Iterable[Field]):Set[Field] = {
      val toClass = (_:Field).getClass.asInstanceOf[Class[Field]]
      (allFieldTypes -- (shownFields map toClass)) map (_.newInstance)
    }
    

    Unfortunately, you’ll have to remember to keep allFieldClasses up-to-date when you add new subclasses of Field–the compiler won’t warn you that your set isn’t complete–but I see no way around that.

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

Sidebar

Related Questions

I need to test whether various types of database objects exist in a given
In the case of finding the line at which two planes intersect, you need
I have the need to proxy the property types of a proxy. So the
PHP is somewhat crippled since it doesn't have return types (yet). I need my
I need to detect the collisions of the same type of enemies in XNA.
I have multiple nodes, of 2 types. One type of node, will do one
When I write tests for certain types of objects, such as UI elements like
I need the most exhaustive English word list I can find for several types
I need to intercept the calls to all the method calls to an Interface.
We have some COBOL programs in our financial applications which need to interact with

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.