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

Toggling explicit struct initialization per struct via attribute #1605

Open
kulkalkul opened this issue Nov 8, 2024 · 5 comments
Open

Toggling explicit struct initialization per struct via attribute #1605

kulkalkul opened this issue Nov 8, 2024 · 5 comments
Labels
Discussion needed This feature needs discussion to iron out details Enhancement Request New feature or request
Milestone

Comments

@kulkalkul
Copy link

ZII is perfect for some tasks, but sometimes I know that given struct is always initialized. So, adding new fields to a struct definition adds possibility of breaking this guarantee as uninitialized fields can go unnoticed. It is possible to overcome this using initialization functions, but I find that it becomes necessity for such structs, makes those functions constructor-like. I also enjoy using struct initialization when possible.

As a solution, an attribute can be added for each struct initialization literal (pardon the term if wrong).

Struct definition (nothing special here):

struct Foo {
  int a;
  int b;
  int c;
  int d;
}

Expression:

@nopartial Foo {
  .a = 5,
  .b = 2,
  .d = 1,
  // compile error, as c is forgotten
}

Attribute at initialization location would allow same struct to be used with both ZII fields and explicit initialization, while preventing hard to follow bugs within big codebases.

@lerno
Copy link
Collaborator

lerno commented Nov 8, 2024

I am not sure how you mean the initializer, that is in no way correct C3.

@lerno lerno added Enhancement Request New feature or request Discussion needed This feature needs discussion to iron out details labels Nov 8, 2024
@kulkalkul
Copy link
Author

kulkalkul commented Nov 8, 2024

Foo foo = Foo { .a = 5, };

I'm talking about the part in right hand side. So, the above code would work as-is

Foo foo = Foo { .a = 5, } @nopartial;

This would error out as fields b, c, and d are missing (I don't know where is the good place for nopartial). So, this allows user to choose whether they want it partial or not per struct "initialization"

@lerno
Copy link
Collaborator

lerno commented Nov 16, 2024

That is indeed the right location from a parsing point of view. I only have some doubts as to whether it should be included. C has had .foo = 5 initialization for ages, so is there some similar attribute in GCC?

@lerno lerno added this to the 0.6.6 milestone Nov 16, 2024
@kulkalkul
Copy link
Author

kulkalkul commented Nov 17, 2024

I don't know, I'm unfortunately not a C programmer 😅 It was something that annoyed me while using Odin, that's why I asked about whether something similar exists in C3 (on discord), which prompted me to create the issue. I read that C3's goal is to stay compatible with C as much as it can, that's also the theme I get from your comment above; so in that case, I agree that it'd probably feel out of place for the language

@lerno
Copy link
Collaborator

lerno commented Nov 17, 2024

Adding attributes are in general fine. I just want to make sure all features are used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion needed This feature needs discussion to iron out details Enhancement Request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants