Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Inline initialisation for enum structs #872

Open
sirdigbot opened this issue Mar 1, 2023 · 6 comments
Open

[Feature Request] Inline initialisation for enum structs #872

sirdigbot opened this issue Mar 1, 2023 · 6 comments
Labels
compiler Problems with the compiler parsing source code. enhancement

Comments

@sirdigbot
Copy link

Very often when using enum structs, it's necessary to have an init() method to set all of the values:

enum struct ABC
{
    int a;
    int b;
    int c;

    void Init()
    {
        a = 1;
        b = 2;
        c = 3;
    }
}

This is not only clunkier to use

ABC x;
x.Init()

but it's also unnecessary code duplication, and introduces a potential mistake in that you can forget to call Init().
The alternative is far simpler and potentially safer:

enum struct ABC
{
    int a = 1;
    int b = 2;
    int c = 3;
}
@Wend4r
Copy link

Wend4r commented Mar 1, 2023

After SourcePawn 1.11, it is possible to do differently by

ABC x = {1, 2, 3};

@Wend4r
Copy link

Wend4r commented Mar 1, 2023

I use ABC::Init() for my decl optimizations (combined with a stack compression), otherwise you can do without it

@sirdigbot
Copy link
Author

sirdigbot commented Mar 1, 2023

After SourcePawn 1.11, it is possible to do differently by

ABC x = {1, 2, 3};

That's not a viable substitute because it requires you to specify the default values each time, which is not necessarily what you want.
It doesn't tell you which values correspond to which members, and if the enum struct were to change it could silently cause breakage.

@sirdigbot
Copy link
Author

I use ABC::Init() for my decl optimizations (combined with a stack compression), otherwise you can do without it

If you can do without the call to init then that's not the kind of use case I'm talking about here.

@Wend4r
Copy link

Wend4r commented Mar 2, 2023

It doesn't tell you which values correspond to which members, and if the enum struct were to change it could silently cause breakage.

I also foresee the need to use the name resolution engine (sizeof() and constant expressions). With the current spcomp structure, it would be easier to release it into something like a constructor. There is a principle in OOP that it is not always good to do complex fields initialization in the constructor (like create SM handle), it may be necessary to add an additional requirement only in a constant expression (default as 0).

@sirdigbot
Copy link
Author

A constructor might be an option but I feel like it's a more heavy-handed feature request than just "when I create an instance of an enum struct use different initial values or call an inlined init function to set them"

@peace-maker peace-maker added enhancement compiler Problems with the compiler parsing source code. labels Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Problems with the compiler parsing source code. enhancement
Projects
None yet
Development

No branches or pull requests

3 participants