Why are lambda expressions not “interned”?

前端 未结 6 903
-上瘾入骨i
-上瘾入骨i 2021-01-01 19:22

Strings are reference types, but they are immutable. This allows for them to be interned by the compiler; everywhere the same string literal appears, the same objec

6条回答
  •  耶瑟儿~
    2021-01-01 19:52

    You're mistaken to call it a literal, IMO. It's just an expression which is convertible to a delegate type.

    Now as for the "interning" part - some lambda expressions are cached , in that for one single lambda expression, sometimes a single instance can be created and reused however often that line of code is encountered. Some are not treated that way: it usually depends on whether the lambda expression captures any non-static variables (whether that's via "this" or local to the method).

    Here's an example of this caching:

    using System;
    
    class Program
    {
        static void Main()
        {
            Action first = GetFirstAction();
            first -= GetFirstAction();
            Console.WriteLine(first == null); // Prints True
    
            Action second = GetSecondAction();
            second -= GetSecondAction();
            Console.WriteLine(second == null); // Prints False
        }
    
        static Action GetFirstAction()
        {
            return () => Console.WriteLine("First");
        }
    
        static Action GetSecondAction()
        {
            int i = 0;
            return () => Console.WriteLine("Second " + i);
        }
    }
    

    In this case we can see that the first action was cached (or at least, two equal delegates were produced, and in fact Reflector shows that it really is cached in a static field). The second action created two unequal instances of Action for the two calls to GetSecondAction, which is why "second" is non-null at the end.

    Interning lambdas which appear in different places in the code but with the same source code is a different matter. I suspect it would be quite complex to do this properly (after all, the same source code can mean different things in different places) and I would certainly not want to rely on it taking place. If it's not going to be worth relying on, and it's a lot of work to get right for the compiler team, I don't think it's the best way they could be spending their time.

提交回复
热议问题