IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0b1
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

forget_deinit

forget_deinit[T: AnyType](var value: T)

Takes ownership and skips running __del__ deinitializers.

This is a low-level operation, and should not be used unless necessary. Consider if refactoring to avoid needing this function would be more appropriate.

This operation is not considered unsafe, as Mojo can not guarantee in general that destructors will eventually be run.

Note: Take care to use ^ to transfer when passing ImplicitlyCopyable values to forget_deinit(), to avoid forgetting a copy instead of the original value.

Example:

from std.memory import forget_deinit

@fieldwise_init
struct Noisy:
def __del__(deinit self):
print("@ Noisy.__del__: Noisy is being deleted!")

def main():
var noisy = Noisy()

# No deletion message is printed
forget_deinit(noisy^)

This will skip the destructor for the "root" value object and all of it's fields, recursively. Example:

from std.memory import forget_deinit

@fieldwise_init
struct Parent:
var child: Child

def __del__(deinit self):
print("@ Parent.__del__")

@fieldwise_init
struct Child(Movable):
def __del__(deinit self):
print("@ Child.__del__")

def main():
var parent = Parent(Child())

# Neither Parent.__del__ nor Child.__del__ is called.
forget_deinit(parent^)

Parameters:

  • T (AnyType): The type of the value to discard without running a deinitializer.

Args:

  • value (T): The value to discard without running a deinitializer.