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

What else can it do? #2

Closed
Milo123459 opened this issue May 1, 2021 · 13 comments
Closed

What else can it do? #2

Milo123459 opened this issue May 1, 2021 · 13 comments

Comments

@Milo123459
Copy link

Milo123459 commented May 1, 2021

Would be cool if we could have something like:

aws: Default::default(),
battery: Default::default()

which autodefault could solve I suppose. Maybe making it so you could do:

aws,
battery

etc.

@zetanumbers
Copy link
Contributor

This would conflict with field punning.

@zetanumbers
Copy link
Contributor

aws: aws or aws: Default::default()

@Milo123459
Copy link
Author

I don't mind, it just might be a cool feature

@zetanumbers
Copy link
Contributor

zetanumbers commented May 2, 2021

We might use ..[foo, bar] syntax for that, it seems to not conflict with anything really.

@Milo123459
Copy link
Author

Alright, cool

@Lucretiel
Copy link
Owner

I'd be more into this if there was a way to make it automatically fill in defaults for all remaining fields:

{
    a: false,
    b: 0,
    c: "",
    d: None,
    e: 10
}

would be

{
    e: 10,
    ??
}

But the main reason I want this is for implementing default on large structs, so I'm not seeing another good way to do it

@Milo123459
Copy link
Author

All good ideas! I don't really mind

@zetanumbers
Copy link
Contributor

zetanumbers commented May 3, 2021

{
    e: 10,
    ??
}

Uh, i wish that was possible. Sadly syn crate is not that flexible. We would have to reimplement the entire parser for this as far as i am aware.

Function-like macros are the only way i can think of that would be maintainable.

@Milo123459
Copy link
Author

Ideally I think what having the usage being
{
??
}
would be best.

@zetanumbers
Copy link
Contributor

zetanumbers commented May 3, 2021

There is one issue, that aims to simplify writing Default::default() to default(). I think we could demangle second one into first as a bonus.

@Lucretiel
Copy link
Owner

Lucretiel commented May 3, 2021

There is one issue, that aims to simplify writing Default::default() to default(). I think we could demangle second one into first as a bonus.

I mean, we could, but why bother? The purpose of the "naked default" change is as a convenience for code authors; proc macros like autodefault can prefer to write extremely verbose code that doesn't risk accidentally experiencing a name collision, and don't need to worry so much about emitting concise code. You'll notice that in the actual code, we emit ::core::default::Default::default(), to ensure that there are no issues if the user has their own local items called default or Default.

@zetanumbers
Copy link
Contributor

zetanumbers commented May 3, 2021

Yes, i was silly there actually. We could just add a helper function named default(), it's has very simple one way to implement it. But that would require having two crates: marcos implementations and helper functions. I think it could be useful to have these "default utilities" as one dependency tho.
So i thought if we would detect use core::default::default at the root of the function's body, then substitute default() with Default::default(), but then i realized that would be doing compiler's job lol.

@zetanumbers
Copy link
Contributor

Edit above: made my saying clearer

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

3 participants