Search code examples
unity-game-enginelambdadelegatesdotween

Why is lambda expression used in DOTween?


I'm using a lambda expression in my C# script in my Unity project to call a function with a parameter when a DOTween callback is called.

It simply looks like this: animation.OnComplete(() => DestroyOnCompleted(children));

It works just fine, but I am note sure why a lambda expression is used in this case. I know it has something to do with delegates, but other than that, I'm not too sure; and I would like to explain it to my exam, if I get asked about it.

Could anyone enlighten me? :-)


Solution

  • If you look at the documentation of DotTween, you see that row:

    enter image description here

    Now looking at the Source Code of DotTween, you can see the definition of TweenCallback:

    public delegate void TweenCallback();
    

    So the question now is, what is a delegate void in c#?

    A delegate in c# is basically an object that "represent" a function. But functions are not all the same, they can have parameters in input and return something (or return void). To understand what kind of function does a delegate represent, try to just remove the keyword delegate.

    For example, the TweenCallback without the keyboard delegate is:

    public void TweenCaalback()
    

    So the delegate represent a void function that has no parameters in input! (And it is Public).

    What does it means represent a function?

    It means that this is valid code:

    void DoNothing()
    {
    
    }
    
    TweenCallback x = DoNothing;
    x();
    

    So you can "assign functions" to a delegate that has the same function signature. In this case, TweenCallback is a delegate void (), so you can assign to it a void() function.

    What is a lambda?

    A lambda is an expression of that style: (string name, int age) => { return 3 };

    you can read that as "string name and int age go in return 3"

    That's a more concise way to describe that function:

    int AnonymousFunction (string name, int age) {}
    

    The main difference is that lambdas do not have any name. If you have not any parameter in input the lambda become like this:

    () => {return 3;}
    

    If you have only one statement inside the {} you are allowed to write it more shortly as

    () => 3;
    

    Final step

    Is this valid code?

    void DoNothing()
    {
    
    }
    
    TweenCallback x = () => DoNothing();
    

    Yes it is! Tween callback is expects a void () function.

    () => DoNothing(); Is a lambda (un-named function) that takes nothing in input and calls some other function. It's the shorter version of () => {DoNothing();} that you have to think as void () {DoNothing();}

    So when writing

    animation.OnComplete(() => DestroyOnCompleted(children));
    

    You are just passing a void () function to OnComplete Method, that makes sense because TweenCallback is a void () delegate.

    Notes

    As you can see, functions and lambdas expression can be converted implicitly to delegates. But you have to understand that they are all different things, and in more advanced coding scenarios that distinction is not just pure theory.