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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:27:34+00:00 2026-06-18T04:27:34+00:00

I’m having trouble digging through Scala’s (somewhat sparse?) documentation on the new 2.10 release.

  • 0

I’m having trouble digging through Scala’s (somewhat sparse?) documentation on the new 2.10 release. I have a situation where I am reading data recursively from a source without type information. On the time of reading I know the expected type, so I can check whether that type aligns with the incoming data.

My problem is: When attempting to retrieve a collection object with type parameters (for instance an Array[Int]), how can I use the expected type to ensure that the read values are of the correct type?

So far I’ve fiddled around with code provided by the Scala Api that allows you to extract type-parameters. I also read about the Class Tags and how they can be used to create Arrays. So my next thought was to 1) find the type parameter, 2) create an array from that type and 3) see if the read data fits without exceptions, like so:

val paramType = paramInfo[Array[X]]  // Step 1: Find the X type parameter
val array     = Array[paramType](size) // Step 2: Can't use TypeTags#Type as a normal Java type...
// Step 3: Feed data into the array and hope for the best
// Step 4: Profit!

Since the above paramType gives me the Type, it should be a simple matter of converting that type to a class tag. But the answer illudes me.

To be honest this solution seems a bit messy to me, but I haven’t been able to figure out anything smarter. If there are alternative solutions I’m all ears!

Thanks in advance.

Edit: To clarify, my above example should demonstrate that I want to extract type X from Array[Int] (for example) and then make an instance of an array containing that particular type. Hope that made it clearer.

Edit: Perhaps further clarification is in order (did I really make it that unclear? 🙂 ). I want to read a collection from a data-source. And I want that collection to be typed with the correct, expected, type. So let’s say I call the method readData. Since I know what type to expect I give it a type parameter of that expected type. As an example, let’s say Array[Int]. It could be Array[String] or Iterable[Any] or simply Null or whatever. When the readData method is called, I would like to match the given, expected type (Array[Int]) to the type of the data read from the external source. If the found type is the same type – or a subtype – of the expected type we, can cast and return the data. If not, an exception is thrown, informing the user that the found data wasn’t of the expected type. So to sum it up: How do I make a call to readData[Array[Int]] work?

Edit: I solved the issue by creating an Array[Any], retrieve the expected type (X above) and iterate the array to see if the elements are of the same type (or subtype) of X. If they are, we can safely cast to Array[X]. In the example below the expected type is represented by E. It’s pretty hackish, I know, but again: I’d love to see alternatives…

// Matches if the type of o corresponds (<:<) to the type of x
def verifyType(o : Any, x : Type) : Boolean = { ... } 

// Get the type of the array parameter (X)
val tpe = reflect.runtime.universe.typeOf[E] tpe match {  
  // The returned Type is not an instance of universe.Type - hence the cast              
  case TypeRef(_, _, args) => args.asInstanceOf[List[Type]] 
  case _ => throw new IllegalArgumentException("Could not find type parameters in type " + tpe)
} 

// Iterate the array and make sure the types match
val hasWrongType = array.exists(e => !verifyType(e, tpe))

if (hasWrongType) throw new Exception(...) // The types does not fit
else array.asInstanceOf[E] // We can safely cast
  • 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-18T04:27:35+00:00Added an answer on June 18, 2026 at 4:27 am

    You don’t really need anything new in Scala 2.10 to do this, though instead of earlier versions’ ClassManifest you use the replacement, ClassTag:

    def makeArray[T : reflect.ClassTag](length: Int): Array[T] = {
      val tTag = implicitly[reflect.ClassTag[T]]
      tTag.newArray(length)
    }
    

    In the REPL:

    scala> makeArray[String](5)
    res0: Array[String] = Array(null, null, null, null, null)
    

    With primitive types:

    scala> makeArray[Int](5)
    res1: Array[Int] = Array(0, 0, 0, 0, 0)
    

    Edit: Take two:

    def makeArrayLike(a: Array[_], length: Int): Array[_] = {
      val cA = a.getClass
      val cC = cA.getComponentType
      java.lang.reflect.Array.newInstance(cC, length).asInstanceOf[Array[_]]
    }
    

    In the REPL:

    scala> val ai1 = Array[Int](1, 2, 3)
    ai1: Array[Int] = Array(1, 2, 3)
    
    scala> val as1 = Array[String]("one", "two", "three")
    as1: Array[String] = Array(one, two, three)
    
    scala> makeArrayLike(as1, 5)
    res0: Array[_] = Array(null, null, null, null, null)
    
    scala> makeArrayLike(ai1, 5)
    res1: Array[_] = Array(0, 0, 0, 0, 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.