Open sandboxFocusImprove this doc

LAMA0524: An initializer can't be added to all constructors of a record

Severity Error
Reported by Metalama.Framework
Message The aspect '<AspectType>' cannot add an initializer to all constructors of the record '<Record>', because the copy constructor cannot be changed. Consider adding initializers directly to the relevant constructors.

Cause

An aspect can add an initializer to all instance constructors of a type by calling the AddInitializer method on the type with the BeforeInstanceConstructor initializer kind. A record has a compiler-generated copy constructor, used by with expressions, and that constructor can't be changed.

LAMA0524 describes this situation, but current versions of Metalama no longer report it. When an aspect adds a BeforeInstanceConstructor initializer to a record, Metalama skips the copy constructor and adds the initializer to the other instance constructors, including the primary constructor of a positional record. Constructors that call another constructor of the same type with this(...) are also skipped, because the initializer already runs in the called constructor.

This diagnostic is no longer reported by Metalama, so it can't be shown in an example.

Resolution

You don't need to do anything if you're using a current version of Metalama.

If you see this error with an older version of Metalama, do one of the following:

  • Update Metalama to the latest version.
  • Add the initializer to the relevant constructors individually by calling AddInitializer on each IConstructor instead of on the type, and exclude the copy constructor.

See also