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

Multiple inheritance (mixins) in gdscript #1093

Closed
me2beats opened this issue Jun 20, 2020 · 5 comments
Closed

Multiple inheritance (mixins) in gdscript #1093

me2beats opened this issue Jun 20, 2020 · 5 comments

Comments

@me2beats
Copy link

Describe the project you are working on:
I am developing gdscript plugins that I am going to support for a long time.

I am convinced that code support requires code purity, as well as keeping the code base as small as possible (without enthusiasm of course), in particular adherence to the DRY principle.

Describe the problem or limitation you are having in your project:
Say, I want to have a class A that contains 2 independent code parts:
one part overrides the add_child method
the other part overrides _to_string method.

class A:
    extends Node
    func add_child(node, unique):
        #do something

    func _to_string(s):
        #do something else

and I also notice that I could reuse these code parts separately for other classes.

but I can't do it easily in gdscript
Using composition also can't solve this problem.

Describe the feature / enhancement and how it helps to overcome the problem or limitation:

Multiple inheritance, particularly python like mixins, could elegantly solve this problem

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

I could have those code parts in 2 different classes (scripts).
and be able to "mixin" them with the main class.

class AddChild:
    extends Node
    func add_child(node, unique):
        #do something

class ToString:
    extends Object

    func _to_string(s):
        #do something else


class A:
    extends ToString, AddChild

#or

class A:
    extends ToString, AddChild, Node

If this enhancement will not be used often, can it be worked around with a few lines of script?:

no unfortunately.
This is not solved by composition or workaround as elegantly as with mixins.

Is there a reason why this should be core and not an add-on in the asset library?:
it should not be implemented as an addon.
Imo it's more logical to have this as a feature of the gdscript language.

@Calinou
Copy link
Member

Calinou commented Jun 20, 2020

Duplicate of #758.

@me2beats
Copy link
Author

me2beats commented Jun 20, 2020

Imo this is not a duplicate.

at least I don’t see how this could solve the problem above.

I quickly googled traits, and I got the impression that they are not able to do everything that python mixins can do

In this case, should I rename proposal to emphasize that I mean python-like mixins, not traits?

@Calinou
Copy link
Member

Calinou commented Jun 20, 2020

@me2beats Multiple inheritance will probably never be added to GDScript. A trait system is much more likely to be added in the future.

@me2beats
Copy link
Author

Where can I find out the reasons for this decision?
Is it difficult to implement or is it a desire to protect the user from writing confusing code, or is there an assumption that this will be rarely used?
or something else?

@Calinou
Copy link
Member

Calinou commented Jun 20, 2020

There have been a few claims here and there that multiple inheritance would be contrary to GDScript's goals of simplicity. Like this part in the documentation: https://docs.godotengine.org/en/latest/getting_started/scripting/gdscript/gdscript_advanced.html#duck-typing

For the record, GDScript is being rewritten from scratch for Godot 4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants