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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:57:52+00:00 2026-06-18T10:57:52+00:00

MWE (obs.: I am avoiding to have to instantiate a class every call of

  • 0

MWE (obs.: I am avoiding to have to instantiate a class every call of c, this is why functions are desired):

object Main extends  App {
     def a(s:String, i:Int) ={
        s + i * i         //some complex op that yields String
     }

     def b(i:Int) ={
        i / 3             //another complex op that yields Int
     }

     def c(f: Any => Any) = { 
        val L = List(1,2,3,4) //list of complex elements
        L map f               //apply f within some complex loop
     }

     println(c(a))
     /*
        scala: type mismatch;
        found   : (String, Int) => String
        required: Any => Any
        println(c(a))
                  ^
      */

     println(c(b))
     /*
        scala: type mismatch;
        found   : Int => Int
        required: Any => Any
        println(c(b))
                  ^
     */
}

Maybe an equivalent question would be “Is there some kind of function inheritance?”,
like

def f
def fa(i: Int):String extends f
def fb(s: String):Int extends f
  • 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-06-18T10:57:53+00:00Added an answer on June 18, 2026 at 10:57 am

    What you’re trying to do isn’t type-safe, since if it were you could pass a String to a function which takes an Int parameter:

    e.g.

    def c(f: Any => Any) = { 
        val L = List("a", "b", "c")
        L map f
    }
    
    c(a)
    

    However you can take a function of type Int => Any, since it is safe to assign a more derived type to Any.

    def c(f: Int => Any) = {
        val l = List(1,2,3,4)
        l.map(f)
    }
    

    This is now safe:

    val bList: List[Any] = c(b)
    

    You still can’t pass a to c however, since it requires two arguments instead of one. You can partially apply the first String argument and pass that:

    val cList = c(a("SomeString", _:Int))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MWE #include <iostream> struct Foo { Foo() { std::cout << Constructing Foo << this
Here is a MWE, where I'm calculating the distribution of (in this case) 181
I have a master document \documentclass{article} \begin{document} <<main>>= a <- 20 b <- 30
I have the following python MWE (code is explained below) #!/usr/bin/python from scipy import
given all the possible solutions to have a template system with GNU Emacs, what
I have the need to build a function that will plot multiple pdfs, read
Here is my MWE code. \documentclass{beamer} \begin{document} <<setup, include=FALSE>>= # smaller font size for
Suppose I have a data.frame that's completely numeric . If I make one entry
Here's an MWE: Func<Int32, Boolean> MyFunc = (s) => { var res = false;
Hi I have a question that: consider I have T(n) = m * n^2

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.