munificent / craftinginterpreters

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

Section 24: Functions can be reassigned like variables #784

Closed katjonathan closed 4 years ago

katjonathan commented 4 years ago

Clox seems happy to compile something like this, despite dummy referring to a function name and not a variable:

fun dummy() { return; }
dummy = 2;
print dummy;    // prints '2'

Should this be allowed?

munificent commented 4 years ago

If you're asking whether the interpreter should allow it, the answer is yes. Lox treats all variables as mutable and function declarations are just syntactic sugar for variable bindings.

If you're asking whether that's a good language design choice for Lox... it's debatable. But doing this way keeps the interpreter simpler and avoids the need to track which identifiers are bound to functions and which to variables at compile time. It's working as intended. :)