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:
- Created 11 years ago
- Reactions:2
- Comments:94 (64 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
🎉
I propose the following syntax:
Where I’m repurposing the
late
annotation to mean “initialized lazily” instead of just “initialized later”. There’s no ambiguity here, since currently alate
field can’t have an initializer, and the compiler enforces that.Reasons why I think this is reasonable: