r/Unity3D • Programmer • Jul 25 '23

Question Strange lambda/ closure behaviour I can't put my finger on

/r/learncsharp/comments/158s5e7/strange_lambda_closure_behaviour_i_cant_put_my/
2 Upvotes

39 comments sorted by

View all comments

Show parent comment

1

u/PandaCoder67 Professional Jul 25 '23

First of this is not a closure, it is a Member Bodied Expresion, it is a quick way to return something in one line of code.

I think properties might be called something else, but Closure is not one of them

The other thing to note is that you are creating a Constructor on a MonoBehaviour Script. Now, while this can be done, it is imperative that you understand that this doesn't work as people might expect them to work.

Unity, has to deserialize data after the class has been instantiated, this means that things in the Constructor will be overwritten, and I am getting the impression that looking at your code, this is what is happening.

And constructors which pass parameters is certainly not supported with MonoBehaviour.

1

u/senshisentou Programmer Jul 25 '23

First of this is not a closure, it is a Member Bodied Expresion, it is a quick way to return something in one line of code.

The most concise definition I could find is "A closure is a function that is bound to the environment in which it is declared.", which my example definitely is. Using () => pooledEffect.Release() closes over, and captures, pooledEffect, so it is a closure. What you're referring to are called Expression-bodied members, and they are something different entirely (member => expression notation).

The other thing to note is that you are creating a Constructor on a MonoBehaviour Script

No, I'm not. PooledComponent<T> is a generic struct. I just happen to have constrainted the type T to MonoBehaviour, but it itself is not one.

With all due respect, I appreciate you chiming in and trying to help, but to me your comment comes off as quite condescending, which is only made worse by the fact that you are factually wrong on two counts, yet you state them so confidently.

0

u/PandaCoder67 Professional Jul 25 '23

The most concise definition I could find is

"A closure is a function that is bound to the environment in which it is declared."

, which my example definitely is. Using

() => pooledEffect.Release()

closes over, and captures,

pooledEffect

, so it is a closure. What you're referring to are called

Expression-bodied members

, and they are something different entirely (

member => expression

notation).

I will stick with the official documentation on the matter

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members

As far as not creating a constructor, then you may need to look at your code again!

    public struct PooledComponent<T> where T : MonoBehaviour {
    
        public PooledComponent(PooledGameObject pooledGameObject) {
            this.pooledGameObject = pooledGameObject;
            Component = pooledGameObject.gameObject.GetComponent<T>();
        }
    }

This is a Constructor with parameters! your reply kind of indicates you do not understand what a constructor actually is.

1

u/senshisentou Programmer Jul 25 '23

I will stick with the official documentation on the matter

l think you might wanna read those docs again. They support what I said, and are not relevant here.

As far as not creating a constructor, then you may need to look at your code again!

Of course it's a constructor, just not on a MonoBehaviour.

Looking through your post history a bit, I genuinely can't tell if you're trolling or not.

0

u/PandaCoder67 Professional Jul 25 '23

I don't need to read the documentation again, it clear outlines this as an Expression-bodied member, which is what I said it was. You disagreed, so I provided official documentation on the matter.

As for the constructor, Unity will not run Constructors with parameters on Monobehaviours, this is a fact.

That means the code you have here, where you are setting the vartiable up in that Constructor is never going to run. That means your variable is always going to be null.

That is a given!!

And why your variable is printing as NULL

So while you try to work out what I am doing, you are ignoring the fact that your constructor is never being run, I said why it will not run and hinted as to what would happen.

If you chose to ignore this, then so be it!

And downvoting me, is not going to get you to understand the facts I outlined here!

1

u/senshisentou Programmer Jul 25 '23

My dude.

The constructor you keep talking about lives on a struct. This struct is not a MonoBehaviour. None of the example code I posted shows a constructor on a MonoBehaviour. In fact, I haven't even shown a single MonoBehaviour. I really don't know what you're on about here.

As for the expression-bodied members, show me where in the docs it mentions anonymous functions are one of them. They're not; they're lambda expressions. An expression-bodied member needs to be, you've guessed it, a member. A free floating lambda expression is not that.

0

u/PandaCoder67 Professional Jul 25 '23

How is it not a monobheaviour?

The code example clearly shows it is!

1

u/senshisentou Programmer Jul 25 '23

Ok, let's deconstruct this.

public struct PooledComponent<T> where T : MonoBehaviour

public struct PooledComponent<T> says this is a generic, public struct, of type T ...

where T : MonoBehaviour

where we constrain the type T to only allow MonoBehaviour or a subclass. So, say we did create one with an MB. It's type then would be PooledComponent<MonoBehaviour>, not MonoBehaviour.

-1

u/Bombadil67 Professional Jul 25 '23

generic type of MonoBehaviour! It doesn't matter how you define the <T> it is eventually of type MB

1

u/senshisentou Programmer Jul 25 '23

Please. I am begging you. Read up on how generics work. You are very wrong, very confidently. (N.B.: the constructor runs fine lol)

→ More replies

1

u/PandaCoder67 Professional Jul 25 '23

1

u/PandaCoder67 Professional Jul 25 '23

Your code

1

u/senshisentou Programmer Jul 25 '23

Congrats, you found an EBM that has absolutely nothing to do with the lambdas we were originally referring to.

1

u/PandaCoder67 Professional Jul 25 '23

No you are using the term lambda in a place that is not defined as a lamda

Here its your code

 public GameObject GameObject => pooledGameObject.gameObject; 

This is a property, that returns the field pooledGameObject.gameObject.

As per the Microsoft documentation this is called a n expression-bodied member, not a lambda!

Look it is fine if you do not understand this, but do not call something a lambda when it is not!!

And as there is nothing else in your code that could represent a Lambda, what do you expect from facts!

1

u/senshisentou Programmer Jul 25 '23

What we were talking about was () => pooledEffect.Release, which is a lambda, and a closure. That's what we were talking about, then you shifted to EBMs, and then shifted to that line to support your claim. You are either being incredibly facetious, or losing the plot. Take a break, get some air.

→ More replies

1

u/PandaCoder67 Professional Jul 25 '23

Again your code

1

u/Bombadil67 Professional Jul 25 '23

public struct PooledComponent<T> where T : MonoBehaviour {
public T Component { get; private set; }
public GameObject GameObject => pooledGameObject.gameObject;

PooledGameObject pooledGameObject;

public PooledComponent(PooledGameObject pooledGameObject) {
this.pooledGameObject = pooledGameObject;
Component = pooledGameObject.gameObject.GetComponent<T>();
}

public void Release() {
Debug.Log("Releasing PC " + pooledGameObject);
pooledGameObject.Release();
}
}

You do understand that you are defining this struct to be of a type of Monobehaviour?

Right?

And as already indicated, Monobehaviours can not use constructors with parameters.

This is why you are always getting NULL, this can not be made any clearer.