I'm reading
Crafting Interpreters and I have a question:
(Lox is the author's language that's being implemented in the book.)
Code:
class Breakfast {
cook() {
print "Eggs a-fryin'!";
}
serve(who) {
print "Enjoy your breakfast, " + who + ".";
}
}
breakfast.meat = "sausage";
breakfast.bread = "sourdough";
Why? This has always struck me as extra pozzed. You have a class, and then some fields just aren't guaranteed to
be on it, and you
don't know because it's still superficially the same old class. Methods you define on the class can't cleanly reference these fields, they have do to a gay
if hasattr(self, "my_secret_field") dance. What does it have to do with
dynamic typing, which the book explains like so:
I get what this means, you can reassign a variable, so that a variable is sometimes a string and sometimes an integer and sometimes a Breakfast. Why does it
also require that there could be an integer with a sausage?