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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:02:31+00:00 2026-05-23T21:02:31+00:00

I came across the following code snippet on the Scala mailing list: scala> class

  • 0

I came across the following code snippet on the Scala mailing list:

scala> class DynamicImpl(x: AnyRef) extends Dynamic {
     |   def _select_(name: String): DynamicImpl = {
     |     new DynamicImpl(x.getClass.getMethod(name).invoke(x))
     |   }
     |   def _invoke_(name: String)(args: Any*) = {
     |     new DynamicImpl(x.getClass.getMethod(name, args.map(_.asInstanceOf[AnyRef].getClass) : _*).invoke(x, args.map(_.asInstanceOf[AnyRef]) : _*))
     |   }
     |   override def typed[T] = x.asInstanceOf[T]
     |   override def toString = "Dynamic(" + x.toString + ")"
     | }
defined class DynamicImpl

scala> 

scala> implicit def toDynamic(x: Any): Dynamic = new DynamicImpl(x.asInstanceOf[AnyRef])
toDynamic: (x: Any)Dynamic

scala> class Duck {
     |   def quack = "Quack!"
     | }
defined class Duck

scala> class QuackingSwan {
     |   def quack = "Swack!"
     | }
defined class QuackingSwan

scala> def makeQuack(d: Dynamic) {
     |   println(d.quack.typed[String])
     | }
dynatype: d._select_("quack")
makeQuack: (d: Dynamic)Unit

scala> makeQuack(new Duck)
Quack!

scala> makeQuack(new QuackingSwan)
Swack!

scala> val s: Dynamic = "Hello, world!"
s: Dynamic = Dynamic(Hello, world!)

scala> s.toLowerCase
dynatype: line8$object.$iw.$iw.s._select_("toLowerCase")
res2: Dynamic = Dynamic(hello, world!)

scala> s.length
dynatype: line8$object.$iw.$iw.s._select_("length")
res3: Dynamic = Dynamic(13)

scala> s.isEmpty.typed[Boolean]
dynatype: line8$object.$iw.$iw.s._select_("isEmpty")
res4: Boolean = false

What’s _select_ and _invoke_ here?

  • 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-23T21:02:32+00:00Added an answer on May 23, 2026 at 9:02 pm

    _select_ and _invoke_ behave like Ruby missing_method. When any method is called on a dynamic instance which does not implement the method, _select_ or _invoke_ are called instead. Those two methods can be implemented as you wish.

    In your example, they use reflection to call the an actual implementation of the x member. For instance:

    scala> val s: Dynamic = "Hello, world!"
    s: Dynamic = Dynamic(Hello, world!)
    
    scala> s.toLowerCase
    dynatype: line8$object.$iw.$iw.s._select_("toLowerCase")
    res2: Dynamic = Dynamic(hello, world!)
    

    The method toLowerCase is not defined in s. So the method _select_ is called instead with argument "toLowerCase". The dynamic mechanism will then call x.toLowerCase using reflection and wrap the result in a new dynamic instance.

    _select_ is called for any method without arguments, while _invoke_ is called for any method with arguments.

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

Sidebar

Related Questions

I am reading this blog and came across following code RunAgain = Class.new(Exception) def
I have a question regarding the following code snippet I came across in one
I came across the following code snippet and needed to predict the output. My
While investigating a crash, I came across the following code snippet and immediately recognized
I was studying the Twitter source code, and I came across the following snippet:
I came across the following code snippet in C++ (I am not yet on
I came across a strange phenomena upon running the following code: #include <iostream> class
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
I am learning javascript and I came across the following code snippet: var outerValue
I've been working in backbone.js and came across the following snippet of code. _(view.buttonViews).each(function(button)

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.