2

Under what cirumstances we should be using Lazy val?

Because coming from c#, Java, I never came across Lazy Val feature in those languages. Is this a feature of functional programming languages?

thanks

  • 2
    lazy vals can be used for heavy computation that may not be required. for instance, a simple if condition: `if(someLightCondition || someHeavyCondition) { // stuff }` where `someHeavyCondition` is a lazy val declared somewhere before. this could be achieved through a method call, but maybe the result is always the same, and by being a lazy val you avoid to compute more than once – pedrorijo91 Nov 22 '16 at 20:26
  • This question http://stackoverflow.com/questions/8000903/what-are-all-the-uses-of-an-underscore-in-scala is not closed and in fact highly received. Why couldn't a question like that exist for `lazy`? – Akavall Nov 22 '16 at 21:20
  • Thanks a lot both of you. –  Nov 22 '16 at 23:40
  • "When people are learning new languages, they will have some generic questions like this" Stack Overflow is simply not the right place to ask these questions. – The SE I loved is dead Nov 22 '16 at 23:55
  • Looks like a totally valid question to me – yǝsʞǝla Nov 23 '16 at 01:25
  • dorukayhan3, do you think stack overflow is only for elite programmers? this is a public forum and programmers at all levels of expertise are allowed here. who are you to say this is "not freaking place" to ask such questions. –  Nov 23 '16 at 03:33
  • dorumkayhan 3 - are you on this forum to collect badges by letting down or bullying new programmers... –  Nov 23 '16 at 03:43
  • Folks, Yogesh, Michael Zajac, cchantep, RP-, pamu. Do you get badges for putting questions on "hold"? shame on you folks. Stackoverflow is becoming one of the worst sites because of stupid rules. How is my quesation not a valid question? looks like when you dont know the answer you are finding reasons to stop those questions being posted. Do you guys know answer to my question? –  Nov 23 '16 at 03:47
  • 2
    The reason I think this is a valid question and is not too broad because it's a question about language feature. It's very close to "what is a lazy val" question which would be definitely in scope of SO. The question is about the difference between `val`, `def` and `lazy val` and what is the purpose of lazy val. It's no a broad or opinion based question because there is a clear purpose of lazy vals in Scala and general use cases/patterns that programmer should know about. It's not about discussing something that does not have a definite answer. – yǝsʞǝla Nov 23 '16 at 04:57
  • Anyhow, if there are 2+ reopen requests that's a sure sign that there are people who are interested and supportive of such questions so let it be. – yǝsʞǝla Nov 23 '16 at 04:59
  • Thanks a lot Aleksey. –  Nov 23 '16 at 16:44
  • I think this is fine question in terms of scope, but it does appear to be a duplicate of: http://stackoverflow.com/questions/7484928/what-does-a-lazy-val-do – Akavall Nov 23 '16 at 21:46
  • Akavall, my question was "when to use lazy val" . I should have specifically for use cases for lazy val – sutterhome1971 Nov 23 '16 at 23:36
  • But doesn't the answers to the question I am linking to give examples of when to use `lazy`? – Akavall Nov 24 '16 at 05:55

0 Answers0