-
Notifications
You must be signed in to change notification settings - Fork 74
Add for/while else #143
Comments
Yes, Starlark does not support for/else, and this may be surprising to a Python programmer. On the other hand, a programmer familiar with every language but Python might stop and wonder what for/else means. Between that, and the complexity of adding "more stuff", I don't find it a compelling feature to add to the language when there is an efficient and abundantly clear workaround available: a boolean variable. |
They have a lot of stuff to wonder about too, like significant whitespace(!!!). With the abundantly clear workaround, for root in roots:
if src.path.startswith(root):
modules.append(create_module(name = src.path[len(root):], file = src))
break
else:
fail("Module roots does not contain %s" % src.path, "srcs") becomes found = False
for root in roots:
if src.path.startswith(root):
found = True
modules.append(create_module(name = src.path[len(root):], file = src))
break
if not found:
fail("Module roots does not contain %s" % src.path, "srcs") The Python is far more readable than the workaround. |
I was about to say that the desugaring is incorrect: the found=True statement should be outside the if statement. But then I checked and found that, no, you are correct, which only proves my point: the semantics of 'else' are far from obvious, even to someone familiar with a dozen other languages. So again I say the desugared code is more readable, though obviously less concise. Regarding significant whitespace, it may be surprising when you first see it, and it may be a cause of carelessly inserted mistakes, but its meaning is abundantly obvious. |
Your rewrite with the I've migrated tons of files at Google from Python to Starlark, and this |
Or rather it proves my point, that you would have desugared wrong and the original form was a more reliable to express a common sort of thing.
And Skylark still chooses to have significant whitespaces. It seems that presence of feature in other languages is not actually a criterion. |
Whitespace is not a feature, it's the base of the language. You might say it was a mistake and another syntax would be better, but looking like Python was a design principle (https://github.com/bazelbuild/starlark#design-principles). That's not relevant to this thread. There's also a desire to keep the number of features low and limit the number of concepts someone needs to know in order to read the code.
The desugared form is obvious. Read the code, it does what it says.
Familiarity is one of the goals. |
I tried to use for-else, only to discover it does not exist (
syntax error at 'else': expected expression
).This is surprising to someone familiar with Python control structures.
The text was updated successfully, but these errors were encountered: