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

Implement abstract methods in GDScript #82987

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ryanabx
Copy link
Contributor

@ryanabx ryanabx commented Oct 8, 2023

Depends on #67777 .

This PR allows the @abstract annotation defined in the PR above to be used on methods. Abstract methods have the following properties:

  • They must exist inside an abstract class:
    image

  • They cannot have an implementation
    image

  • They MUST be implemented by subclasses
    image

Please test this and let me know your thoughts!

Below is a simple example project to use in testing:

test_project_abstract_methods.zip

@ryanabx ryanabx requested review from a team as code owners October 8, 2023 06:25
@dalexeev dalexeev added this to the 4.x milestone Oct 8, 2023
Copy link
Member

@dalexeev dalexeev left a comment

Choose a reason for hiding this comment

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

We also need to check that super() and super.method() do not refer to an abstract method.

modules/gdscript/gdscript_analyzer.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_parser.cpp Show resolved Hide resolved
modules/gdscript/gdscript_analyzer.cpp Outdated Show resolved Hide resolved
if (!p_function->get_datatype().is_hard_type() && p_function->body->get_datatype().is_set()) {
// Use the suite inferred type if return isn't explicitly set.
p_function->set_datatype(p_function->body->get_datatype());
} else if (p_function->get_datatype().is_hard_type() && (p_function->get_datatype().kind != GDScriptParser::DataType::BUILTIN || p_function->get_datatype().builtin_type != Variant::NIL)) {
if (!p_function->body->has_return && (p_is_lambda || p_function->identifier->name != GDScriptLanguage::get_singleton()->strings._init)) {
if (!p_function->is_abstract && !p_function->body->has_return && (p_is_lambda || p_function->identifier->name != GDScriptLanguage::get_singleton()->strings._init)) {
Copy link
Member

Choose a reason for hiding this comment

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

We need to check if there is code that relies on has_return and if this could lead to negative effects. I remember exactly that this is used in the compiler and in DocGen. Alternatively, we could add a fake return for abstract non-void functions, but this is an undesirable hack.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would one check for this? I'm still learning about the GDScript codebase :p

modules/gdscript/gdscript_parser.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_analyzer.cpp Outdated Show resolved Hide resolved
@dalexeev
Copy link
Member

dalexeev commented Oct 8, 2023

It should also be prohibited to mark constructor (_init()) and several multi-level methods (_notification(), _get_property_list(), ...) as abstract.

@ryanabx ryanabx force-pushed the features/abstract-methods branch 4 times, most recently from 7c06997 to 4d2d2e8 Compare October 9, 2023 01:20
@ryanabx
Copy link
Contributor Author

ryanabx commented Oct 9, 2023

I resolved most of the changes from you @dalexeev . I will need some help with the remaining steps you mentioned:

  • Prohibiting @abstract annotation on _init, and multi-level function calls _notification(), _get_property_list(), etc.)
  • Checking if there is code that relies on has_return, per your comment
  • Making sure that super() and super().method don't refer to an abstract method

modules/gdscript/gdscript_analyzer.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_analyzer.cpp Outdated Show resolved Hide resolved
@ryanabx ryanabx force-pushed the features/abstract-methods branch from bd03c7a to c380cc2 Compare October 18, 2023 19:14
@InkONat
Copy link

InkONat commented Feb 19, 2024

It's been a while since anything was said about this PR...

@ryanabx
Copy link
Contributor Author

ryanabx commented Feb 19, 2024

It's been a while since anything was said about this PR...

This PR depends on #67777

Once it's merged, I will probably come back to it and update the PR to accommodate any changes made since my last push.

@leandro-benedet-garcia
Copy link

leandro-benedet-garcia commented May 26, 2024

I think since the required pr uses keywords to declare abstract classes, you should also use keywords to declare abstract functions

@AThousandShips
Copy link
Member

AThousandShips commented May 26, 2024

This was not originally the decision, so this simply follows the original design, for more details see #67777 (comment), but see also #82987 (comment), this is waiting on another PR

@leandro-benedet-garcia
Copy link

This was not originally the decision, so this simply follows the original design, for more details see #67777 (comment)

Yes, so shouldn't this PR also be changed?

@ryanabx
Copy link
Contributor Author

ryanabx commented May 26, 2024

This was not originally the decision, so this simply follows the original design, for more details see #67777 (comment)

Yes, so shouldn't this PR also be changed?

Yes, but I will work on it once the abstract classes PR gets merged 😅

@dalexeev
Copy link
Member

Yes, so shouldn't this PR also be changed?

It should, but it depends on the desire and availability of the author; we cannot require volunteers to work. It also makes sense to wait until the base PR is at least reviewed and approved, so as not to ask the author of this PR to do extra work.

@leandro-benedet-garcia
Copy link

Yes, so shouldn't this PR also be changed?

It should, but it depends on the desire and availability of the author; we cannot require volunteers to work. It also makes sense to wait until the base PR is at least reviewed and approved, so as not to ask the author of this PR to do extra work.

Ah, sorry for my phrasing, I was just asking if it should, not that it needs to.

@dalexeev dalexeev mentioned this pull request Oct 1, 2024
12 tasks
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 GDScript support for defining virtual/prototype/must-override methods in base classes
7 participants