Extension methods and struct extends for inheritance

Started by eri0o, Wed 11/12/2024 13:38:02

Previous topic - Next topic

eri0o

This is more to kick the ball on something...

Currently if you have a managed struct that extends another managed struct, and extension methods on the parent managed struct, the struct that extends it doesn't get the extensions from the first.

Should it get? Should it not get? What is the expected behavior here?

Snarky

My expectation would be that it should. For example, a Label is a GUIControl, so any method available for GUIControl should be available for Label.

Crimson Wizard

#2
Not only it should, but it already works.

Compiler already supports pointer upcast: you can pass subclass pointer to a function that expects base class pointer.
Therefore, an extension method is able to accept child pointer as well.

Code: ags
struct MyStruct
{
  int a;
};

struct MyChildStruct extends MyStruct
{
  int b;
};

function Extender(this MyStruct*)
{
}

function game_start()
{
  MyStruct x;
  x.Extender();
  MyChildStruct y;
  y.Extender();
}

Above compiles and runs fine with ags3 compiler.

So the problem is only with Autocomplete not working as it should again.

eri0o

Aaahh, I didn't knew it was the autocomplete only, I need to make a small test - probably will steal your code there - and look into where it is wrong. Thanks!

SMF spam blocked by CleanTalk