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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:03:45+00:00 2026-06-09T15:03:45+00:00

Or are val s in scala objects lazy by default? Anyway, if it’s necessary

  • 0

Or are vals in scala objects lazy by default?

Anyway, if it’s necessary to declare a val in an object lazy by using lazy, is it possible to do something like

lazy object SomeObject

or (like you do in c++)

object A {
lazy:
    val a
    val b
    ...
}

Because I would like to be lazy and not have to relabel all my vals lazy val

  • 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-09T15:03:47+00:00Added an answer on June 9, 2026 at 3:03 pm

    To answer your first question (“are vals in scala objects lazy by default?”): No, not exactly, but the objects themselves are kind of lazy, which may be lazy enough. From 5.4 (“Object Definitions”) of the Scala language specification:

    Note that the value defined by an object definition is instantiated
    lazily. The new m$cls constructor is evaluated not at the point of
    the object definition, but is instead evaluated the first time m is
    dereferenced during execution of the program (which might be never at
    all).

    So, for example, if we have these three objects:

    object X {
      val answer = { println("Here's X's answer!"); 42 }
    }
    
    object Y {
      lazy val answer = { println("Here's Y's answer!"); 1 }
    }
    
    object Z extends App {
      println("Here we go.")
      println(X)
      println(Y)
      println(X.answer)
      println(Y.answer)
    }
    

    Then when we run Z, we see the following:

    Here we go.
    Here's X's answer!
    X$@38d24866
    Y$@f1aa6ce
    42
    Here's Y's answer!
    1
    

    So the val in X isn’t lazy, but it’s also not evaluated until the first time we use X.

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

Sidebar

Related Questions

I've been using scala's lazy val idiom a lot and I would like to
This question is about Scala objects, which are defined something like this: object Pipeline
Before I had something like this (simplified), using sbt 0.11.3: // project/Build.scala import sbt._
Here's a seq of tuples in Scala val t = Seq((1,2,3),(4,5,6)) I like to
I am generating the scala AST using the following code: val setting = new
In a Scala web application, is lazy val scoped to the lifetime of the
Is it possible to use something similar to where-clauses in Scala? Maybe there is
I'm puzzled by something in Scala. I seem to have objects that have the
I am trying to write some Scala code that needs to do something like:
I've got some nested objects like this: object Routes { object admin { def

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.