kebab-case in function name?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sxxtp

For some reason godot does not like the use of kebab-case in a function name. I won’t be able to use godot if this isn’t an option.

func _ready(): 

func test-function():

The above code returns an error. Is there any workarounds to this?

:bust_in_silhouette: Reply From: kidscancode

The - character is the subtraction operator in GDScript, so no, you can’t use it in a variable/function name.

The GDScript style guide prescribes snake_case for variables and functions. The best “workaround” would be to stick to test_function() as that will match the style of all the built-in function names.

As I stated in my original post, I need to be able to use kebab case. I’m surprised Godot, as an open source engine, is this Authoritarian about naming conventions. The way you’ve stated it, it seems my only options are to go into the source code and change the subtraction operator, or download Unity.

sxxtp | 2020-03-07 23:41

I’m not sure how that’s going to help you, as - is not an allowed character in C# either (or any other language I can think of besides Lisp).

kidscancode | 2020-03-07 23:54

Yep, I’ll second what kidscancode already said. You’ll be hard-pressed to find a mainstream language that supports a dash character in a function name. With that in mind, I assume you’ll need to rethink your “I need to be able to use kebab case” statement.

Why is that particular convention such a hard requirement in your case?

jgodfrey | 2020-03-08 00:10

Thanks, i’ll look into lisp

sxxtp | 2020-03-08 00:12

jgodfrey, there is an organization I am attempting to join and they require new recruits to create a game wherein code is neat and all names follow their standards and practices.

sxxtp | 2020-03-08 00:15

Lisp is not going to be much use for you if you’re interested in game development. It would be more useful to ask what possible reason this “organization” has for such a strange policy, and what possible languages they could be prescribing.

Note there are very good reasons that the majority of programming languages disallow - in identifiers (and also any other operators like + / & etc). Consider the following:

var a = 5
var b = 2
var a-b = 1
var c = a-b  # what is c? 1 or 3?

Allowing this would make it impossible for the compiler to know whether you meant a variable name or an operation there.

kidscancode | 2020-03-08 00:21

Huh… Ok. It seems really strange that they’d adopt (and require) a naming convention that inherently eliminates most mainstream languages.

Anyway, good luck.

jgodfrey | 2020-03-08 00:23

The organization is mainly for fun, nothing serious.
There’s no good reason why the compiler can’t just interpret everything after "func " and before “:” as a name. For your example above, I don’t see any reason why it would be 3. The only confusion I could imagine is this case:

var a = 1
var b = 2
var a-b = a-b

but I don’t see how this affects functions

sxxtp | 2020-03-08 00:32

but I don’t see how this affects functions

var a = 1

func b():
    return 2

func a-b():
    return 3

func _ready():

So, what will this print then? -1 or 3? How do you know?

njamster | 2020-03-08 12:52