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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:52:00+00:00 2026-05-25T19:52:00+00:00

This is just one of those I was wondering… questions. Scala has immutable data

  • 0

This is just one of those “I was wondering…” questions.

Scala has immutable data structures and (optional) lazy vals etc.

How close can a Scala program be to one that is fully pure (in a functional programming sense) and fully lazy (or as Ingo points out, can it be sufficiently non-strict)? What values are unavoidably mutable and what evaluation unavoidably greedy?

  • 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-25T19:52:01+00:00Added an answer on May 25, 2026 at 7:52 pm

    Regarding lazyness – currently, passing a parameter to a method is by default strict:

    def square(a: Int) = a * a
    

    but you use call-by-name parameters:

    def square(a: =>Int) = a * a
    

    but this is not lazy in the sense that it computes the value only once when needed:

    scala> square({println("calculating");5})
    calculating
    calculating
    res0: Int = 25
    

    There’s been some work into adding lazy method parameters, but it hasn’t been integrated yet (the below declaration should print "calculating" from above only once):

    def square(lazy a: Int) = a * a
    

    This is one piece that is missing, although you could simulate it with a local lazy val:

    def square(ap: =>Int) = {
      lazy val a = ap
      a * a
    }
    

    Regarding mutability – there is nothing holding you back from writing immutable data structures and avoid mutation. You can do this in Java or C as well. In fact, some immutable data structures rely on the lazy primitive to achieve better complexity bounds, but the lazy primitive can be simulated in other languages as well – at the cost of extra syntax and boilerplate.

    You can always write immutable data structures, lazy computations and fully pure programs in Scala. The problem is that the Scala programming model allows writing non pure programs as well, so the type checker can’t always infer some properties of the program (such as purity) which it could infer given that the programming model was more restrictive.

    For example, in a language with pure expressions the a * a in the call-by-name definition above (a: =>Int) could be optimized to evaluate a only once, regardless of the call-by-name semantics. If the language allows side-effects, then such an optimization is not always applicable.

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

Sidebar

Related Questions

Just wondering if this and other related functions like those Integer is one of
This is just one of those little things I did all the time in
I have a problem with my WCF Data Service, and this one is just
I installed from kubuntu's package management this handy pnglite library. It contains just one
I'm creating a stacked bar chart just like this one . Notice that the
I have a method somewhat similar to the one written bellow(this is just a
I just saw this question and one of the answers looks really appealing to
Just a quick one, hopefully. I've coded up this: <script> $(div.design-project).css('display', 'none'); function InsertContent(tid)
if ConvertAll throw an exception on one element, can i just skip this element
this one is not homework, it just the fact that i've been out of

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.