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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:00:56+00:00 2026-06-04T10:00:56+00:00

class ClosureClass { def printResult[T](f: => T) = { println(choice 1) println(f) } def

  • 0
class ClosureClass {
  def printResult[T](f: => T) = {
    println("choice 1")
    println(f)
  }

  def printResult[T](f: String => T) = {
    println("choice 2")
    println(f("HI THERE"))
  }
}

object demo {
  def main(args: Array[String]) {
    val cc = new ClosureClass
    cc.printResult()   // call 1
    cc.printResult("Hi")  // call 2
  }
}

I play with the above code, and the result showed me. I have two questions

1) why both call 1 and call 2 go into choice 1?

2) How can I pass a parameter so that I can get into choice 2.

Thanks,

choice 1
()
choice 1
Hi
  • 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-04T10:00:57+00:00Added an answer on June 4, 2026 at 10:00 am

    The type => T means that f is a call-by-name parameter. This means that f is of type T, and the expression passed into the function will be evaluated when it is used (not when the function is called.

    The type String => T means that f is a Function[String,T], that is, a function from a String to a T.

    When you call with "Hi", a String as the argument, Scala sees that there are two choices for printResult:

    1) => T: In this case case T would bind to String, which is fine.

    2) String => T: This does not work since String is not a function from String to anything… it’s not a function at all.

    How you fix this depends on what you are trying to do. If you just want to fix how printResult is being called, then you call call it with:

    val g: (String => String) = (s: String) => s + "***"
    cc.printResult(g)
    

    which prints:

    choice 2
    HI THERE***
    

    since you are now are now correctly passing a function, g, from String to some T. That T here is String, since the function is just adding *** onto the end of whatever it is passed and returning the modified string.

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

Sidebar

Related Questions

class ZiggyTest{ public static void main(String[] args){ RunnableThread rt = new RunnableThread(); Thread t1
{ class Program { static void Main(string[] args) { int id = 0, stock
class Program { static void Main(string[] args) { Console.WriteLine(Fib 1: ); Console.ReadLine(); } long
class ZiggyTest{ public static void main(String[] args) { String str = aaaaaaaaabb; String []
class A { String s4 = abc; static public void main(String[]args ) { String
class Program { private int x = 0; static void Main(string[] args) { Program
class Hello @hello = hello def display puts @hello end end h = Hello.new
class ITestType(object): Sample interface type __metaclass__ = ABCMeta @abstractmethod def requiredCall(self): return class TestType1(object):
class test: def __init__(self, val): self.val = val self.val.lower() Why doesn't lower() operate on
class Object { }; Class Derived : public Object { }; boost::shared_ptr<Object> mObject(new Derived);

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.