Open btarg opened 2 months ago
See also:
The C# way looks confusing. The gdscript way looks much cleaner.
The C# way looks confusing. The gdscript way looks much cleaner.
I'm not suggesting instanceof
be the syntax if that's what you mean. Otherwise I find it much easier and quicker to work with - especially if you have to otherwise cast multiple times! For example:
if my_variable is MyCustomType my_custom_type:
print(my_custom_type.function())
elif my_variable is AnotherCustomType another_custom_type:
# so on and so forth...
The last time we discussed this, we came to the conclusion that it would be nice to have the static analyzer take into account local control flow, including type checking. This seems more consistent with GDScript's design goals (simplicity and readability) than explicit type narrowing syntax.
A small nitpick, but to my knowledge there is no instanceof
keyword in C#, it's is
and it's used just as you suggested for gdscript:
if (vehicle is Car car)
car.drive();
Also you can do this in gdscript without double casting:
var car = vehicle as Car
if car: # null if not Car.
car.drive()
That said, I do like C# syntax for is
, but would prefer even more if it worked via magic/static analysis as mentioned above due to dynamic nature of gdscript.
Also the syntax in C# feels more idiomatic: you declare variables with Car car;
, so using similiar statement in tandem with is
is natural.
In gdscript, you declare with keyword var
, so the resulting code doesn't look like normal gdscript.
A small nitpick, but to my knowledge there is no
instanceof
keyword in C#, it'sis
and it's used just as you suggested for gdscript:if (vehicle is Car car) car.drive();
Also you can do this in gdscript without double casting:
var car = vehicle as Car if car: # null if not Car. car.drive()
That said, I do like C# syntax for
is
, but would prefer even more if it worked via magic/static analysis as mentioned above due to dynamic nature of gdscript. Also the syntax in C# feels more idiomatic: you declare variables withCar car;
, so using similiar statement in tandem withis
is natural. In gdscript, you declare with keywordvar
, so the resulting code doesn't look like normal gdscript.
My apologies, I didn't realise you could do that in GDScript. Perhaps the lack of autocomplete was an editor issue on my end. And yeah, looks like I was mixing up c# and Java when discussing is
.
Alternative syntaxes:
# I think this is the most "fluent"
if some_var is SomeType create new_var:
# kinda strange
if some_var is SomeType scope new_var:
# uhh not totally sure about this one
if new_var: SomeType stores some_var:
# this might be getting somewhere
if new_var: SomeType holds some_var:
(not saying it should be like this, just another option)
Describe the project you are working on
A game making heavy use of GDScript
Describe the problem or limitation you are having in your project
Casting into a variable safely currently requires quite a bit of boilerplate.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
I believe GDScript casting should be more like C#.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
In C# and Java, this can be shortened as
Here is an example of how we can adopt a similar syntax in GDScript:
If this enhancement will not be used often, can it be worked around with a few lines of script?
This is an enhancement to the current way of doing things rather than a brand new feature.
Is there a reason why this should be core and not an add-on in the asset library?
This is part of GDScript