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

Use default value for an argument after , #1117

Closed
MaaaxiKing opened this issue Jun 24, 2020 · 21 comments
Closed

Use default value for an argument after , #1117

MaaaxiKing opened this issue Jun 24, 2020 · 21 comments

Comments

@MaaaxiKing
Copy link

Describe the project you are working on:
Reaction game
Describe the problem or limitation you are having in your project:
I have to give the default value of the first argument if I want to pass a value that is different from the second argument's default.
Describe the feature / enhancement and how it helps to overcome the problem or limitation:
You could step over the argument and only pass a value to the next one.
Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

func _ready():
	my_function("vvv", "mmm")
	my_function("passed to first argument")
	my_function(, "passed to second argument")

my_function(arg1 = "hshaf", arg2 = "ldvm"):
	print(arg1, arg2)

This would print
vvv
mmm
passed to first argument
ldvm
hshaf
passed to second argument
If this enhancement will not be used often, can it be worked around with a few lines of script?:
I don't think so.
Is there a reason why this should be core and not an add-on in the asset library?:
I think an add-on can't change/add etc. something to the language.

@bojidar-bg
Copy link

The problem/limitation discussed is likely the same one as #902.

@Calinou
Copy link
Member

Calinou commented Jun 24, 2020

This feature might look confusing at a first glance to people coming from other languages. I can't think of any popular language which allows this.

@SIsilicon
Copy link

@Calinou I personally think this is a pretty neat idea, regardless of what other languages do.
Besides, isn't this syntax similar to what is done with a setter and getter?

var set_and_get setget set, get
var set_only setget set
var get_only setget , get

@Calinou
Copy link
Member

Calinou commented Jun 25, 2020

@SIsilicon Yes, but that setget syntax is going away in 4.0: godotengine/godot#39093

@KoBeWi
Copy link
Member

KoBeWi commented Jun 25, 2020

I can't think of any popular language which allows this.

You can do this in Javascript, but with undefined keyword, not blank space.

@MaaaxiKing
Copy link
Author

MaaaxiKing commented Jun 25, 2020

I've also had thaughts about the setget similarity.
But one big question I have is why does a language must have similarities to other ones? Why won't it just be made better no matter if other languages support this, it could be the first language with that!

@Calinou
Copy link
Member

Calinou commented Jun 25, 2020

But one big question I have is why does a language has similarities to other ones? Why can't it just be made better no matter if other languages support this or if it would be the first language?!

Most people using Godot have experience with at least one other programming language. Breaking expectations means they have to "unlearn" things they've learned in other languages. We'd prefer to keep this to a minimum. For reference, several modern languages like V or Zig are precisely built around this idea. This doesn't mean we should never attempt to add our own things, but we need to weigh the benefits and downsides when doing so.

In theory, this feature doesn't hurt so much as you can't accidentally use it. But in practice, I think it looks confusing at a first glance. If you're introducing new Godot developers to your team, they might wonder what's going on when looking at your scripts 🙂

I'm also not sure how often this feature would be needed in practice, especially if we end up getting support for keyword arguments in GDScript.

@MaaaxiKing
Copy link
Author

I can understand that but if all language programmers would make just little differences to other languages, you could then just not program a new one.

@YuriSizov
Copy link
Contributor

You can do this in Javascript, but with undefined keyword, not blank space.

An explicit keyword is definitely better than just an empty space (I know that undefined is not a keyword in JS, it's actually problematic there). It's hard to notice a comma, it can look like an error or typing mistake. So if we want a way to explicitly request "use a default value from the function declaration" then I propose default or some other descriptive keyword to be used here.

So the example would be:

my_function(default, "passed to second argument")

@MaaaxiKing
Copy link
Author

@pycbouh, I agree but what if you have a variable named default?

@MaaaxiKing
Copy link
Author

Or maybe just a comma would be better if it was highlighted in a strong color? That would be my favourite, I think.

@YuriSizov
Copy link
Contributor

YuriSizov commented Jun 27, 2020

@MaaaxiKing Keywords are reserved words, you can't name variables using reserved words. You may have a variable named default, or something else, now, and in that case you'd need to change your code for a future version of GDScript. And since any of this can only be implemented in 4.0, a breaking change is not a big problem.

Or maybe just a comma would be better if it was highlighted in a strong color? That would be my favourite, I think.

Code readability should not be dependent on syntax highlighting.

@MaaaxiKing
Copy link
Author

OK, would be fine then.

@KoBeWi
Copy link
Member

KoBeWi commented Jun 27, 2020

The keyword could be something less likely used for a variable name, like _default_.

@MaaaxiKing
Copy link
Author

The keyword could be something less likely used for a variable name, like _default_.

Or maybe _default?

@bojidar-bg
Copy link

There is already a "keyword" for something similar: _, at least when used for discarding values in match statements.

@MaaaxiKing
Copy link
Author

Oh, you're right.

@YuriSizov
Copy link
Contributor

There is already a "keyword" for something similar: _, at least when used for discarding values in match statements.

@bojidar-bg Arguably not the best syntax either. I'd prefer something that reads obviously in both cases. Makes a bit more sense in match statements to mean "match any" though. If it was a switch statement, making it default would be more reasonable.

@MaaaxiKing
Copy link
Author

I think _ from match should be changed to _default and in this proposal it should be _default too then.

@Calinou Calinou changed the title Use default value for an argument after , Use default value for an argument after , Jun 29, 2020
@MaaaxiKing MaaaxiKing changed the title Use default value for an argument after , Use default value for an argument after "," Dec 28, 2020
@MaaaxiKing MaaaxiKing changed the title Use default value for an argument after "," Use default value for an argument after , Dec 29, 2020
@vnen
Copy link
Member

vnen commented Mar 2, 2021

I don't see a way to implement this. When calling functions in C++ there's no way to do this, and the binders also don't allow that (maybe could be done with a good amount of hacks but I'd rather not to). So even though I understand the value of this, I don't think it's feasible to implement.

@Calinou
Copy link
Member

Calinou commented Mar 2, 2021

Closing per @vnen'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

7 participants