Open sandboxFocusImprove this doc

LAMA0060: A template can't return a reference

Severity Error
Reported by Metalama.Framework
Message '<Member>' cannot be used as a template because it returns 'ref'. This feature is not yet supported.

Cause

Metalama doesn't support templates that return by reference. When it loads an aspect class, it reports LAMA0060 for every template member, for instance a member marked with [Introduce] or [Template], that's a method returning ref or ref readonly, or a property of a ref type. In the message, <Member> is the template member.

In the following example, the Counter aspect introduces a Count property that returns a reference to the counter field, so that callers can increment it.

1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0060.Error;
4
5public class CounterAttribute : TypeAspect
6{
7    [Introduce]
8    private int _count;
9
10    // Error: a template property cannot return a reference.
11    [Introduce]
    Error LAMA0060: 'CounterAttribute.Count' cannot be used as a template because it returns 'ref'. This feature is not yet supported.

12    public ref int Count => ref this._count;
13}
14
1namespace Doc.LAMA0060.Error;
2
3[Counter]
4internal partial class PageVisits { }
5

Resolution

Change the template so that it returns a value instead of a reference. If callers need to modify the underlying field, introduce a setter or a method that performs the modification. In the following example, the aspect introduces a read-only Count property and an Increment method.

1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0060.Fixed;
4
5public class CounterAttribute : TypeAspect
6{
7    [Introduce]
8    private int _count;
9
10    // Fixed: the property returns the value instead of a reference.
11    [Introduce]
12    public int Count => this._count;
13
14    [Introduce]
15    public void Increment() => this._count++;
16}
17
Source Code
1namespace Doc.LAMA0060.Fixed;
2

3[Counter]
4internal partial class PageVisits { }
5
Transformed Code
1namespace Doc.LAMA0060.Fixed;
2
3[Counter]
4internal partial class PageVisits
5{
6    private int _count;
7
8    public int Count
9    {
10        get
11        {
12            return _count;
13        }
14    }
15
16    public void Increment()
17    {
18        _count++;
19    }
20}
21

See also