openlang

Should eval ever fall back to a host global?

eval("rate + 1", env) with rate bound to 3 in env should mean 4. In compile-time mode, in runtime mode, every time. Flip a mutable host global called rate to 9 and the meaning of that input should not move.

That's the rule I want for this experimental D evaluator, so the environment is always an argument, even though a global lookup would make the first example shorter. D already leans this way. It evaluates eligible functions where a compile-time value is required, and CTFE code can't reference mutable globals or statics. Nice pressure test. That promise is narrower than it looks, though. Not every function can run under CTFE, and CTFE is no general security sandbox.

The host pays for it by assembling and passing every binding it wants the evaluator to see. In return, the same expression with the same environment means the same thing in both modes.

If rate is missing, no quiet fallback to the host global. Rejected. A runtime recovery hook could come later, and its docs would need to say whether a supplied replacement becomes part of the evaluation input.

后续讨论 3

顺着彼此的依据读下去
  1. 固定链接

    Detection and recovery come apart right at the missing rate. The evaluator knows the binding is absent. It does not know what should replace it. The host might.

    Common Lisp already has names for two different answers, the use-value and store-value restarts, and the difference matters for your rule. A value used once belongs in the record as an additional input to that one evaluation. A stored value changes the environment, so the next evaluation of the same text runs with a different env and may mean something else.

    For the D experiment I'd still document two behaviors, rejection at compile time and whatever recovery the runtime offers, and say which of the two kinds that recovery is.

  2. 固定链接

    Tidy so far. Before anyone builds that recovery hook, can a caller decline recovery and still get the original missing-binding diagnostic, not some rewritten version of it?

  3. 固定链接

    Lua makes the first half of this one argument. load(src, "expr", "t", env) resolves every free name in the chunk through env, so a missing rate is nil and rate + 1 raises an error. The host global only leaks in if someone gives env a metatable with __index = _G, and that line is easy to search for.

同一个问题的其他角度

  1. Same Int, two targets, two answers

    Found this while running the same Gleam module on both targets, and it's a runtime fact more than a language one. gleam run p…

    回复 3最近活动 gleam
  2. D's pure lets a function write through its arguments

    D's pure is weaker than the word suggests, and I only noticed because an evaluator experiment wanted to cache calls. pure in…

    回复 4最近活动 d
  3. CTFE happens where the declaration asks for it

    Had D's CTFE pictured wrong for a while. I assumed that once a function is fit for compile time evaluation, the compiler woul…

    回复 4最近活动 d