Godot version:
3.1.1
Issue description:
Right now GDScript only allows letters and underscores in identifier names. IMO it would be useful to allow exclamation and question marks.
Why useful though? It's more about convention. See, Ruby has some methods that end with ? or !. If method ends with ?, it's supposed to return boolean. ! denotes a method that works "in-place" instead on a copy.
If these were adopted to Godot, we could e.g. change stuff like Array.empty()
to Array.empty?()
, which makes it more clear what the method does (another option of course is renaming it to is_empty()
, but I remember a discussion that some classes do this and others don't and it's inconsistent). ! would find some use too, see https://github.com/godotengine/godot/pull/30874#issuecomment-516186244; we could have two versions of this method: filter()
which returns a filtered array and filter!()
, which filters the array you call it on. That applies to other array/dictionary methods too.
I don't think adding more "magic" to GDScript is useful here. The boolean return type can already be easily viewed in the documentation. We should rather rename methods to be more consistent in 4.0.
Fun anecdote: I do tend to use ?
like this via comments:
func create(stuff):
var success = false
success = process(stuff)
return success#?
Ruby seems to be a weird one to imitate given GDScript is mainly inspired by Python. Besides, ?
could (in my opinion) have a much better use as the null-conditional operator, see: C#, JavaScript
IMO, '?' would be much more useful as a ternary conditional operator, like so (example from C#):
var status = life > 0 ? "alive" : "dead"
I find it very convenient and pretty easy on the eyes.
@dioptryk GDScript already supports a ternary operator, with the same syntax as Python :smiley:
var status = "alive" if life > 0 else "dead"
It follows a different order compared to the C-style ternary operator, but when used with simple expressions, it's still easy to read.
As a ruby user, I would normally be in support of this, however Godot has already established is_*
in place of adding ?
to the end of functions (exceptions apply, however changing them before a major release would break compatibility -- see #16863). In regards to !
, I find myself not needing such an operator compared to some_object = some_object.do_a_thing()
(assuming the function returns a copy of the object and doesn't already do in-place edits).
My opinion is that I don't see how it could be useful compared to existing conventions.
@Calinou oh man, how did I miss that? Awesome :)
Regarding the issue at hand: excluding some _established_ (so to say) syntax constructs, I'm personally against adding more "magic" to the language. There's a nice blog post "less is more" here about it. The code doesn't become more readable, the compiler/interpreter/parser become more complicated, and if the new syntax subtly differs from other languages, it only becomes a source of confusion. To me, GDScript is language of choice in Godot because of its simplicity, and I have more than a decade professional .net experience, so I thought that I'd prefer C# - but the only thing I'm missing are collections.
Heh, the most down-voted issue I managed to create. Nice 馃槑
Most helpful comment
Ruby seems to be a weird one to imitate given GDScript is mainly inspired by Python. Besides,
?
could (in my opinion) have a much better use as the null-conditional operator, see: C#, JavaScript