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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:14:58+00:00 2026-05-24T20:14:58+00:00

Following my previous question came a quick and great answer, however it turned out

  • 0

Following my previous question came a quick and great answer, however it turned out my example didn’t match my actual production code well enough. In summary I’m in need of a new implementation of the collect method.

The second fruit world (with some pretty funky fruit trees):

class Fruit {
    var seeds:Array[Fruit] = Array()
    def naiveCollect[F <: Fruit]:Array[Fruit] = this match {
        case f:F => Array(this)
        case _ => seeds.map(_.select[F]).flatten.toArray
    }
}

class Apple extends Fruit
class Pear extends Fruit
class GrannySmith extends Apple

Does not work because of type erasure:

var tree = new Fruit { seeds = Array(
                new Apple,
                new Pear,
                new GrannySmith,
                new Pear { seeds = Array(
                    new Apple,
                    new Pear)},
                new Apple)}

scala> tree.naiveCollect[Apple]
res1: Array[Fruit] = Array($anon$2@5a4b99fa)

// wanted output: Apple, GrannySmith, Apple, Apple

EDIT, SOLUTION1:

Turns out i managed to produce something which works by using the PartialFunction as in the std lib.

class Fruit {
    ...
    def clumsyCollect[F](pf:PartialFunction[Fruit, F]):Seq[F] = 
        if (pf.isDefinedAt(this))
            List(pf(this))
        else
            seeds.flatMap(_.selectPartial[F](pf))
}

Use case:

tree.clumsyCollect { case a:Apple => a }

Any alternatives or tips on cleaning this up would still be great though!

  • 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-24T20:15:00+00:00Added an answer on May 24, 2026 at 8:15 pm

    I think what you need is scala.reflect.ClassManifest

    class Fruit {
        var seeds:Array[Fruit] = Array()
        def select[F <: Fruit](implicit cm: ClassManifest[F]): Array[F] = 
          if (cm.erasure.isInstance(this))        
            Array(this.asInstanceOf[F])
          else 
            seeds.flatMap(_.select[F])
    }
    

    With every call of select an implicit parameter containing actual class information will be passed. You can now make class checks at runtime, and also you can return Array of more specific type.

    This produces desired result:

    scala> tree.select[Apple]
    res12: Array[Apple] = Array(Apple@10fa4d, GrannySmith@a10ca8, Apple@14611ec, Apple@142b533)
    

    Alternatively, you can use context bound syntax:

    def select[F <: Fruit : ClassManifest]: Array[F] = 
      if (classManifest[F].erasure.isInstance(this))        
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from the excellent answer to my previous question: Linq Entity Framework generic
Following a previous question I posted and received an excellent answer to - Dynamically
Following on from my previous question , is it possible to make a Python
Following on from my previous question [link text][1] , I have a new problem.
Following up from my previous question , why is CShell so different from C?
Following on from a previous question, for some reason when I use the following
Following on from a previous question relating to heap usage restrictions , I'm looking
Following on from a previous question , I am creating a symbolic link on
In a comment on a previous question, someone said that the following sql statement
Related to my previous question , I have a string on the following format:

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.