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

Add merged method to allow returning Dictionary after merging #65526

Merged

Conversation

KoBeWi
Copy link
Member

@KoBeWi KoBeWi commented Sep 8, 2022

Dictionary merge() is void right now, but it could return itself, so you can chain it or easily use as return value etc.

@KoBeWi KoBeWi added this to the 4.x milestone Sep 8, 2022
@KoBeWi KoBeWi requested review from a team as code owners September 8, 2022 16:18
@akien-mga
Copy link
Member

akien-mga commented Sep 8, 2022

Hm that's not a common pattern in our API. Usually if it returns something it would be merged().
One could make the same case for append() and many other core APIs.

@KoBeWi
Copy link
Member Author

KoBeWi commented Sep 8, 2022

Well I opened this based on how I use my old utility method (I had a GDScript implementation before it was in core). tbh the method would be as useful even if it returned a new Dictionary; I think I originally made it modify the Dictionary, because it's cheaper than copying it (and Dictionary doesn't have any methods that return a new Dictionary).

Tweens have similar API (i.e. it allows chaining method calls on the same object), so it's not unheard of. I just wanted to replace the old method in my project with the new merge() and realized it lacks an important aspect, hence this PR >_>

One could make the same case for append() and many other core APIs.

Well if there is use, they could. Dictionary.merge() is useful for returning a Dictionary with defaults (as I gave example in the doc).

@KoBeWi KoBeWi force-pushed the {}.merge({}).merge({}).merge({}) branch from 7358b83 to 764c127 Compare September 8, 2022 19:11
Copy link
Contributor

@Mickeon Mickeon left a comment

Choose a reason for hiding this comment

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

I know this is literally two years old but I do agree with the sentiment of Akien of the past.

@KoBeWi
Copy link
Member Author

KoBeWi commented Feb 25, 2024

And I still think some sort of merge and return is useful. I could make it a new method.

@Mickeon
Copy link
Contributor

Mickeon commented Feb 25, 2024

Mhm!

@KoBeWi
Copy link
Member Author

KoBeWi commented Feb 25, 2024

Should it return a new Dictionary or is it ok if it modifies and returns the same one?

@Mickeon
Copy link
Contributor

Mickeon commented Feb 25, 2024

Methods such as Vector2's rotated & normalized return a whole new Vector2. So I feel like a theoretically merged should return a new Dictionary as well, perhaps noting that comes at a cost.

@KoBeWi KoBeWi force-pushed the {}.merge({}).merge({}).merge({}) branch from 764c127 to 43b2057 Compare February 25, 2024 21:42
@KoBeWi
Copy link
Member Author

KoBeWi commented Feb 25, 2024

Pushed as a new method.
I also slipped a fix for merge() 🙃

@KoBeWi KoBeWi force-pushed the {}.merge({}).merge({}).merge({}) branch from 43b2057 to 3502a40 Compare February 25, 2024 21:44
Copy link
Contributor

@Mickeon Mickeon left a comment

Choose a reason for hiding this comment

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

In my opinion the first parameter could be called with, although unfortunately that'd not be consistent with merge, so hmm.

<param index="0" name="dictionary" type="Dictionary" />
<param index="1" name="overwrite" type="bool" default="false" />
<description>
Creates a new Dictionary, which is a duplicate of this Dictionary merged with the other [param dictionary]. Useful for quickly making Dictionaries with default values.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Creates a new Dictionary, which is a duplicate of this Dictionary merged with the other [param dictionary]. Useful for quickly making Dictionaries with default values.
Returns a copy of this dictionary merged with the dictionary [param dictionary]. Useful for quickly making dictionaries with default values.

Copy link
Member Author

Choose a reason for hiding this comment

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

It sounds awkward either way.
"with other dictionary dictionary"
"with other dictionary with"

Copy link
Contributor

Choose a reason for hiding this comment

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

It's about making it easier to translate, as someone did mention to avoid "punning" for that reason.

doc/classes/Dictionary.xml Outdated Show resolved Hide resolved
@KoBeWi KoBeWi force-pushed the {}.merge({}).merge({}).merge({}) branch 2 times, most recently from 788770c to b1694e3 Compare February 25, 2024 23:01
Copy link
Contributor

@Mickeon Mickeon left a comment

Choose a reason for hiding this comment

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

Commit name needs to be changed based on what we discussed above.

Dare I say this method can be nicer to use than merge. Mhm... I wonder who's the silly billy that even added the orig-...
image

doc/classes/Dictionary.xml Outdated Show resolved Hide resolved
@KoBeWi KoBeWi force-pushed the {}.merge({}).merge({}).merge({}) branch from b1694e3 to d68b7c3 Compare February 26, 2024 19:31
@Mickeon Mickeon self-requested a review February 27, 2024 19:46
@Mickeon Mickeon changed the title Make Dictionary.merge() return Dictionary Add merged method to allow returning Dictionary after merging Mar 2, 2024
Copy link
Contributor

@Mickeon Mickeon left a comment

Choose a reason for hiding this comment

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

This has actually been a-okay for a while, at least documentation wise. I just forgot to approve of it.

Now it's a matter of determining if this is desirable or not (probably yes).

doc/classes/Dictionary.xml Outdated Show resolved Hide resolved
@KoBeWi KoBeWi force-pushed the {}.merge({}).merge({}).merge({}) branch from d68b7c3 to eb0a624 Compare March 6, 2024 13:49
Copy link
Member

@adamscott adamscott left a comment

Choose a reason for hiding this comment

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

Let's merge! *starts fusion dance*

@akien-mga akien-mga removed this from the 4.x milestone Mar 6, 2024
@akien-mga akien-mga added this to the 4.3 milestone Mar 6, 2024
@akien-mga akien-mga merged commit 4b20d0d into godotengine:master Mar 6, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks!

@KoBeWi KoBeWi deleted the {}.merge({}).merge({}).merge({}) branch March 6, 2024 23:36
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.

5 participants