LazyLock
This class provides a mechanism for lazy initialization of a value, ensuring that the initialization function is only invoked once upon the first access. It utilizes a synchronization primitive to manage the internal state, making it suitable for scenarios where a value is expensive to compute and may not always be needed.
Attributes
| Attribute | Type | Description |
|---|---|---|
| init | fn()- > T | A function pointer used to initialize the value on the first access. |
| value | OnceLock< T > | A synchronization primitive that stores the initialized data and ensures it is only computed once. |
Constructor
Signature
def LazyLock(
init: fn()- > T,
value: OnceLock< T >
) - > [LazyLock](lazylock.md?sid=build_tools_lazylock)
Parameters
| Name | Type | Description |
|---|---|---|
| init | fn()- > T | A function that produces the value when called. |
| value | OnceLock< T > | The synchronization primitive used to store the initialized value. |
Signature
@classmethod
def new(
init: fn()- > T
) - > LazyLock< T >
Parameters
| Name | Type | Description |
|---|---|---|
| init | fn()- > T | The function or closure used to compute the value when it is first requested. |
Methods
force()
def force() - > &T
Ensures the value is initialized and returns a reference to it, triggering the initialization function if it has not been called yet.
Returns
| Type | Description |
|---|---|
&T | A reference to the initialized value. |