-
-
Notifications
You must be signed in to change notification settings - Fork 902
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
enhancement: make the Component.add
, Component.addToParent
and Component.remove
methods generic
#1689
Comments
Do we need it on |
The The
You still might want to have some other type of logic there, it wouldnt do any harm adding it on that method as well. |
By adding it to
Same here.
When you say logic, do you mean restrictions? Logic I feel would be type checking that is done directly on the object and not checks on T? Can you put up some example usecases? I think maybe the whole |
I am failing to understand how adding a generics would to these methods would give more control or make it easier to enforce rules. Virtually adding the generics But maybe I am missing something so a use case here would be very important to continue the discussion. |
Let's say you have a use case where a certain component can only hold one instance of any other component as it's child. To be able to do that you would need to be able to specifically filter down using it's type. A small example of the above use case could, with generics, look like this: @override
Future<void>? add<T extends Component>(T component) {
assert(children.whereType<T>().isEmpty, 'The parent already has a component of type $T');
return super.add(component);
} This wouldn't be possible without generics unless you provide a new method. Which in my case would not be preferred. |
There is nothing stopping us from also having
Logic is whatever the user wants to do, it can be constraints, lookups or whatever. The fact that you have that extra data is just the added addition.
I just posted one above! But I think each method should still have their own generic. On the class itself makes no sense imho as that would imply that only that |
That is not at all how I imagined that it would be used, since that would be a runtime error, and that runtime error can just as well be done by type checking today: @override
Future<void>? add(Component c) {
assert(children.where((c2) => c.runtimeType == c2.runtimeType).isEmpty, 'The parent already has a component of type ${c.runtimeType}');
return super.add(component);
} What I imagined was using T to restrict what types that can be added to a component by setting a stricter generics when overriding (which is not your usecase), so your usecase is what meant by "logic" in my other comment. I'm not sure I like this way of using generics. |
I am not sure if that is true, I haven't tested but I think this would work: @override
Future<void>? add(Component component) {
assert(
children.where((child) => child.runtimeType == component.runtimeType).isEmpty,
'The parent already has a component of type ${child.runtimeType}',
);
return super.add(component);
} |
Using But even tho this example could be rewritten like this I don't think that should be a reason to not add these generics typings. As they come with zero overhead and would provide much more valuable data to anyone needing that on the methods (or component).
|
Generics have costs though, don't they? When you have a method like Looking at dart-lang issue that you linked, there seem to be some comments that indicate that accessing Lastly, I believe @erickzanardo's solution for the problem posed (i.e. how to ensure that a component can have only children of distinct types) is more correct. For example, suppose I have a Of course, there is another bug with both solutions: the |
Problem to solve
As a developer I want to have generic information of component being added or removed, so that I can add extra validation steps if I do so require.
While this feature won't be used in most games, it can be quite crucial for third-party Flame packages developers as it gives them the extra control to enforce certain rules that their package might require, for instance a custom ECS build on top of the existing FCS.
Proposal
My proposal is to add generic parameters to the following methods:
Component.add
Component.remove
Component.addToParent
Component.changeParent
This would be a breaking change as the method definition will change, and anyone who was already overwriting these methods will have to make changes to reflect the new definition.
The text was updated successfully, but these errors were encountered: