lambdabeef

What does [&] mean when defining a lambda?


In the method reference documentation, there is some example code, copied below:

char8 GetNext()
{
    if (i >= str.Length)
        return 0;
    return str[i++];
}

/* Allocates a delegate bound to GetNext() */
delegate char8() strDlg = scope => GetNext;

strDlg = scope () =>
{
    return 'A';
};

strDlg = scope [&] () =>
{
    return GetNext();
};

/* This delegate owns a string */
String tempStr = new String(str);
tempStr.EnsureNullTerminated();
strDlg = scope [&] () =>
{
    return str[i++];
}
~
{
    delete tempStr;
};

In two of the three examples of lambda definitions, there is a [&] between the allocation expression (scope) and the parameters of the lambda. What is that [&]?

For additional examples, here are some excerpts from tests included in the IDE source:

struct Splattable
{
    public int32 mA = 10;
    public int16 mB = 200;

    public void TestLambda() mut
    {
        delegate int(ref int a, ref int b) dlg = scope [&] (a, b) => 
        {
            a += 20;
            b += 30;
            mA++;
            return mA + a + b;
        };

        mA = 100;
        int testA = 8;
        int testB = 9;
        Test.Assert(dlg(ref testA, ref testB) == 100+1 + 20+8 + 30+9);
        Test.Assert(testA == 28);
        Test.Assert(testB == 39);
        Test.Assert(mA == 101);
    }
}

class ClassA
{
    public int mA;

    public void TestLambda()
    {
        delegate int(ref int a, ref int b) dlg = scope (a, b) => 
        {
            a += 20;
            b += 30;
            mA++;
            return mA + a + b;
        };

        mA = 100;
        int testA = 8;
        int testB = 9;
        Test.Assert(dlg(ref testA, ref testB) == 100+1 + 20+8 + 30+9);
        Test.Assert(testA == 28);
        Test.Assert(testB == 39);
        Test.Assert(mA == 101);
    }
}

Here's another set of example/tests from the IDEHelper part of the source code where every lambda definition has [&]:

using System;

namespace Tests
{
    class Lambdas
    {
        [Test]
        static void TestBasics()
        {
            int a = 1;

            Action act = scope [&] () =>
            {
                Action act2 = scope [&] () =>
                {
                    a += 100;
                };
                act2();
            };
            act();

            Test.Assert(a == 101);
        }

        static int Add3<T>(T func) where T : delegate int()
        {
            return func() + func() + func();
        }

        [Test]
        static void TestValueless()
        {
            Test.Assert(Add3(() => 100) == 300);

            int a = 20;
            int result = Add3(() => a++);
            Test.Assert(result == 63);
        }

        [Test]
        static void LambdaWithDtor()
        {
            int a = 10;
            int b = 20;

            //
            {
                delegate void() dlg = scope [&] () =>
                {
                    a++;
                }
                ~
                {
                    b++;
                };
                dlg();
            }

            Test.Assert(a == 11);
            Test.Assert(b == 21);

            delegate void() dlg = new [&] () =>
            {
                a += 100;
            }
            ~
            {
                b += 200;
            };
            dlg();
            Test.Assert(a == 111);
            Test.Assert(b == 21);
            delete dlg;
            Test.Assert(b == 221);
        }
    }
}

Finally, [&] may be not strictly a lambda thing. I can put it in other definitions like let s = new [&] String(); and it compiles with no issue and as far as I can tell runs with the same outcome as without it. But, I haven't yet seen it occur anywhere else in example/test code.


Solution

  • [&] Means capture any referenced locals by reference rather than by value.

    In the example, for ClassA this is being passed by value to the lambda. Effectively: this.mA++;