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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:50:08+00:00 2026-05-27T06:50:08+00:00

Why is a return statement required to allow this while statement to be evaluated

  • 0

Why is a return statement required to allow this while statement to be
evaluated properly? The following statement allows

import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.io.BufferedReader
import java.io.InputStreamReader

trait Closeable {
  def close ()
}

trait ManagedCloseable extends Closeable {
  def use (code: () => Unit) {
    try {
      code()
    }
    finally {
      this.close()
    }
  }
}

class CloseableInputStream (stream: InputStream)
extends InputStream with ManagedCloseable {
  def read = stream.read
}

object autoclose extends App {
  implicit def inputStreamToClosable (stream: InputStream):
    CloseableInputStream = new CloseableInputStream(stream)

  override
  def main (args: Array[String]) {
    val test = new FileInputStream(new File("test.txt"))

    test use {
      val reader = new BufferedReader(new InputStreamReader(test))

      var input: String = reader.readLine

      while (input != null) {
        println(input)
        input = reader.readLine
      }
    }
  }
}

This produces the following error from scalac:

autoclose.scala:40: error: type mismatch;
 found   : Unit
 required: () => Unit
      while (input != null) {
      ^
one error found

It appears that it’s attempting to treat the block following the use as an
inline statement rather than a lambda, but I’m not exactly sure why. Adding
return after the while alleviates the error:

test use {
  val reader = new BufferedReader(new InputStreamReader(test))

  var input: String = reader.readLine

  while (input != null) {
    println(input)
    input = reader.readLine
  }

  return
}

And the application runs as expected. Can anyone describe to me what is going
on there exactly? This seems as though it should be a bug. It’s been
persistent across many versions of Scala though (tested 2.8.0, 2.9.0, 2.9.1)

  • 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-27T06:50:08+00:00Added an answer on May 27, 2026 at 6:50 am

    That’s because it’s use is declared as () => Unit, so the compiler expects the block you are giving use to return something that satisfies this signature.

    It seems that what you want is to turn the entire block into a by-name parameter, to do so change def use (code : () => Unit) to def use (code : => Unit).

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

Sidebar

Related Questions

When is pinning using the fixed() statement required? I see many examples, including this
I have tried something like this in my Bison file... ReturnS: RETURN expression {printf(;)}
Dupe: return statement in a lock procedure: inside or outside The title is a
Take the standard return statement for a controller: return View(Index); is there a way
I have seen some developers use the return statement in a catch block. Why/when
I like the idea of having only one return statement per method. What do
I have a DataService returning an IQueryable I breakpoint at the return statement, list
I was surprised recently to find that it's possible to have a return statement
I am trying to find a way to have a SELECT statement return the
In the end of my function, I have the statement: RETURN @Result What I

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.