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 more array methods: find_custom, rfind_custom #80208

Closed
wants to merge 1 commit into from
Closed

Add more array methods: find_custom, rfind_custom #80208

wants to merge 1 commit into from

Conversation

SlashScreen
Copy link
Contributor

@SlashScreen SlashScreen commented Aug 3, 2023

Added 3 new array functions - count_custom, first_custom, and last_custom. These perform the functions of the existing count,, first, and rfind respectively, but using a Callable as the conditions, like sort_custom.
Last could be renamed rfind_custom for consistency sake.

@SlashScreen SlashScreen requested a review from a team as a code owner August 3, 2023 08:22
@AThousandShips
Copy link
Member

AThousandShips commented Aug 3, 2023

Please fix the message of your commit

@AThousandShips
Copy link
Member

AThousandShips commented Aug 3, 2023

These need to be added to the binds for them to be useful in scripting

count_custom seems excessive, can easily be worked around with reduce (the same argument that rejected adding min/max_custom, in favor of documentation)

@AThousandShips AThousandShips changed the title pushed to wrong branch. Whoopsie. Add more array methods: first_custom, last_custom, count_custom Aug 3, 2023
@@ -353,6 +353,36 @@ int Array::find(const Variant &p_value, int p_from) const {
return ret;
}

int Array::first_custom(const Callable& p_callable, int p_from = 0) const {
Copy link
Member

@AThousandShips AThousandShips Aug 3, 2023

Choose a reason for hiding this comment

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

This name feels misleading, using first makes it sound like it returns that value, not the index, just like suggested on the previous PR I'd say find_custom and rfind_custom

@SlashScreen
Copy link
Contributor Author

You're right, I didn't think about using reduce in that way. I can remove that. Should I rename last_custom to rfind_custom?
As for the first ambiguity, that too is correct, but does saying find_custom make it sound like it does the same thing as any? Or no, because any returns a bool.
Also, yeah, not sure how I thought it would work without binding. I couldn't find where it was bound before, but after another look, I found it.

@dalexeev
Copy link
Member

dalexeev commented Aug 3, 2023

Should I rename last_custom to rfind_custom?

I think it should be find_custom and rfind_custom, not first_custom and last_custom, for consistency with the already existing naming scheme.

I couldn't find where it was bound before, but after another look, I found it.

/* Array */
bind_method(Array, size, sarray(), varray());
bind_method(Array, is_empty, sarray(), varray());
bind_method(Array, clear, sarray(), varray());

Don't forget the docs (XML files in the doc/classes folder). You can use bin/<godot_executable> --doctool to generate an empty method descriptions and fill it later.

Also, you should consider adding tests for the methods.

@SlashScreen
Copy link
Contributor Author

How would I create a callable for the tests? I don't see precedent for this, though I could be missing something.

@dalexeev
Copy link
Member

dalexeev commented Aug 3, 2023

How would I create a callable for the tests?

I think you can use callable_mp(&object, &Class::method):

target.connect("my_custom_signal", callable_mp(&object, &Object::notify_property_list_changed));

or callable_mp_static(&Class::static_method):

static void static_callable_test() {
counter[0].sub(2);
}

tasks2[i] = WorkerThreadPool::get_singleton()->add_task(callable_mp_static(static_callable_test), !low_priority);

@AThousandShips
Copy link
Member

AThousandShips commented Aug 3, 2023

Or add it as a gdscript test under modules/gdscript/tests/scripts/runtime/features

@dalexeev
Copy link
Member

dalexeev commented Aug 3, 2023

Or add it as a gdscript test under modules/gdscript/tests/scripts/runtime/features

I think it's less preferable, GDScript tests are for testing the GDScript implementation, not the core.

@SlashScreen
Copy link
Contributor Author

How would I create a callable for the tests?

I think you can use callable_mp(&object, &Class::method):

target.connect("my_custom_signal", callable_mp(&object, &Object::notify_property_list_changed));

or callable_mp_static(&Class::static_method):

static void static_callable_test() {
counter[0].sub(2);
}

tasks2[i] = WorkerThreadPool::get_singleton()->add_task(callable_mp_static(static_callable_test), !low_priority);

Ah, so I can use a c++ function as a callable?

@dalexeev
Copy link
Member

dalexeev commented Aug 3, 2023

Ah, so I can use a c++ function as a callable?

Yes.

@SlashScreen SlashScreen requested review from a team as code owners August 3, 2023 10:22
@SlashScreen
Copy link
Contributor Author

I hope I amended it correctly.
I added the test, removed count custom, added some documentation, and added some tests.

@YuriSizov YuriSizov changed the title Add more array methods: first_custom, last_custom, count_custom Add more array methods: find_custom, rfind_custom Aug 3, 2023
@YuriSizov
Copy link
Contributor

YuriSizov commented Aug 3, 2023

Please also make sure that the commit message refers to the actual names of the methods.

Personally, I agree that these should be variants of find. first_custom and last_custom don't make sense semantically. So the current state of this PR seems fine to me. Provided we agree to add these at all, of course.

@SlashScreen
Copy link
Contributor Author

Fixed typo and comment style nitpick.

@SlashScreen
Copy link
Contributor Author

If there's any more changes that need be made, I will address them tomorrow, as it is 3:30 in the morning and my brain is shutting down.
Could someone double-check that I created the callable properly in the tests?

@RedMser
Copy link
Contributor

RedMser commented Aug 3, 2023

The method documentation should include that the func callable only gets called with one parameter: once for each element in the array. See the documentation of map, since I think it's a lot clearer.
E.g. you could otherwise assume it also includes the index as a second parameter or something (which I kinda wish for, but this needs a separate proposal and PR).

@SlashScreen
Copy link
Contributor Author

Resolved typos and style checks from @AThousandShips .

@SlashScreen SlashScreen requested a review from YuriSizov August 4, 2023 02:31
@SlashScreen
Copy link
Contributor Author

Whoops, I messed up the test. That's what I get for having nvim in a thin window where I can't see the whole line.

@YuriSizov
Copy link
Contributor

You still need to amend the commit message to reflect what the methods are actually called.

@YuriSizov YuriSizov removed their request for review August 4, 2023 16:53
@SlashScreen
Copy link
Contributor Author

nice catch.

@SlashScreen SlashScreen requested a review from dalexeev August 5, 2023 07:11
Copy link
Member

@AThousandShips AThousandShips left a comment

Choose a reason for hiding this comment

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

Looks good save for some corrections to the documentation and style

@YuriSizov YuriSizov modified the milestones: 4.x, 4.2 Sep 15, 2023
@SlashScreen
Copy link
Contributor Author

Integrated the changes, @AThousandShips .

@AThousandShips
Copy link
Member

Turns out there's already a PR for this change, predating it:

But this one is more refined IMO

@SlashScreen
Copy link
Contributor Author

oh dear, I'm not sure i updated the documentation for the other function... Will the checks need to be run again?

@AThousandShips
Copy link
Member

They do, but no worries just go ahead and fix them

@maximkulkin
Copy link
Contributor

This one is exactly same as #73490 minus naming with an exception that the other one has much more comprehensive test suite.

@SlashScreen
Copy link
Contributor Author

So is mine redundant?

@AThousandShips
Copy link
Member

Will be a decision which one to merge

@SlashScreen
Copy link
Contributor Author

Changed semantics on rfind_custom().

@AThousandShips
Copy link
Member

@YuriSizov YuriSizov modified the milestones: 4.2, 4.3 Oct 2, 2023
@akien-mga akien-mga modified the milestones: 4.3, 4.x Jun 12, 2024
@SlashScreen SlashScreen closed this by deleting the head repository Aug 11, 2024
@AThousandShips AThousandShips removed this from the 4.x milestone Aug 11, 2024
@Calinou
Copy link
Member

Calinou commented Aug 13, 2024

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.

Add more array methods: first_custom, last_custom, count_custom
8 participants