问题标签 [dynamic-scope]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
3414 浏览

clojure - Clojure:绑定与 with-redefs

clojure.core 具有宏绑定with-redefs。查看 clojuredocs.org 上的文档字符串和示例,它们似乎做了非常相似的事情。有什么区别,我应该在哪些情况下使用哪一个?

0 投票
2 回答
2338 浏览

perl - 为什么不能在 Perl 中本地化词法变量?

我有以下 Perl 代码。

即使 $x 在 b() 子例程中具有范围,为什么 Perl 不允许本地化它?

0 投票
2 回答
617 浏览

macros - Dynamic scope in macros

Is there a clean way of implementing dynamic scope that will "reach" into macro calls? Perhaps more importantly, even if there is, should it be avoided?

Here's what I'm seeing in a REPL:

This m-get-a macro isn't my actual goal, it's just a boiled down version of the problem I have been running into. It took me a while to realize, though, because I kept debugging with macroexpand, which makes everything seem fine:

Doing macroexpand-all (used from clojure.walk) on the outer binding call leads me to believe that the "issue" (or feature, as the case may be) is that (m-get-a) is getting evaluated before the dynamic binding takes:

Here's my crack at a workaround:

It will evaluate a single macro expression with the relevant dynamic bindings. But I don't like using macroexpand in a macro, that just seems wrong. It also seems wrong to resolve symbols in a macro--it feels like a half-assed eval.

Ultimately, I'm writing a relatively lightweight interpreter for a "language" called qgame, and I'd like the ability to define some dynamic rendering function outside of the context of the interpreter execution stuff. The rendering function can perform some visualization of sequential instruction calls and intermediate states. I was using macros to handle the interpreter execution stuff. As of now, I've actually switched to using no macros at all, and also I have the renderer function as an argument to my execution function. It honestly seems way simpler that way, anyways.

But I'm still curious. Is this an intended feature of Clojure, that macros don't have access to dynamic bindings? Is it possible to work around it anyways (without resorting to dark magic)? What are the risks of doing so?

0 投票
0 回答
129 浏览

language-agnostic - 静态作用域和动态作用域

我有这个程序:

  1. 使用静态范围,sub1 中的 a 和 b 是主要的吗?
  2. 使用动态范围和链:main → sub → sub → sub1。sub1 中使用了哪些变量 a 和 b?
0 投票
2 回答
657 浏览

pseudocode - 这个(类 C 语言)伪代码使用动态范围的输出是什么?

使用 DYNAMIC SCOPING 的给定伪代码的输出是什么?在这里,我想知道将打印的 x 值是多少。

它只是一种类似于 C 但具有动态作用域的语言中的简单伪代码。

0 投票
1 回答
62 浏览

perl - 如何在“闭包范围”内执行 lambda?

这怎么行?

换句话说,$driver_cr 如何在没有以下条件的情况下访问 $constant:

  1. 将 $constant 作为 arg 传递给驱动程序 &$driver_cr($constant)
  2. 将 $constant 的范围更改为全局our $constant = "abcd";
  3. 制作一个公共块并从基础移动 $constant:

    /li>
0 投票
1 回答
199 浏览

scope - 程序语言理解 - 静态与动态范围

我无法理解我们回来的作业的答案。我相信我对“嵌套”函数的概念感到困惑,但也许这是错误的。我正在寻找一些关于从以下代码中分配动态和静态范围值的帮助。

当我经历每一个时,我的想法:

静止的:

  • Run set_x(0),由于 n 的参数,这会产生一个局部变量,但是由于我们将 x 设置为 n 而没有在本地声明 x (int x =..),因此我们将全局 x 更新为 0。
  • Run first(),它执行set_x(1),它遵循相同的逻辑将 x 全局更新为 1。然后我们首先在其中运行print_x,它打印出 1 的全局 x。
  • Run print_x,它只是重新打印 1。
  • 运行second()我们在本地声明 x 并运行set_x(2),它会将 2 更新为 n。(因为是set,而不是second过程,对吧?然后我们运行它的print_x过程来打印 2。
  • 再次运行print_x它只会转储 2。
  • 结果 1122

动态的(对这个更困惑)

  • 运行set_x(0)将 x 和全局 x 设置为 0。
  • 运行first()我们set_x再次点击并将 x 更新为 1。我们打印 1。
  • 运行print_x我们重新打印 1。
  • 运行second()我们在本地创建 x,我们运行set_x(2)并将全局 x 设置为 2。然后我们打印 2。
  • 运行print_x最后我们再次重新打印,这里是我猜的 2,但答案应该是 1。
  • 我的猜测是 1122,实际答案是 1121

我对动态的最后一部分以及为什么它是 1 而不是 2 感到困惑。

0 投票
0 回答
85 浏览

javascript - 每个评估方向的动态范围

鉴于以下 [任意语言,虽然我认为这是 ALGOL] 程序:

您将如何使用javascript在两个方向上使用动态范围(从左到右评估然后从右到左评估)在整个程序中显示值,以便我可以交互地更改值并观察会发生什么?

我在一些问题上学得有点慢,比如要求输出的值,但没有在整个程序中显示值状态,并试图更好地理解,特别是在我可以交互玩的例子中。

我为从左到右的静态作用域创建了一个 plnkr,为从右到左
的静态作用域创建了另一个 plnkr,请 随意分叉它们的交互式答案:)。

0 投票
1 回答
272 浏览

html - 如何访问angularjs中返回的动态范围

我有一个要求——单个 Angular js 函数根据函数的输入参数之一动态返回范围变量。对于控制器功能,我找到了一些关于如何返回动态范围的示例

$scope[attributeId] = data.response; (attributeId 为入参,data.response 为数组)

问题是如何在 HTML 中使用这样的范围变量?我有一个这样的选择控件,它将填充一个下拉列表,其中包含在范围内重新调整的值,在这种情况下应该指定为模型和选项。

一个例子真的很有帮助。

0 投票
2 回答
151 浏览

scope - 为什么 Common Lisp 中的闭包没有捕获封闭函数的参数?

test.lisp

在 REPL 中:

我认为现在 common lisp 应该是词法范围的,那么为什么var1内部 lambda 没有捕获的值test呢?我怎样才能确保它被捕获?