What are the advantages of dynamic scoping?

后端 未结 10 933
迷失自我
迷失自我 2020-12-14 01:21

I\'ve learned that static scoping is the only sane way to do things, and that dynamic scoping is the tool of the devil, and results only from poor implementations of interpr

10条回答
  •  轮回少年
    2020-12-14 01:36

    Dynamic scope allows for definition of contextual functions. In that sense, it is quite similar to dependency injection in modern frameworks. (For example, consider when you annotate a Java class with dependency injection definitions to allow transparent initialization of various references. (cf spring, or JPA, etc.))

    Obviously, dynamic scoping makes certain assumptions regarding the runtime characteristics of the invocation site for a given function which can not be guaranteed at compile (or design) time. Again, following the example of a modern (Java) framework component, if you instantiate such a class outside of the controlled (runtime) environment of a container, it is easily possible that the class will not be able to function given that its required dependencies will not have been initialized (aka injected).

    But equally obviously, component systems (as just one example) clearly benefit from dynamic binding mechanisms. Dependency injection is a framework level means of achieving this. Dynamic scoping is a language level means of the same.

提交回复
热议问题