Open sandboxFocus

Class DurableLazy<T>

A durable alternative to Lazy<T>, that is, one that is safe to be held across compilations. The factory delegate is released as soon as the value has been computed.

Inheritance
DurableLazy<T>
Namespace: Metalama.Framework.Utilities
Assembly: Metalama.Framework.dll
Syntax
[Durable]
public sealed class DurableLazy<T>
Type Parameters
Name Description
T

The type of the lazily computed value, which must itself be durable.

Remarks

Lazy<T> is not durable, because it holds its factory delegate and a delegate holds its closure. A lambda that mentions a compilation produces a closure object that holds it, and whatever holds the delegate holds the compilation. That capture is invisible in the source, which is what makes it the most expensive mistake of this kind.

This type closes the gap in two ways. The constructor parameter is marked with DurableAttribute, so the analyzer examines what the factory captures at every call site. And the factory is set to null once the value has been computed, so nothing it captured is retained beyond the first evaluation.

The value is computed at most once. Like the default mode of Lazy<T>, this type uses lock based synchronization, so a factory that acquires the same lock from another thread deadlocks.

Constructors

Name Description
DurableLazy(Func<T>)

Initializes a new instance of the DurableLazy<T> class.

Properties

Name Description
IsValueCreated

Gets a value indicating whether the value has already been computed.

Value

Gets the value, computing it on the first access.

Methods

Name Description
ToString()

Extension Methods