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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:32:53+00:00 2026-06-04T12:32:53+00:00

I can the require method in Scala’s Predef class with a String as second

  • 0

I can the require method in Scala’s Predef class with a String as second argument, e.g.

require ("foo" == "bar", "foobar")

First a thought the require method is overloaded for different parameters as second argument. But it is not. The Signature of the require method (Scala 2.9.1) is:

require(requirement: Boolean, message: ⇒ Any): Unit

Why is the above method call possible ?

  • 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-04T12:32:54+00:00Added an answer on June 4, 2026 at 12:32 pm

    I don’t fully understand the question, but here is a bit of explanation. require has one overloaded version in Predef:

    def require(requirement: Boolean) //...
    def require(requirement: Boolean, message: => Any) //...
    

    The second one is a bit confusing due to message: => Any type. It would probably be easier if it was simply:

    def require(requirement: Boolean, message: Any) //...
    

    The second parameter is of course a message that is suppose to be appended to error message if assertions is not met. You could imagine message should be of String type but with Any you can simply write:

    require(x == 4, x)
    

    Which will add actual value of x (of type Int) into an error message if it is not equal to 4. That’s why Any was chosen – to allow arbitrary value.

    But what about : => part? This is called call by name and basically means: evaluate this parameter when it is accessed. Imagine the following snippet:

    require(list.isEmpty, list.size)
    

    In this case you want to be sure the list is empty – and if it is not, add the actual list size to the error message. However with normal call convention the list.size part must be evaluated before the method is called – which might be wasteful. With call by name convention the list.size is only evaluated the first time it is used – when the error message is constructor (if required).

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

Sidebar

Related Questions

Using Scala's command line REPL: def foo(x: Int): Unit = {} def foo(x: String):
In Visual Studio, you can require that an assembly reference match a specific version
How can I require the Address element? If I perform an XSD validation on
Can any user run inline assembly for current linux kernel or does that require
How can you determine which rubygem is being used in response to a require
How can I rewrite Apple's GLCameraRipple example so that it doesn't require iOS 5.0?
I hope I can explain this right I have two input fields that require
Edit: We can close. Isn't truly asynchronous, non-blocking javascript impossible? var PATH = require
Quick yes/no - I'm building an AJAX application and some scripts require authentication. Can
the following code raises the aforementioned error, how can I fix that? require 'mechanize'

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.