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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:49:16+00:00 2026-05-20T15:49:16+00:00

Is there a way in scala to get the arguments back from a already

  • 0

Is there a way in scala to get the arguments back from a already partially applied function?

Does this even make sense, should be done, or fits into any use case?

example:


def doStuff(lower:Int,upper:Int,b:String)=
for(turn <- lower to upper) println(turn +": "+b)

Imagine that at one point I know the ‘lower’ argument and I get a function of applying it to ‘doStuff’


val lowerDoStuff = doStuff(3,_:Int,_:String)

Is there a way for me to get that 3 back ?
(for the sake of example, imagine that I am inside a function which only received ‘lowerDoStuff’ and now needs to know the first argument)

Idiomatic scala is prefered to introspection/reflection (if possible).

  • 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-20T15:49:16+00:00Added an answer on May 20, 2026 at 3:49 pm

    Idiomatic Scala: no, you can’t. You have specifically said that the first argument is no longer relevant. If the compiler can make it disappear entirely, that’s best: you say you have a function that depends on an int and a string, and you haven’t made any promises about what generated it. If you really need that value, but you also really need to pass a 2-argument function, you can do it by hand:

    class Function2From3[A,B,C,Z](f: (A,B,C) => Z, val _1: A) extends Function2[B,C,Z] {
      def apply(b: B, c: C) = f(_1, b, c)
    }
    val lowerDoStuff = new Function2From3(doStuff _, 3)
    

    Now when you get the function later on, you can pattern match to see if it’s a Function2From3, and then read the value:

    val f: Function2[Int,String,Unit] = lowerDoStuff
    f match {
      case g: Function2From3[_,_,_,_] => println("I know there's a "+g._1+" in there!")
      case _ => println("It's all Greek to me.")
    }
    

    (if it’s important to you that it be an integer, you can remove A as a generic parameter and make _1 be an integer–and maybe just call it lower while you’re at it).

    Reflection: no, you can’t (not in general). The compiler’s smarter than that. The generated bytecode (if we wrap your code in class FuncApp) is:

    public final void apply(int, java.lang.String);
      Signature: (ILjava/lang/String;)V
      Code:
       0:   aload_0
       1:   getfield    #18; //Field $outer:LFuncApp;
       4:   iconst_3
       5:   iload_1
       6:   aload_2
       7:   invokevirtual   #24; //Method FuncApp.doStuff:(IILjava/lang/String;)V
       10:  return
    

    Notice the iconst_3? That’s where your 3 went–it disappeared into the bytecode. There’s not even a hidden private field containing the value any more.

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

Sidebar

Related Questions

Is there a standard way of invoking the scala REPL from a gradle task?
is there way how to get name ov event from Lambda expression like with
In Scala, is there a way to get the currently evaluated items in a
is there a way in Scala to sort an array of tuples using and
Is there a good scala-esque (I guess I mean functional) way of recursively listing
Is there way in next piece of code to only get the first record?
Im using scala Map#get function, and for every accurate query it returns as Some[String]
I have started learning scala and I wonder is there a way I could
I'm a Scala beginner and this piece of code makes me struggle. Is there
Forgive me if this has already been asked elsewhere. I have a Scala syntax

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.