It’s like other Languages of this world for example punctuation: We know it should be there but it’s pointless and somehow it still has a purpose just think into it! I may not fully understand what you meant but colons do have a purpose (idk why I just joined Godot) but everything has a reason to exist. Just like Gaming I mean kids love it but parents think it’s a waste of time (just like you think colons are) but gaming has its purpose: entertainment
And colons must have a purpose too!
Hope it helped please correct me if I made something wrong I prefer to be corrected than not
To me it’s the start of the branching statement (and code block) and the end of an expression, as well as high level languages are meant for humans, not computers.
I would also think having condition syntax makes the compiler more complex.
I think having a func declared in code like this could be mighty confusing.
var f = func()
return "foo"
Switch statements could not work without them. Basically you would have to have two types of keyword expressions, one that always requires a :, and expressions that maybe need one.
Also can your really ask newb question at this point?
lol, good point, but I’m new to Godot and GDScript at least, and never used Python, so didn’t get used to the colon.
It’s so amazing like how finally someone questions rigid syntax requirements of programming languages. The colon separation is a programming language design trend where languages try to immitate Python syntax.
From a design perspective, the colon is not required. I even doubt, it improves readability.
It’s just there because it comes from Python and Python likely used it because it was simpler to implement the parser (or probably simpler to provide automatic grammar parsing rules for the prototype). It’s a bit less verbose than writing if a then b
but syntactically, it’s easy to remember, meaning as much as “here comes code”.
Other languages like Julia do not separate the condition from the body with extra symbols (and Julia doesn’t even enforce identiation). Having no operator between two values is enough as separator. When I tried to learn Nim, I found, the colon is particularly confusing to read because there are like 4 incompatible syntactic purposes that the colon is used for (“here comes code”, “here comes type”, “here comes value” (named arguments or named values of literals) and likely something else that I forgot).
Thank you, by the way, for an actually useful and thoughtful reply.
var f = func (x)
return "foo"
A compiler could be made to understand this perfectly well as it’s unambiguous. Yes, it’s unambiguous because indentation denotes scope, and therefore this can only be a function declaration, not a function call.
However, I agree it’s confusing for humans because we’re not very good at parsing code that uses indentation for scope.
Indeed, you said youself that “To me it’s the start of the branching statement (and code block) and the end of an expression”. All those things are taken care of with EOL+TAB, but humans need more visual reinforcement.
However, I’m not sure that’s a good reason to mandate a colon though, and it’s an especially bad reason to make me put one in manually instead of adding it automatically.
If the argument is that the colon is there to make sure I’m not making an error while coding, then that’s an argument against using whitespace and indentation as tokens at all.
Switch statements could not work without them.
I believe that’s incorrect, as the colon on each line is followed by EOL and an indented code block:
match x
1
print("It's one!")
2
print("It's one times two!")
var new_var
print("It's not 1 or 2, it's ", new_var)
is unambiguous.
I contend that
code : EOL TAB code
is functionally equivalent to
code EOL TAB code
in all cases.
Note: I’m actually very interested to find out if I’m wrong about this, so if anyone can give me example code that would behave unexpectedly without colons, I’d be grateful. (Obviously I mean with a parser that didn’t expect colons, not in Godot as it stands now)
It’s so amazing like how finally someone questions rigid syntax requirements of programming languages. The colon separation is a programming language design trend where languages try to immitate Python syntax.
Thanks. I think it’s because I didn’t grow up on Python that I questioned the colon - coming from C, I thought “if indentation is used to denote code blocks, what’s the colon for?”
From a design perspective, the colon is not required.
Hear hear!
I even doubt, it improves readability.
Well, I admit, @pennyloafers’s example of a var func() definition does benefit (visibly) from the colon. But then, I think, code blocks would also benefit (visibly) from an end token. Oh! { } would work!
It’s just there because it comes from Python and Python likely used it because it was simpler to implement the parser
Probably!
So the only excuse to keep the colon is for readability for humans, because using whitespace for scope is terrible for readability (and for bugs)?
I would suggest you make your case and proposal here. See what they say.
Okay, that seems plausible.
I started to change the parser and changed all colon syntax errors to newline tokens. But since the parser consumes the code while it is read, (i.e. it doesn’t look back or peak ahead) it introduces some “slack” issues where, after, it expects certain tokens before continuing. A second order problem.
Not to say this could be resolved I just wanted to go to bed instead.
I also could be biased Im on team colon for an aesthetic reasons it seems.
Well, I appreciate the effort!
I still don’t really understand why the colon is necessary, but I’m just going to have to accept that the parser needs it disambiguation, and leave it at that.
Although the fact that the parser can tell me it’s missing means it’s not necessary for disambiguation…
It was a good discussion though.
Thanks @krisutofu, @wyattbiker and @pennyloafers
Thanks for the link. I looked into whether other people have commented on the colon. One I found Can we make : optional when declaring functions? is relevant, but the answers are wishywashy, and don’t actually make sense, yet are used to dismiss the idea. It’s a fight I won’t win because the colon is now dogma.
That’s because of the program iterations. You have to search for that in the project settings as debug.
Maximum computers from the past like Cleaning Watches and Detonators only have 8 iterations. From start to finish. It doesn’t matter how you format this language. It’s required because the program internals have it set up that way. Line 16 to 88 of C++ will be :(colon) Lines 256 to 12000 will be periods. And, so on…
Apologies for reviving this now old thread, but I wanted to add that as a beginner programmer, and new to Godot, I couldn’t find a decent answer about why : was needed at the end of an if statement. I’m glad there are people out there calling out pointless conventions. As you mentioned again and again @tobydjones, if the parser knows it’s not there, it obviously isn’t needed. Hopefully it becomes optional, although I will live if it doesn’t.
I couldn’t find an improvement proposal discussion about this. If there isn’t one then it almost certainly won’t be changed.
You can start one here.
Be aware that if you ask to make it such that colon isn’t allowed in the if statement, that would be a breaking change and won’t even be considered for 4.x.
I frequently peruse the discussions there and I get the sense that you are going to be in for a losing battle even trying to get the colon as optional for 5.x.
I tried every way I could think of to create an if statement that actually needed a colon but I couldn’t do it.
I agree that the colon does nothing in an if statement.