2 ms·
The basic problem is, how do you even know that something is a variable reference? Remember, everything is a string. This means that the body of the lambda is a
by int_19h 5y ago
The basic problem is, how do you even know that something is a variable reference? Remember, everything is a string. This means that the body of the lambda is also a string. Now you can require that it's an eval'able string at the point of creation, and parse it to identify any $var references. But you can only do that for the topmost level of the lambda, because it can contain nested strings - and you don't know which of those strings are code, and which aren't (since it's really the command to which the string is passed that decides to treat something as code or not).
Furthermore, if you process $ like that, this won't work for any other command that takes a variable name as an argument, and does something to it.
- rightbyte 5y ago> The basic problem is, how do you even know that something is a variable reference? Isn't that by convention? Like there is a "upvar" to grab values of the prior stack.
- int_19h 5y ago"upvar" is explicit, there's no guesswork involved there. But how would you handle a lambda with a body like this? if {$foo == 1} { puts $bar } else { puts $baz } given that all the {}s are just non-expanded string literals, and what makes them be interpreted as code is the implementation of "if"? Sure, you can hardcode "if"; but the whole point of Tcl is that syntax constructs like that can be easily implemented as a library, which breaks if you have to special-case them in lambdas for them to work. Now if you used upvar, this kinda sorta works, but only so long as the lambda is immediately invoked by the function that it was passed to. If you want to pass it on, you'd have to wrap it in another lambda. And, of course, this only works for lambdas that don't escape.