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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:12:33+00:00 2026-06-12T01:12:33+00:00

say your given an instance of List[Class[_ <: Base]] , and say that many

  • 0

say your given an instance of List[Class[_ <: Base]], and say that many classes extends Base:

class A extends Base
class B extends Base
class C extends A

etc’…
now, the given list may contain only some classes. e.g. : val classes = classOf[A] :: Nil, well, how can i test when i get an instantiated val if it’s class is of type that is found in the list, or if it a subclass for a class in the list? i.e. how would you implement:
def testClass(class : List[Class[_ <: Base]], instance : Base) : Boolean
when:

val classes = classOf[A] :: Nil
testClass(classes, new A) // should return true
testClass(classes, new B) // should return false
testClass(classes, new C) // should return true

use case:
i’m trying to write a generic retry pattern, i.e. getting some code that is very error prone, and i want to retry executing it for some number of maximum tries, when every time it fails, it should execute some “wait” method.
e.g.

retryRequest({
    //Some code that throws exceptions
}, classOf[SomeException] :: classOf[SomeOtherException] :: Nil,
100, {Thread.sleep(5000)})

well this works OK, but it won’t test for subclasses of a given exception:

def retryRequest(req : => Unit, validExceptions : List[Class[_ <: java.lang.Throwable]], tries : Int, waitMethod : => Unit) {
    var keepTrying = false
    var tryCount = 0
    do{
        try{
            logger.debug("retryRequest, try #" + tryCount)
            keepTrying = false
            req
        }catch{
            case ex if(tryCount >= tries && validExceptions.contains(ex.getClass)) => {
                throw new MaxTriesReachedException("tried for " + tryCount + "times, but no luck. " +
                                                   "you may want to try ommitting generic exceptions types from the given list.") 
            }
            case ex if (validExceptions.contains(ex.getClass)) => {
                logger.debug("intercepted " + ex.toString)
                tryCount += 1
                keepTrying = true
                waitMethod
            }
        }
    }while(keepTrying)
}

i would realy want to replace:

validExceptions.contains(ex.getClass)

with something like:

validExceptions.exists(exClass => ex.isInstanceOf[exClass]) //won't compile

is it possible? how?

  • 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-12T01:12:34+00:00Added an answer on June 12, 2026 at 1:12 am

    I think this should work:

    validExceptions.exists(exClass => exClass.isAssignableFrom(ex.getClass))
    

    http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#isAssignableFrom%28java.lang.Class%29

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

Sidebar

Related Questions

Let's say your creating an application that needs to send you an email monthly.
Section 7.1.6 of the Modeshape docs say 'Your application can now create and remove
let's say that you in your new programming work your boss comes and says
The answers here all say that pushViewController retains the given viewController but they don't
Given a list of instances, say clients I'm trying to pluck an item from
Say, for example, you are caching data within your ASP.NET web app that isn't
Say your .ssh directory contains 30 keys (15 private and 15 public). Where in
for example lets say your pulling data from somewhere and you put it in
Say a user registers for your site, you hash the password they have chosen
Say you have a one to one relationship in your entity model. The code

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.