question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attribute lazy initialization

See original GitHub issue

[@matejonnet] Looking for a shorter way to initialize an object attribute on a first use, and ability to use immutable attribute instead of variable

variable Foo? foo := null;
Foo getFoo() {
    if (exists f = foo) {
        return f;
    }
    foo := createFoo();
    if (exists f = foo) {
        return f;
    }
    throw;
}

I would like to write this as:

Foo? foo;
Foo getFoo() {
    if (!exists foo) {
        foo = createFoo();
    }
    return foo;
}

[Migrated from ceylon/ceylon-spec#438]

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:2
  • Comments:94 (64 by maintainers)

github_iconTop GitHub Comments

3reactions
bjansencommented, Jan 17, 2017

🎉

3reactions
gavinkingcommented, Nov 20, 2016

I propose the following syntax:

late Float pi = calculatePi();

Where I’m repurposing the late annotation to mean “initialized lazily” instead of just “initialized later”. There’s no ambiguity here, since currently a late field can’t have an initializer, and the compiler enforces that.

Reasons why I think this is reasonable:

  • it seems to match the English meaning of “late”,
  • the actual code generation is pretty similar: instead of throwing an exception, we initialize it to the given expression, and
  • it seems to me that issue #6721 applies equally to lazy fields as to late fields (they can both be placed in the declaration section).
Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy Initialization - .NET Framework - Microsoft Learn
To implement a public property by using lazy initialization, define the backing field of the property as a Lazy<T>, and return the Value ......
Read more >
Lazy loading - Web performance | MDN
Lazy loading is a strategy to identify resources as non-blocking (non-critical) and load these only when needed. It's a way to shorten the ......
Read more >
Lazy initialization - Wikipedia
In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other...
Read more >
Lazy class attribute initialization - python - Stack Overflow
_attr2 is a private instance attribute that both indicates whether the value has been computed yet, and saves the computed value for future...
Read more >
jonathanstowe/Attribute-Lazy: Lazy initialization for Perl 6 ...
This is based on an experimental trait that was briefly in the Rakudo core. Attribute::Lazy provides a single trait will lazy that will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found