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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:23:29+00:00 2026-05-15T08:23:29+00:00

At just after 2:40 in ShadowofCatron ‘s Scala Tutorial 3 video , it’s pointed

  • 0

At just after 2:40 in ShadowofCatron‘s Scala Tutorial 3 video, it’s pointed out that the parentheses following the name of a thunk are optional. “Buh?” said my functional programming brain, since the value of a function and the value it evaluates to when applied are completely different things.

So I wrote the following to try this out. My thought process is described in the comments.

object Main {

    var counter: Int = 10
    def f(): Int = { counter = counter + 1; counter }

    def runThunk(t: () => Int): Int = { t() }

    def main(args: Array[String]): Unit = {
        val a = f()     // I expect this to mean "apply f to no args"
        println(a)      // and apparently it does

        val b = f       // I expect this to mean "the value f", a function value
        println(b)      // but it's the value it evaluates to when applied to no args
        println(b)      // and the application happens immediately, not in the call

        runThunk(b)     // This is an error: it's not println doing something funny
        runThunk(f)     // Not an error: seems to be val doing something funny
    }

}

 

To be clear about the problem, this Scheme program (and the console dump which follows) shows what I expected the Scala program to do.

(define counter (list 10))
(define f (lambda ()
            (set-car! counter (+ (car counter) 1))
            (car counter)))

(define runThunk (lambda (t) (t)))

(define main (lambda args
               (let ((a (f))
                     (b f))
                 (display a) (newline)
                 (display b) (newline)
                 (display b) (newline)
                 (runThunk b)
                 (runThunk f))))

> (main)
11
#<procedure:f>
#<procedure:f>
13

 

After coming to this site to ask about this, I came across this answer which told me how to fix the above Scala program:

    val b = f _     // Hey Scala, I mean f, not f()

But the underscore ‘hint’ is only needed sometimes. When I call runThunk(f), no hint is required. But when I ‘alias’ f to b with a val then apply it, it doesn’t work: the application happens in the val; and even lazy val works this way, so it’s not the point of evaluation causing this behaviour.

 

That all leaves me with the question:

Why does Scala sometimes automatically apply thunks when evaluating them?

Is it, as I suspect, type inference? And if so, shouldn’t a type system stay out of the language’s semantics?

Is this a good idea? Do Scala programmers apply thunks rather than refer to their values so much more often that making the parens optional is better overall?


Examples written using Scala 2.8.0RC3, DrScheme 4.0.1 in R5RS.

  • 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-15T08:23:30+00:00Added an answer on May 15, 2026 at 8:23 am

    The problem is here:

    Buh?” said my functional programming
    brain, since the value of a function
    and the value it evaluates to when
    applied are completely different
    things.

    Yes, but you did not declare any function.

    def f(): Int = { counter = counter + 1; counter }
    

    You declared a method called f which has one empty parameter list, and returns Int. A method is not a function — it does not have a value. Never, ever. The best you can do is get a Method instance through reflection, which is not really the same thing at all.

    val b = f _     // Hey Scala, I mean f, not f()
    

    So, what does f _ means? If f was a function, it would mean the function itself, granted, but this is not the case here. What it really means is this:

    val b = () => f()
    

    In other words, f _ is a closure over a method call. And closures are implemented through functions.

    Finally, why are empty parameter lists optional in Scala? Because while Scala allows declarations such as def f = 5, Java does not. All methods in Java require at least an empty parameter list. And there are many such methods which, in Scala style, would not have any parameters (for example, length and size). So, to make the code look more uniform with regards to empty parameter list, Scala makes them optional.

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

Sidebar

Related Questions

I have a program that writes to a FILE *cgiOut and just after it
I just went through some MVC tutorials after checking this site out for a
I'm getting this error after trying to appendChild to an element that was just
I’m stuck in trying to grep anything just after name= , include only spaces
Just after some opinions on the best way to achieve the following outcome: I
Just dipping my toes into Linq2sql project after years of rolling my own SQL
I'm just getting back into C++ after a couple of years of doing a
I just purchased C++ GUI Programming with Qt4 and after reading the code samples
I just found /n softwares free Powershell NetCmdlets, and after playing with them I
After searching online, the best solution I've found so far is to just make

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.