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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:20:48+00:00 2026-06-11T12:20:48+00:00

Given a type declaration, I am able to resolve the type argument. scala> reflect.runtime.universe.typeOf[List[Int]]

  • 0

Given a type declaration, I am able to resolve the type argument.

scala> reflect.runtime.universe.typeOf[List[Int]] match {case x:TypeRef => x.args}
res10: List[reflect.runtime.universe.Type] = List(Int)

For a runtime value, The same method doesn’t work.

scala> reflect.runtime.currentMirror.reflect(List(42)).symbol.toType match {case x:TypeRef => x.args}
res11: List[reflect.runtime.universe.Type] = List(B)

Is there a way to overcome the type erasure for reflected values?

  • 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-11T12:20:50+00:00Added an answer on June 11, 2026 at 12:20 pm

    An example based on TypeTag knowledge gained from reading
    Scala: What is a TypeTag and how do I use it?
    posted by
    Eugene Burmako in the comments on your question:

    import scala.reflect.runtime.universe._
    
    object ScalaApplication {
      def main(args: Array[String]) {
        printType(List(42))
        printType(List("42"))
        printType(List("42", 42))
      }    
    
      def printType[T : TypeTag](t: T) {
        println(typeOf[T])
      }
    }
    

    This should give the output:

    $ scala ScalaApplication.scala 
    List[Int]
    List[String]
    List[Any]
    

    [UPDATE 1:]

    However, if you want to be aware of the type assigned to a reference of type Any you might have to opt for some sort of type aware wrapper:

    import scala.reflect.runtime.universe._
    
    object ScalaApplication {
      def main(args: Array[String]) {
        val anyWrapper = new AnyWrapper
    
        List(1,2,3).foreach { i =>
          i match {
            case 1 => anyWrapper.any = 42
            case 2 => anyWrapper.any = "a string"
            case 3 => anyWrapper.any = true
          }
          print(anyWrapper.any)
          print(" has type ")
          println(anyWrapper.typeOfAny)
        }
      }
    
      class AnyWrapper {
        private var _any: Any = null
        private var _typeOfAny: Type = null
    
        def any = _any
        def typeOfAny = _typeOfAny
        def any_=[T: TypeTag](a: T) = {
          _typeOfAny = typeOf[T]
          _any = a
        }
    
      }
    }
    

    This should give the output:

    $ scala ScalaApplication.scala 
    42 has type Int
    a string has type String
    true has type Boolean
    

    But this solution still does not cover the case where the reference type is unknown at compile time.

    [UPDATE 2:]

    If the types are explicitly cast to reference of type Any, you might have to enumerate all the possible types in a match statement in order to recover the type:

    import scala.reflect.runtime.universe._
    
    object ScalaApplication {
      def main(args: Array[String]) {
    
        List(1,2,3).foreach { i =>
          val any: Any = i match {
            case 1 => 42.asInstanceOf[Any]
            case 2 => "a string".asInstanceOf[Any]
            case 3 => true.asInstanceOf[Any]
          }
          print(any)
          print(" has type ")
          println(matchType(any))
        }
      }
    
      def matchType(any: Any) = {
        any match {
          case a: Int => typeOf[Int]
          case a: String => typeOf[String]
          case a: Boolean => typeOf[Boolean]
        }
      }
    }
    

    This should give the output:

    $ scala ScalaApplication.scala
    42 has type Int
    a string has type String
    true has type Boolean
    

    But this solution requires you to know (and list) all the possible types that you could receive in the any value.

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

Sidebar

Related Questions

Given: typedef type-declaration synonym; I can see how: typedef long unsigned int size_t; declares
1) If we want to create an instance of a given type during runtime
Given an enumerated type declaration in Delphi such as: TMyType = (Item1, Item2, Item3);
Given a function/type declaration like this: function Person(name, ... other args ...){ this.name ...
Given the following type declaration: TMyEnum = (onehundred,twohundred,threehundred); TMyEnum2 = (Aonehundred = 100 ,
For any given type i want to know its default value. In C#, there
I want to retrieve all objects (not DOM elements) of a given type created
I need to test, if an instance is exactly of a given type. But
My friend is trying to create a utility function that is given some Type
I have read the documentation that states that "given the type of the enum,

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.