The Cobra Programming Language's syntax is heavily inspired by Python's, and it does away with the colon, so it seems that it is not strictly necessary. However, it is not enough to just remove the colon, there are other changes to the syntax needed. See, for example this piece of code from one of my toy projects:
kons = lambda hd, tl: lambda x: hd if x else tl
virst = lambda l: l(True )
rrest = lambda l: l(False)
Without the colon to separate the body from the parameter list, I would have to use indentation:
kons = lambda hd, tl
lambda x
hd if x else tl
virst = lambda l
l(True )
rrest = lambda l
l(False)
I believe earlier versions of Cobra made the colon optional, you could use either indentation or a colon or both. Similar to how it works in Ruby, where there are keywords to separate the different parts of control expressions, but you can also use expression separators (semicolon or newline):
# idiomatic
while true do puts "I am awesome" end
# ↑↑
# non-idiomatic, but legal
while true; puts "I am awesome" end
# ↑
# non-idiomatic, but legal
while true
puts "I am awesome" end
# idiomatic
while true
puts "I am awesome"
end
In current versions of Cobra, you can use a comma:
if x
y
can be written as
if x, y
You need some way of separating the different parts of control expressions or definitions. In Python, that is the colon. If you remove the colon, you need to replace it with something else, e.g. forced indentation. Only removing the colon won't work.
The only way to be absolutely sure is to formalize the syntax with and without colon and prove its non-ambiguity.
Note, however, that one of the aphorisms of the Zen of Python is "Explicit is better than Implicit", so the explicit delineation of blocks with colons seems to fit with the general philosophy of Python. The Design and History FAQ also mentions that this decision is based on empirical evidence from Python's predecessor, ABC.
if
,else
,while
and so on. If you did that, would python still be language that can be used without ambiguity?if .. then .. end
in a single line. So here in pythonthen
is substituted by two things a:
and a required newline. One of these seems redundant.