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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:46:13+00:00 2026-05-14T07:46:13+00:00

Can I retrieve a Method via reflection, somehow combine it with a target object,

  • 0

Can I retrieve a Method via reflection, somehow combine it with a target object, and return it as something that looks like a function in Scala (i.e. you can call it using parenthesis)? The argument list is variable. It doesn’t have to be a “first-class” function (I’ve updated the question), just a syntactic-looking function call, e.g. f(args).

My attempt so far looks something like this (which technically is pseudo-code, just to avoid cluttering up the post with additional definitions):

class method_ref(o: AnyRef, m: java.lang.reflect.Method) {
    def apply(args: Any*): some_return_type = {
        var oa: Array[Object] = args.toArray.map { _.asInstanceOf[Object] }
        println("calling: " + m.toString + " with: " + oa.length)

        m.invoke(o, oa: _*) match  {
            case x: some_return_type => x;
            case u => throw new Exception("unknown result" + u);
        }
    }
}

With the above I was able to get past the compiler errors, but now I have a run-time exception:

Caused by: java.lang.IllegalArgumentException: argument type mismatch

The example usage is something like:

  var f = ... some expression returning method_ref ...;
  ...
  var y = f(x) // looks like a function, doesn't it?

UPDATE

Changing the args:Any* to args:AnyRef* actually fixed my run-time problem, so the above approach (with the fix) works fine for what I was trying to accomplish. I think I ran into a more general issue with varargs 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-14T07:46:13+00:00Added an answer on May 14, 2026 at 7:46 am

    If you’re not looking for a generic invoke that takes the method name–but rather, you want to capture a particular method on a particular object–and you don’t want to get too deeply into manifests and such, I think the following is a decent solution:

    class MethodFunc[T <: AnyRef](o: Object, m: reflect.Method, tc: Class[T]) {
      def apply(oa: Any*): T = {
        val result = m.invoke(o, oa.map(_.asInstanceOf[AnyRef]): _*)
        if (result.getClass == tc) result.asInstanceOf[T]
        else throw new IllegalArgumentException("Unexpected result " + result)
      }
    }
    

    Let’s see it in action:

    val s = "Hi there, friend"
    val m = s.getClass.getMethods.find(m => {
      m.getName == "substring" && m.getParameterTypes.length == 2
    }).get
    val mf = new MethodFunc(s,m,classOf[String])
    
    scala> mf(3,8)
    res10: String = there
    

    The tricky part is getting the correct type for the return value. Here it’s left up to you to supply it. For example,if you supply classOf[CharSequence] it will fail because it’s not the right class. (Manifests are better for this, but you did ask for simple…though I think “simple to use” is generally better than “simple to code the functionality”.)

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

Sidebar

Ask A Question

Stats

  • Questions 400k
  • Answers 400k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no built-in timeout for this. What I do… May 15, 2026 at 4:21 am
  • Editorial Team
    Editorial Team added an answer You'll need to hook the keypress event and look for… May 15, 2026 at 4:20 am
  • Editorial Team
    Editorial Team added an answer An AddIn targeting 2007 should also work in Office 2010,… May 15, 2026 at 4:20 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.