Skip to main content

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

AttributeTypeDescription
initfn()- > TA function pointer used to initialize the value on the first access.
valueOnceLock< 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

NameTypeDescription
initfn()- > TA function that produces the value when called.
valueOnceLock< T >The synchronization primitive used to store the initialized value.

Signature

@classmethod
def new(
init: fn()- > T
) - > LazyLock< T >

Parameters

NameTypeDescription
initfn()- > TThe 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

TypeDescription
&TA reference to the initialized value.