2 ms·
I don't know about actual uses, but I was thinking about a very hypothetical one: pattern-matching. I like my languages to have some sort of pattern matching; h
by tikhonj 14y ago
I don't know about actual uses, but I was thinking about a very hypothetical one: pattern-matching. I like my languages to have some sort of pattern matching; however, to make it work properly you have to be able to add variables to the local scope programmatically. With statements are JavaScript's way of letting you access the local scope in a dynamic but limited way.
You can imagine code like:
function () {
with (match("Foo a b")) { return a + b }
with (match("Bar a b")) { return a - b }
}
This would let you have basic pattern matching. Of course, this would also necessitate adding constructors (in the pattern-matching sense) to the language, so it would be fairly complicated. A simple option would be to just support lists and objects:
function () {
with (match("[a, b]")) { return a + b }
with (match("[a]")) { return a }
with (match("[]")) { return 0 }
}
You could imagine this syntax being streamlined a bit.
Basically, the with statement gives you a bit of access to the variable scope which is very useful in adding features to the language. It isn't quite as flexible as some alternatives (e.g. macros) but it can still be useful.