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

Make opAssign return ref #1466

Closed
wants to merge 1 commit into from
Closed

Make opAssign return ref #1466

wants to merge 1 commit into from

Conversation

Bolpat
Copy link
Contributor

@Bolpat Bolpat commented Sep 1, 2016

Assignment should return the assigned object and not be void. The example is misleading.

Assignment should return the assigned object and not be void. The example is misleading.
@AndrejMitrovic
Copy link
Contributor

That's not true at all. Where are you getting this from?

@Bolpat
Copy link
Contributor Author

Bolpat commented Sep 2, 2016

Where are you getting this from?

From my experience with all C-like languages I know. One can always do x = y = z (except for no-copy types) and if ((a = f()) == b) { } for built-in types and usually for user defined types. I've been taught to define assignment to work like this and never different.

Maybe I've said it wrong. The result of assignment is (a reference to) the lvalue after being assigned to (and not the value being assigned as one could interpret the term assigned object). It should not be void. At least in Ali Çehreli's Book it is the assigned lvalue, but not a reference to it which is an error from what I know.

When I read the example the first time, I concluded the D compiler made the return this and ref implicit.

That's not true at all.

Give me a proper counterexample. Where is it useful not to return the lvalue after being assigned to?

I've taken a look through Phobos and found Nullable's opAssign that returns void. I also found BigInt's opAssign that does return BigInt. At least BitFlags's opAssign returns ref sometimes.

Interestingly, any C++ course teaches to define assignment as my_type& operator=(...) but in D this seems not to be the case. I'd really like to understand the reason to it, so I've asked this in the Forum.

@AndrejMitrovic
Copy link
Contributor

Returning ref can be useful for chaining assignments, sure. But it's not an actual requirement.

@Bolpat Bolpat closed this Sep 4, 2016
@Bolpat Bolpat deleted the patch-1 branch September 4, 2016 20:32
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.

2 participants