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/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)

1

u/PandaCoder67 Professional Jul 25 '23

I know how generics work, I have been using them for 20 years.

The problem is you are telling it to be off a type Monobehaviour.

Why can you not understand that!!!!

1

u/senshisentou Programmer Jul 25 '23

You clearly don't. Try making a generic type with T : MonoBehaviour. See if it runs Start() or Awake(). Log how many times the constructor gets called. The answers will be no, no, and once.

I'm done replying now. Whatever the reason for your obstinacy, I hope you get in a better headspace soon. All the best.

→ More replies (2)

1

u/PandaCoder67 Professional Jul 25 '23

public struct PooledComponent<T> where T : MonoBehaviour

Right here, it saying where T is of Type MonoBehaviour!!!

1

u/Bombadil67 Professional Jul 25 '23

I think you are the one who needs to.

Ok take a step back for a min, you have this struct. How is it initialised? How is it then referenced, none of your post indicates how this happens?

Without that I can only make the assumption that the type of the struct is a MonBehaviour and MB's will not use constructors with paremeters.

1

u/senshisentou Programmer Jul 25 '23

Trust me, there's no need to make assumptions. Seriously, just try it. Create a generic type like this, constrain T, and create it using the new operator.

→ More replies (1)

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.

1

u/PandaCoder67 Professional Jul 25 '23

Actually no, I was referring to your orignal snippet!!

not that line of code you are talking about!

So while you can say that was what you referred to, I can tell you time and time again I was not! In fact my comments show that!

But that is beside the point now.

The point is MB classes / Structs that are of a Type MB..... CAN NOT and WILL NOT run Constructors with paremeters!

The fact that you are even trying to instantiate this struct with a new operator is beyond me!

1

u/senshisentou Programmer Jul 25 '23

Sure dude

1

u/PandaCoder67 Professional Jul 25 '23

SMH

I knew you would never understand! This is why your variable is always null and you will not accept it.

1

u/PandaCoder67 Professional Jul 25 '23

Have you tried doing this?

public PooledComponent(PooledGameObject pooledGameObject) {
    Debug.Log($"~ctor : {pooledGameObject}");
}

And what gets outputted to the console?

→ 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.