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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:56:03+00:00 2026-06-13T23:56:03+00:00

I add variables with Dynamic from scala 2.10.0-RC1 like this: import language.dynamics import scala.collection.mutable.HashMap

  • 0

I add variables with Dynamic from scala 2.10.0-RC1 like this:

import language.dynamics
import scala.collection.mutable.HashMap

object Main extends Dynamic {
  private val map = new HashMap[String, Any]
  def selectDynamic(name: String): Any = {return map(name)}
  def updateDynamic(name:String)(value: Any) = {map(name) = value}
}

val fig = new Figure(...)  // has a method number

Main.figname = fig

Now, if I want to access Main.figname.number it doesn’t work, because the compiler thinks it’s of type Any.

But it’s also Main.figname.isInstanceOf[Figure] == true, so it’s Any and Figure, but doesn’t have Figures abilities. Now I can cast it like, Main.figname.asInstanceOf[Figure].number and it works! This is ugly! And I can’t present this to my domain users (I’d like to build a internal DSL.)

Note: If I use instead of Any the supertype of Figure it doesn’t work either.

Is this a bug in scala 2.10, or a feature?

  • 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-13T23:56:05+00:00Added an answer on June 13, 2026 at 11:56 pm

    It is quite logical. You are explicitly returning instances of Any. A workaround would be to have instances of Dynamic all along:

    import language.dynamics
    import scala.collection.mutable.HashMap
    import scala.reflect.ClassTag
    
    trait DynamicBase extends Dynamic {
      def as[T:ClassTag]: T
      def selectDynamic[T](name: String): DynamicBase
      def updateDynamic(name:String)(value: Any)
    }
    
    class ReflectionDynamic( val self: Any ) extends DynamicBase with Proxy {
      def as[T:ClassTag]: T = { implicitly[ClassTag[T]].runtimeClass.asInstanceOf[Class[T]].cast( self ) }
       // TODO: cache method lookup for faster access + handle NoSuchMethodError
      def selectDynamic[T](name: String): DynamicBase = {
        val ref = self.asInstanceOf[AnyRef]
        val clazz = ref.getClass
        clazz.getMethod(name).invoke( ref ) match {
          case dyn: DynamicBase => dyn
          case res => new ReflectionDynamic( res )
        }
      }
      def updateDynamic( name: String )( value: Any ) = {
        val ref = self.asInstanceOf[AnyRef]
        val clazz = ref.getClass
        // FIXME: check parameter type, and handle overloads
        clazz.getMethods.find(_.getName == name+"_=").foreach{ meth =>
          meth.invoke( ref, value.asInstanceOf[AnyRef] )
        }
      }
    }
    
    object Main extends DynamicBase {  
      def as[T:ClassTag]: T = { implicitly[ClassTag[T]].runtimeClass.asInstanceOf[Class[T]].cast( this ) }
      private val map = new HashMap[String, DynamicBase]
      def selectDynamic[T](name: String): DynamicBase = { map(name) }
      def updateDynamic(name:String)(value: Any) = {
        val dyn = value match {
          case dyn: DynamicBase => dyn
          case _ => new ReflectionDynamic( value )
        }
        map(name) = dyn
      }
    }
    

    Usage:

    scala>     class Figure {
         |       val bla: String = "BLA"
         |     }
    defined class Figure
    scala> val fig = new Figure()  // has a method number
    fig: Figure = Figure@6d1fa2
    scala> Main.figname = fig
    Main.figname: DynamicBase = Figure@6d1fa2
    scala> Main.figname.bla
    res40: DynamicBase = BLA
    

    All instances are wrapped in a Dynamic instance.
    We can recover the actual type using the as method which performs a dynamic cast.

    scala> val myString: String = Main.figname.bla.as[String]
    myString: String = BLA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$_SESSION[{'\'testingrecord'.$testingrecordsfec.'\''}] this originally looked like this ${'testingrecord'.$testingrecordsfec} but I need to add the variables
I am trying to add 2 member variables of Xtemplate. I tried something like
I have written a function for deleting object preiodically. function add(variable, expireSecond){ this.list[variable] =
I'd like to create a dynamic Multidimensional ArrayList that reads from a text file
i want to add a dynamic configuration path (generated from pkg-config) to my project.
So I am trying extend the base Application class and add member variables to
Where Linux/Unix environment variables are kept? How can I add my own environment variable
I have two variables, key and value , and I want to add them
I have the following code which creates a dynamic object that is assigned to
I am building a dynamic form creator that allows users to add form elements

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.