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

GDScript function arguments with default values shouldnt have to be the last argument(s) #3135

Closed
balloonpopper opened this issue Aug 14, 2021 · 5 comments

Comments

@balloonpopper
Copy link

Describe the project you are working on

Tycoon game

Describe the problem or limitation you are having in your project

If I have a function where I specify a default value for an argument, all following arguments require a default value. I have a convention for my functions and this limitation means I need to reorder the parameters for all my functions to fit ones where I want to provide a default value.

Currently this is allowed
func MyFunc(a:int, b:int=20)

But this is not allowed
func MyFunc(a:int=50, b:int)
(Error message : "Parser Error: Default parameter expected.")

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

Any function argument with a default value would be able to be followed by one without a default value.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

This code in the editor
func MyFunc(a:int=50, b:int)

Will not cause the error : "Parser Error: Default parameter expected.")

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

It can be worked around by coding your functions differently, but the current limitation is an arbitrary limitation as far as I can see, and the behaviour (and current error message) aren't intuitive.

Is there a reason why this should be core and not an add-on in the asset library?

Its a core part of GDScript

@Calinou
Copy link
Member

Calinou commented Aug 14, 2021

I don't see how this is technically feasible, as this means the order of arguments would have to vary depending on the number of arguments specified when calling the function. I think allowing this kind of syntactic freedom where the argument order can differ between the definition and call is asking for trouble. Also, I haven't seen any other widespread programming language allow something like that.

@timothyqiu
Copy link
Member

For a function like:

func foo(a=1, b, c=2, d)

what should foo(1, 2, 3) result in?

@balloonpopper
Copy link
Author

Oops. I hadn't thought it through properly as I wasn't picturing the scenario you mentioned. Thinking through it further, I agree, you'd need to leave the parameter empty if you wanted to use one with a default value - so you couldd end up with a call like
foo(,2,,3)
to preserve the correct number of parameters, and that's not a good solution.

Lets amend my proposal to "Lets document that you need every parameter after one with a default value to also have a default value"

@AaronRecord
Copy link

I think what you really want is #1571 and #902

@YuriSizov
Copy link
Contributor

This is also closely related to #1117, which was closed per GDScript maintainer's comment.

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

5 participants