Skip to content

Return primitive types by value from getters#12

Closed
Dushistov wants to merge 4 commits intojbaublitz:masterfrom
Dushistov:return-primitive-by-value
Closed

Return primitive types by value from getters#12
Dushistov wants to merge 4 commits intojbaublitz:masterfrom
Dushistov:return-primitive-by-value

Conversation

@Dushistov
Copy link
Copy Markdown
Contributor

No description provided.

Dushistov added 4 commits May 14, 2018 12:09
to reduce formating noise just use standard formating
This version used by serde, so it would be nice
to not have the same packages with different versions
as dependency.
@Boscop
Copy link
Copy Markdown
Collaborator

Boscop commented May 14, 2018

It wouldn't work for type aliases:

type Float = f32;

#[derive(Getters)]
struct Foo {
    #[get]
    x: Float
}

proc-macros can't know whether a field type is Copy (other than hardcoding it for primitives by name), but it would make sense to allow the user to specify that he wants a copying getter, by writing #[get = "*"] or #[get = "*pub"], as an easy way to remember that it's equivalent to dereferencing a reference, like instead of writing *foo.x() one writes foo.x() and #[get = "*"] or #[get = "*pub"].

With this syntax, it would be useable also for type aliases and non-primitive small structs like Point2d that should also be returned by value :)

@Dushistov
Copy link
Copy Markdown
Contributor Author

Dushistov commented May 14, 2018

@Boscop

Yes, it doesn't handle type aliases, also struct Generic<T> { #[get] x: T } / Generic<u32> is not handled.
But I suppose these cases are not part of this PR.
It minimize code writing for obvious case struct Foo { #[get] x: i32 },
other cases require syntax's invention (like you suggest #[get = "*"])
and may be discussion and I hope would be handled by different PR.

@Boscop
Copy link
Copy Markdown
Collaborator

Boscop commented May 14, 2018

I agree that it would be nice to have both: use copying by default for primitives, and still have the special syntax to be able to also use it for type aliases and non-primitive Copy types.

Btw, why are you using a HashSet+macro instead of a match statement to keep the code simple? :)

Copy link
Copy Markdown
Collaborator

@Hoverbear Hoverbear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is a neat refactor. I generally prefer such a big refactor to be done on its own, and not bundled in with a feature PR.

Could you open the refactor (ae15db6) as its own PR?

I'd rather review it (and merge it) seperately, and I feel like this PR will take awhile to properly spec out.

@Hoverbear Hoverbear mentioned this pull request May 14, 2018
@Dushistov
Copy link
Copy Markdown
Contributor Author

@Hoverbear

Could you open the refactor (ae15db6) as its own PR?

#13

@Hoverbear
Copy link
Copy Markdown
Collaborator

@Dushistov Thanks! :) Could you rebase this now?

@Dushistov
Copy link
Copy Markdown
Contributor Author

Thanks! :) Could you rebase this now?

Not sure about that, actually not sure that this PR is the way to go right now.

In my fork I modify getset to simplify my life further, it also
returns Option<primitive type> as copy.
And I have plans to replace -> &String with -> &str, Vec<X> -> &[X] and
-> &Option<X> with -> Option<&X>.
This saves a lot of typing, now I have to write such getters by hands.

And for me such getset behaviour is consistent semantics
because get methods written by human would have similar signature.

However, after all modifications I am still unhappy about tons of

#[get = "pub"]
#[set = "pub"]

that I have to write. So my plan (when I'd have some free time) is to discuss this problem
first, then write code and only after that return to this PR.

@Hoverbear
Copy link
Copy Markdown
Collaborator

@Dushistov Please, take your time. :) It'd be great to do this the right way!

@Boscop
Copy link
Copy Markdown
Collaborator

Boscop commented May 17, 2018

@Dushistov Yes, I agree with you, one way to reduce the amount of typing for #[get = "pub"] #[set = "pub"] is to automatically imply get when writing set (and to imply both when writing get_mut), I proposed this here: #4
Also, I'd prefer a shorter syntax for public than #[get = "pub"], e.g. #[Get], #[Set] and #[GetMut] for public, or #[_get] for private and #[get] for public.

@SOF3
Copy link
Copy Markdown

SOF3 commented Aug 26, 2019

Selectively having certain Copy types dereferenced and some not looks really confusing and inconsistent. Is it so hard to do a * on the return value?

@Hoverbear
Copy link
Copy Markdown
Collaborator

I think #29 is a simpler solution to this without any magic. What do you think?

@Hoverbear
Copy link
Copy Markdown
Collaborator

Closing this. :) Feel free to reopen if you'd prefer.

@Hoverbear Hoverbear closed this Oct 21, 2019
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

Successfully merging this pull request may close these issues.

4 participants