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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:19:08+00:00 2026-05-31T13:19:08+00:00

Related: How do you assign a function to a value in Scala? Given: class

  • 0

Related: How do you assign a function to a value in Scala?

Given:

class Foo{
  def bar = println("bar")
  def bat = println("bat")
}

How do I create a fnRef such that it points to either Foo.bar or Foo.bat?

def deepFunction(foos : List[Foo], fnRef : ()=>Unit) = {
  foos.map(_.fnRef) //May call either bar or bat
}  

Bonus: Is it possible to constrain the fnRef so that it’s only methods of that signature within the Foo class?

  • 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-31T13:19:09+00:00Added an answer on May 31, 2026 at 1:19 pm

    You don’t. 🙂 Instead you write your code in terms of first class functions. What makes this nice in Scala is that it will create a function literal from _.method that’s of type T => R, where T is the type of the parameter, and R is the method’s return type.

    So, _.bar and _.bat would both make Foo => Unit:

    scala> class Foo{
         |   def bar = println("bar")
         |   def bat = println("bat")
         | }
    defined class Foo
    
    scala> def deepFunction(foos: List[Foo], fn: Foo => Unit) {
         |   foos.map(fn)
         | }
    deepFunction: (foos: List[Foo], fn: Foo => Unit)Unit
    
    scala> deepFunction(List(new Foo, new Foo), _.bar)
    bar
    bar
    
    scala> deepFunction(List(new Foo, new Foo), _.bat)
    bat
    bat
    

    What’s really nice about this approach is that you can use any function you want, not just member functions.

    scala> def bam(f: Foo) { println("bam") }
    bam: (f: Foo)Unit
    
    scala> deepFunction(List(new Foo, new Foo), bam)
    bam
    bam    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know why I can't assign a value captured by .hover function to
Related: How can I use polymorphism in XML Serialization? I have a class I
This is tenuously related to C#: How do i assign many variables with an
This is a follow up on a related topic found here https://stackoverflow.com/questions/1987485/conditionally-assign-c-var-as-elegant-as-it-get s if
I know that I can assign a value specifically to a float by doing
Possible Duplicate: Create shortcut to console.log() In javascript we can easy assign functions to
Related to https://stackoverflow.com/questions/139944/where-can-one-find-free-software-icons-images I have a need for free weather-related icons. Specifically, I need
Related to this question , I decided to check the UDFs in my data
Related to this question: URL characters replacement in JSP with UrlRewrite I want to
(Related to Is there a secure Browser Cache? ): Do any of the implementations

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.