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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:49:08+00:00 2026-05-30T05:49:08+00:00

I have two functions with different type signatures, and I want to store them

  • 0

I have two functions with different type signatures, and I want to store them both in a Map:

val add = ((a: Int, b: Int) => a + b)
val odd = ((a: Int) => a % 2 == 0)

var funcs = Map[String, Any]("add" -> add, "odd" -> odd)

Then I want to access these functions at a later date, without having to know about their types. How would I do this? I can write:

funcs("add").asInstanceOf[(Int, Int) => Int](1, 2)

But that requires me to either

  1. know ahead of time what the type of the function is
  2. do some sort of pattern match that accounts for every possible type.

Is there some way I can find out the type of the object stored as Any and convert it to that type?

  • 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-30T05:49:10+00:00Added an answer on May 30, 2026 at 5:49 am

    Although this certainly is not a good idea, if you really have to do it, you can do it using reflection.

    Using PaulP’s Invocation utility:

    scala> val add: (Int, Int) => Int = _ + _
    add: (Int, Int) => Int = <function2>
    
    scala> val isOdd: Int => Boolean = _ % 2 != 0
    isOdd: Int => Boolean = <function1>
    
    scala> import Invocation._
    import Invocation._
    
    scala> val funcs = Map("add" -> add, "isOdd" -> isOdd)
    funcs: scala.collection.immutable.Map[java.lang.String,ScalaObject] = Map(add -> <function2>, isOdd -> <function1>)
    
    scala> funcs("add") o 'apply(3, 4)
    res18: Any = 7
    
    scala> funcs("isOdd") o 'apply(11)
    res19: Any = true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions that have different enough logic but pretty much the same
So let's say I have two different functions. One is a part of the
I am adding musicCD information to a set. I have two different functions for
Here i am using two different functions for calculating CRC16 for any type of
I have a few functions that I'm using to draw different type of text.
I have a set of functions that are templated both by an integer type
I have two overloaded member functions with the following signatures: class MyClass { void
I have two functions to submit to different sets of data to the same
I have two JS functions that preforms AJAX calls, I've wanted to test them
I have two functions that make $.getJSON calls - one looks at JSON stored

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.