最近我听到了它们的定义,对我来说它们听起来是相同的。 thunk 与延迟加载相同吗?如果不是,这两个定义有什么区别?
最佳答案
使用以下定义:
延迟加载
Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed.
重击
A thunk is an unevaluated expression, often represented as an object on the heap with bound variables and a code pointer (a closure). Lazy evaluation replaces thunks by more evaluated forms (pure values for simple types but otherwise an evaluated outer structure, and possibly unevaluated inner contents, i.e., weak head normal form). That replacement is done destructively, i.e., a side-effect of evaluation.
您可能会说“延迟加载”是一种在面向对象语言中将计算推迟到第一次需要对象时的方法。当需要该值时,将评估整个对象。
thunk 的相似之处在于它是推迟对函数式编程语言中任何表达式求值的方法。当需要时,重击将被替换为其评估的内容,这可能是另一个重击。在像 Haskell 这样的语言中,所有值(理论上)都由 thunk 表示,使得每个评估步骤都可能是惰性的。
它们是大致相似的机制,用于实现相同的最终目标:非严格评估。
关于oop - 延迟加载 vs thunking?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828898/