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

Nice to have: initialization of static arrays of builtin types #8

Closed
schveiguy opened this issue Oct 11, 2022 · 3 comments
Closed

Nice to have: initialization of static arrays of builtin types #8

schveiguy opened this issue Oct 11, 2022 · 3 comments

Comments

@schveiguy
Copy link
Contributor

schveiguy commented Oct 11, 2022

C:

int x[10] = {0};

D:

int[10] x = [0];

Results in a compiler error for size mismatch. Nicer would be:

int[10] x = 0;

This won't work in all situations, but the current behavior will never work.

@dkorpel
Copy link
Owner

dkorpel commented Oct 11, 2022

Results in a compiler error for size mismatch.

Only in function scope apparently:

void main()
{
	int[10] x = [0]; // error
}

int[10] x = [0]; // no error?

Kinda surprised.

But indeed, initializer translation needs work. In C, {0} can initialize almost anything. D is a lot more specific and picky with initializers.

@schveiguy
Copy link
Contributor Author

Only in function scope apparently

Huh, that's odd.

But I was actually thinking -- would it be possible to write an allZeros!T template that properly initializes any type? That could be used in its place. It might not be easy to do with CTFE.

Normally when translating, I have just been removing the initializer and using auto initialization. The one place where it is a problem is floating point types or char buffers.

@dkorpel
Copy link
Owner

dkorpel commented Oct 13, 2022

Fixed by recognizing {} and {0} on static arrays in 0f31729

Normally when translating, I have just been removing the initializer and using auto initialization. The one place where it is a problem is floating point types or char buffers.

I'm thinking that char/float members of structs should add a = 0; on the D side so S.init can be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants