简体   繁体   English

怎么称OOP相当于“参考透明度”?

[英]What to call OOP's equivalent of “referential transparency”?

My understanding is that the term " referential transparency " can really only be applied to functional code. 我的理解是,“ 引用透明度 ”这个术语实际上只能应用于功能代码。 However, a method call on an object in object-oriented code can have a similar property, which is that the return value of the method, and the state of the object after a method call depends only on the state of the object before the call, and the method's arguments. 但是,对面向对象代码中的对象的方法调用可以具有类似的属性,即方法的返回值和方法调用后的对象的状态仅取决于调用之前对象的状态,以及方法的论点。

ie functional referential transparency: 即功能参照透明度:

i = foo(n, m);
// return value depends only on n, m

OO "referential transparency": OO“参考透明度”:

i = obj.foo(n, m);
// return value, and subsequent state of obj, depends 
// only on initial state of obj, n, m

Is there a name for this property? 这个房产有名字吗?

If the state of obj does not change during the call to foo() , then the "object oriented" style is equivalent to the functional form if function overloading is supported since it could be rewritten as: 如果在调用foo()期间obj的状态没有改变,那么“面向对象”样式等同于函数形式,如果支持函数重载 ,因为它可以被重写为:

i = foo(obj, n, m);
// return value depends only on obj, n, m

However, is pretty common for the state of obj to change in a method call, so I'm not sure if this helps the analysis... 但是, obj状态在方法调用中更改是很常见的,所以我不确定这是否有助于分析...

Your mistake is thinking that FP and OO are somehow fundamentally different. 你的错误在于认为FP和OO在某种程度上是根本不同的。 The "OO version" of referential transparency is just referential transparency. 参考透明度的“OO版本”仅仅是参考透明度。

An expression e is referentially transparent if and only if e can be replaced with its evaluated result without affecting the behavior of the program. 当且仅当e可以用其评估结果替换而不影响程序的行为时,表达式e是引用透明的。

So if you have an expression o.foo(a) , then it is referentially transparent if you could modify your code to replace it with the result of the call, without changing how your program behaves. 因此,如果您有一个表达式o.foo(a) ,那么如果您可以修改代码以将其替换为调用结果,则它在引用上是透明的,而不会更改程序的行为方式。 Obviously, if o.foo is void, you can't do that. 显然,如果o.foo无效,你就不能这样做。 Ditto if it modifies the internal state of o . 同上,如果它修改了o的内部状态。 So the only way for o.foo(a) to be referentially transparent is if its result is a function of o and a . 因此, o.foo(a)被引用为透明的唯一方法是它的结果 oa 的函数

In my mind, "functional code", is synonymous with "referentially transparent code". 在我看来,“功能代码”是“引用透明代码”的同义词。

The functional term would be referential transparency as you say. 正如你所说,功能术语将是参考透明度 I would humbly propose that the form you've described here with the results depending on the method's arguments plus the object's state be termed referential opacity . 我谦卑地建议你在这里描述的形式结果取决于方法的参数加上对象的状态被称为参考不透明度

I don't think that the property you described in the OO scenario gives you anything similar to what referential transparency does in functional programming. 我不认为你在OO场景中描述的属性给出了类似于函数式编程中引用透明性的内容。 You described a property where the foo method modifies only the state of the obj object in the following call: 您描述了一个属性,其中foo方法仅修改以下调用中obj对象的状态:

i = obj.foo(n, m); 

However, if you have another object that references obj then the call to foo also modifies the behavior of the other object. 但是,如果您有另一个引用obj对象,则对foo的调用也会修改另一个对象的行为。 Since references between objects are essential in OO (meaning that this is a problem you cannot easily avoid), this means that the property you described doesn't tell you much about code. 由于对象之间的引用在OO中是必不可少的(这意味着这是一个你不能轻易避免的问题),这意味着你描述的属性并没有告诉你很多代码。 For example: 例如:

a = new Other(obj);
i = obj.foo(n, m);  // changes state of 'obj' and 'a'

If the foo method was referentially transparent (didn't modify any state - just returned some results), then that would be an interesting property - because it wouldn't modify the state of a . 如果foo方法是引用透明 (不修改任何状态-刚刚返回一些成果),那么这将是一个有趣的属性-因为它不会改变的状态a

the state of the object after a method call depends only on the state of the object before the call, and the method's arguments. 方法调用后对象的状态仅取决于调用之前对象的状态以及方法的参数。

I suppose you could say that the method has no external dependencies . 我想你可以说该方法没有外部依赖

Unlike referential transparency however, I'm not sure what this gains you. 然而,与参考透明度不同,我不确定这会给你带来什么。 I suppose it means the method is easily testable. 我想这意味着该方法很容易测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM