2 ms·
val fullName = with(miltonFriedman) { "$firstName $lastName" <-- slick! } That's a famously poor idea; these designers must have been playing hookie when
by kazinator 3y ago
val fullName = with(miltonFriedman) {
"$firstName $lastName" <-- slick!
}
That's a famously poor idea; these designers must have been playing hookie when the issue with the Pascal WITH statement was discussed in compiler class.
Yeah, it's slick allright---like an oil spill in a hairpin turn.
When we bring into various scopes throughout a program all of the members of an object, we allow the definition of that object, from a distance, to manipulate those scopes.
with (thisObj) {
with (thatObj) {
fun(foo, bar, xyzzy);
}
}
Today, foo and bar come from thatObj, whereas xyzzy comes from thisObj.
Tomorrow, thatObj changes and suddenly has a xyzzy member, so xyzzy now refers to the one in thatObj. You have a bug which is distant from the definition of thatObj.
The bad WITH is still found in Modula 2, but the late Niklaus Wirth dropped it when designing Oberon.
https://www.modula2.org/reference/withstatements.php https://www.modula2.org/reference/withstatements.php
Oberon-2 has a WITH statement that is something else entirely; some kind of switch that dispatches branches of code based on the dynamic type of an expression, such that the value is then considered to have a certain static type in each of the branches.
Modula-3, dsigned by Luca Cardelli and a few other authors, has a sanely-designed WITH statement:
https://www.cs.purdue.edu/homes/hosking/m3/reference/with.html https://www.cs.purdue.edu/homes/hosking/m3/reference/with.ht...
This has the property that all the identifiers it introduces have to be explicitly mentioned. It's like a kind of aliasing let. Not exactly like symbol-macrolet in Common Lisp, but similar.
When a construct does implicitly bind identifiers, they should be fixed, documented ones: e.g. anaphoric if macro binding the it variable, or loops binding a break or continue.
Constructs that create a wormhole whereby a distant part of the program elsewhere controls the content invite bugs. It's like the scoping equivalent of the COME FROM statement in Intercal.