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

GenericSignal helper class #587

Merged
merged 16 commits into from
Dec 5, 2024

Conversation

samdeane
Copy link
Contributor

@samdeane samdeane commented Oct 24, 2024

See also #42

Instead of generating a helper class for each signal property, we use a single GenericSignal which can be specialised with the list of argument types that the signal contains.

@samdeane samdeane marked this pull request as ready for review October 28, 2024 15:41
@samdeane
Copy link
Contributor Author

Not sure the best way to comprehensively test this.

I think the one thing that might fail at runtime is the code that is unpacking the arguments to pass to the connect closure.

It is assuming that they can be constructed from a Variant. This should be true, but it's not the same code as was in the old generated helpers.

I've tested a bit with my own Godot project, but it would be good to try some others that make use of Godot-defined signals with parameters, and check that they work.

@samdeane samdeane force-pushed the generic-signal branch 2 times, most recently from 3e12f3d to 7ca7537 Compare October 29, 2024 13:07
@samdeane samdeane force-pushed the generic-signal branch 9 times, most recently from e64e458 to de2fa45 Compare October 31, 2024 12:37
@samdeane
Copy link
Contributor Author

I've cleaned up the formatting - the PR should now just contain the actual changes I've made.

@samdeane samdeane force-pushed the generic-signal branch 3 times, most recently from 95efcb3 to 2be97a8 Compare November 7, 2024 11:52
@@ -37,8 +37,8 @@ jobs:
steps:
- uses: compnerd/gha-setup-swift@main
with:
branch: swift-5.10-release
tag: 5.10-RELEASE
branch: swift-6.0.2-release
Copy link
Contributor Author

@samdeane samdeane Nov 21, 2024

Choose a reason for hiding this comment

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

@migueldeicaza I thought I'd try bumping the version right up to 6.0.2 here to see if the Windows compiler's argument pack support was fleshed out, as I remember you speculating that it might not be fully functional in 5.10 on Windows.

The tests pass so I guess that it is.

I guess it might be worth setting the swift-tools-version to 6.0 if we truly do need Swift 6 on Windows - but then we'd have to add an explicit opt-out of the Swift 6 language model for now, to avoid having to deal with that whole can of worms...

@samdeane
Copy link
Contributor Author

samdeane commented Nov 21, 2024

Note that with @elijah-semyonov's variant changes, I think the generic class has to deal with the possibility of one of the arguments passed to a signal-with-arguments could be nil.

I'm not sure if that's actually possible in reality? Since this code was written before that change was merged, for now I've just taken the simplest route and I throw an error for a nil argument, just like I would for one of the wrong type. I can probably eventually get nil arguments to work fine if they are needed, but I wanted to minimise the changes after rebasing, so it might be simpler to tackle it in a follow-up PR later.

@migueldeicaza
Copy link
Owner

The lack of support for nil is troubling, because there is a world where we would need to pass a nil that is being surfaced by Godot to our code.

One solution might be to bring back a Variant.Nil that conforms to VariantStorable so we can satisfy this signature:

    public func connect(flags: Object.ConnectFlags = [], _ callback: @escaping (_ t: repeat each T) -> Void) -> Object {

Or perhaps the only scenario we care about is passing an object that might be nil.

I think that we might be able to get away with creating such an object, and manually set the handle to nil, and users of that code would need to test for 'object.isValid'.

@migueldeicaza
Copy link
Owner

I am not a fan of 'object.isValid' approach, so perhaps we do need to introduce a Nil type, perhaps we can call it 'CallbackNil' to make it clear that it is to be generally shunned.

@samdeane samdeane force-pushed the generic-signal branch 2 times, most recently from 4f4776f to fc03df2 Compare December 5, 2024 14:38
@samdeane
Copy link
Contributor Author

samdeane commented Dec 5, 2024

The lack of support for nil is troubling

I've revised this so that nil arguments should be passed through to the callback properly.

@samdeane
Copy link
Contributor Author

samdeane commented Dec 5, 2024

After further discussion with @elijah-semyonov I've changed the generator as he suggested, so that primitive signal arguments are non-optional, but object ones are optional, in the actual signature of the GenericSignal.

Require macOS 14 for the generic pack support

Would bumping the swift-tools-version to 5.10 be better?

Use generic signal instead of generating helper

Don't use SimpleSignal

Moved GenericSignal to its own file. Removed some unused code, tidied comments.

Fixed popping objects.

Cleaner unpacking, without the need to copy the arguments.

revert unrelated formatting changes

put back SignalSupport to minimise changes

Removed SimpleSignal completely

tweaked names for a cleaner diff

Added test for built in signals
@migueldeicaza
Copy link
Owner

Thank you for nurturing this change.

Cheers!

@migueldeicaza migueldeicaza merged commit d9cc4f4 into migueldeicaza:main Dec 5, 2024
1 of 2 checks passed
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