Why exactly is eval evil?

前端 未结 12 1774
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 09:51

I know that Lisp and Scheme programmers usually say that eval should be avoided unless strictly necessary. I’ve seen the same recommendation for several program

12条回答
  •  我在风中等你
    2020-11-22 10:34

    Eval is not evil. Eval is not complicated. It is a function that compiles the list you pass to it. In most other languages, compiling arbitrary code would mean learning the language's AST and digging around in the compiler internals to figure out the compiler API. In lisp, you just call eval.

    When should you use it? Whenever you need to compile something, typically a program that accepts, generates or modifies arbitrary code at runtime.

    When shouldn't you use it? All other cases.

    Why shouldn't you use it when you don't need to? Because you would be doing something in an unnecessarily complicated way that may cause problems for readability, performance and debugging.

    Yeah, but if I'm a beginner how do I know if I should use it? Always try to implement what you need with functions. If that doesn't work, add macros. If that still doesn't work, then eval!

    Follow these rules and you'll never do evil with eval :)

提交回复
热议问题