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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:45:28+00:00 2026-05-26T09:45:28+00:00

I am trying to implement C#’s ExpandoObject -like class in Scala. This is how

  • 0

I am trying to implement C#’s ExpandoObject-like class in Scala. This is how it is supposed to work:

val e = new ExpandoObject
e.name := "Rahul" // This inserts a new field `name` in the object.
println(e.name) // Accessing its value.

Here is what I have tried so far:

implicit def enrichAny[A](underlying: A) = new EnrichedAny(underlying)
class EnrichedAny[A](underlying: A) {
  // ...
  def dyn = new Dyn(underlying.asInstanceOf[AnyRef])
}

class Dyn(x: AnyRef) extends Dynamic {
  def applyDynamic(message: String)(args: Any*) = {
    new Dyn(x.getClass.getMethod(message, 
      args.map(_.asInstanceOf[AnyRef].getClass) : _*).
      invoke(x, args.map(_.asInstanceOf[AnyRef]) : _*))
  }
  def typed[A] = x.asInstanceOf[A]
  override def toString = "Dynamic(" + x + ")"
}

class ExpandoObject extends Dynamic {
  private val fields = mutable.Map.empty[String, Dyn]
  def applyDynamic(message: String)(args: Any*): Dynamic = {
    fields get message match {
      case Some(v) => v
      case None => new Assigner(fields, message).dyn
    }
  }
}

class Assigner(fields: mutable.Map[String, Dyn], message: String) {
  def :=(value: Any): Unit = {
    fields += (message -> value.dyn)
  }
}

When I try to compile this code, I get a StackOverflowError. Please help me get this work. 🙂 Thanks.

  • 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-26T09:45:28+00:00Added an answer on May 26, 2026 at 9:45 am

    Got it working after some playing around. The solution isn’t typesafe though (which in this case does not matter, since the point of this little utility is to work around type system. 🙂

    trait ExpandoObject extends Dynamic with mutable.Map[String, Any] {
      lazy val fields = mutable.Map.empty[String, Any]
      def -=(k: String): this.type = { fields -= k; this }
      def +=(f: (String, Any)): this.type = { fields += f; this }
      def iterator = fields.iterator
      def get(k: String): Option[Any] = fields get k
      def applyDynamic(message: String)(args: Any*): Any = {
        this.getOrElse(message, new Assigner(this, message))
      }
    }
    
    implicit def anyToassigner(a: Any): Assigner = a match {
      case x: Assigner => x
      case _ => sys.error("Not an assigner.")
    }
    
    class Assigner(ob: ExpandoObject, message: String) {
      def :=(value: Any): Unit = ob += (message -> value)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement an autocomplete based on this It looks like it's very straight
SetFocus I'm trying implement the above Se Focus code in a Class Library that
Trying to implement this gallery on my website. http://coffeescripter.com/code/ad-gallery/ It is noted in the
iam trying to implement an Searchmachine into my site. Iam using Zend_Search_Lucene for this.
Trying to implement a shell, mainly piping. I've written this test case which I
Trying to implement the new FP 10.1 Global error handler into my projects but
Im trying to implement a UnitofWork pattern using this Scott Allen tutorial My current
Im trying to implement pagination using multiple searching criteria. Supposed I Have student table.
Trying to implement, in Scala, the following Haskell function (from Learn You a Haskell...)
Trying to implement a function in my linkedlist class that will return total amount

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.