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

[PoC] In-place struct initialization #8460

Closed
wants to merge 2 commits into from
Closed

Conversation

wilzbach
Copy link
Member

@wilzbach wilzbach commented Jul 6, 2018

So this is my WIP hack to have in-place struct initialization into DMD.
As during the parsing stage we don't have much information about the struct and can only parse a StructInitializer, I created an intermediate StructLiteralExp2 that will be converted into a StructLiteralExp during expression semantic.
I would like to have a more elegant solution and this still needs a lot of work, but it already passes the test suite for the common cases of this DIP and I thought it's good to show that it's possible to do this in dmd for submitting the DIP. Also I get to see what the CIs say.

What's in-place struct initialization?

Full DIP: dlang/DIPs#71

tl;dr:

struct StructFoo
{
    int a;
}

void bar(StructFoo foo)
{
    assert(foo.a == 3);
}

StructFoo f = {a: 1};
f = StructFoo({a: 2});
assert(f.a == 2);
bar(StructFoo({a: 3}));

@wilzbach wilzbach added Review:WIP Work In Progress - not ready for review or pulling Review:Pending DIP Approval labels Jul 6, 2018
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @wilzbach!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + dmd#8460"

@jacob-carlborg
Copy link
Contributor

Why do you need a new expression type?

@RazvanN7
Copy link
Contributor

RazvanN7 commented Jul 6, 2018

Can't you just use the existing StructLiteralExp and add the fields fields you want + a bool field that tells you if StructLiteralExp is used for in-place initialization?

@andralex
Copy link
Member

status?

@jacob-carlborg
Copy link
Contributor

status?

@andralex there's a DIP that hasn't been approved. Personally I don't think a DIP is needed. But we seem to disagree on the syntax.

@wilzbach
Copy link
Member Author

status?

This is a working PoC, but there are multiple ways to integrate this feature in the D grammar (see the DIP for more details) and as there was never a consensus on which syntax to use. @andralex what are your thoughts on the syntax for this?
Once that's decided, polishing & merging this would be easy...

@wilzbach wilzbach force-pushed the struct2 branch 2 times, most recently from 4837f97 to 8ffce92 Compare December 31, 2018 12:38
@dkorpel
Copy link
Contributor

dkorpel commented May 10, 2023

This has been superseded by named arguments, which have been implemented for struct constructors.

@dkorpel dkorpel closed this May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants