munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.5k stars 1.01k forks source link

Question Regarding 8.2.1 #1059

Open chrisjbreisch opened 2 years ago

chrisjbreisch commented 2 years ago

In 8.2.1, you state that the following is illegal:

if (monday) var beverage = "espresso";

With the following afterwards:

We could allow the latter, but it’s confusing. What is the scope of that beverage variable? Does it persist after the if statement? If so, what is its value on days other than Monday? Does the variable exist at all on those days?

Code like this is weird, so C, Java, and friends all disallow it.

Actually, I don't find this confusing or weird at all, and now that I think about it, I find this exception to be both confusing and weird. The purist in me has a serious problem with it. Why disallow it? I admit that it serves no purpose, but I can create many legal statements that serve no purpose.

You made this illegal: if (monday) var beverage = "espresso";

But this isn't?

if (monday) {
    var beverage = "espresso";
}

You (and those who came before you) are treating a statement block differently than a statement. And for no good reason, really.

The answers to all of your questions about the first example are simple, because they're exactly the same as they would be in the second example:

What is the scope of the beverage variable?

The scope is the statement executed when monday is true. Exactly as it would be in the second example.

Does it persist after the if statement?

No, of course not. The question itself is ludicrous. Would it persist outside of the statement block in the second example?

If so, what is the value on days other than Mondays? Does the variable exist at all on those days?

Again, the questions themselves are ludicrous. Of course the variable doesn't exist on days other than Mondays. You wouldn't expect it to in the second example. Why would you have any questions at all about the first?

Were I designing Lox, this would be a perfectly legal, if admittedly, completely useless statement.

I think that if you're going to disallow it, you have to justify it better. I don't find your justification convincing at all.

Sidewinder1138 commented 2 years ago

I just wanted to comment that, having taken a job working with a lot of thin-skinned younger engineers, it was refreshing to read this criticism that doesn't hold back. The use of "ludicrous" cracked me up 😆