Re:The Dynamic Def – abusing Ruby's def statement
by TeMPOraL on Sunday October 18, 2015 @02:56PM
(#10409285)
This is indeed the basic feature of any language that's evaluated at runtime. When working with such a language, one needs to learn the program as a dynamically growing construct instead of a vision cast in stone when you press the "compile" button.
[ Parent
| on HN ]
Re:The Dynamic Def – abusing Ruby's def statement
by kazinator on Sunday October 18, 2015 @04:32PM
(#10409670)
Or rather, one needs to learn which constructs destructively manipulate a global environment, and which perform lexical binding.
In Python, an inner def will lexically bind a function, creating a closure. Python is not less dynamic than Ruby.
In Common Lisp, a defun inside a defun will behave similarly to Ruby; but if you want lexically scoped local functions, you use a different operator, namely flet or labels.
Scheme has a define which is lexical: it brings a lexical identifier into the scope for forms which follow.
Lexically scoped items, even in a dynamic language, in fact can be "cast in stone when you press the compile button"; they are cast in that stone which is the entire compiled environment of the surrounding function.