For the purposes of this question, by 'pure object-oriented programming language' I mean one in which functions can only exist inside (static or non-static) objects, i.e. as methods.
Consider the following Java code:
import java.lang.Math;
class Program {
public static void main(string[] args){
Math.addExact(2, 3);
new Addends(2, 3).sum();
}
}
final class Addends {
private final int addend1;
private final int addend2;
Addends(int x, int y){
addend1 = x;
addend2 = y;
}
final int sum(){
return addend1 + addend2;
}
}
In this example:
- Is
Math.addExact
a pure function? - Is
Addends.sum
a pure function?
The answer to the first depends on how methods are understood to relate to functions, i.e. whether methods are considered kinds of functions or merely their nearest OOP equivalent.
The answer to the second depends on whether fields whose values are injected into an object may be considered inputs for an associated method using those fields (assuming a pure function to be one that gives the same output for the same inputs). The reason for thinking so is the recognition that for any object and particularly for those exhibiting high cohesion, it's fairly easy to convert any n-parameter method in it into an n - m
-parameter method by requiring its enclosing object be instantiated by injecting m
values, assigned to m
immutable fields, into an m
-parameter constructor for that object.
NOTE: I recognize the answer to this question is going to be partly a matter of semantics/convention. I'm asking whether there happen to be agreed upon conventions on this matter, and if so, what they are.
CLARIFICATION AFTER QUESTION WAS CLOSED AS OPINION-BASED: As stated above, I'm not asking for the opinions of individual contributors on this question: I'm asking whether there is convergence among programmers and/or computer scientists on examples like those given, based on a) whether methods are considered functions, and b) how broadly 'input' is construed in the definition of a pure function; and if there is convergence, what it is. Compare
- Opinion-based: What is morality?
- Not opinion-based: How is morality defined on Wikipedia?
- Disputed: Do uncountable sets exist?
- Not disputed: is the existence of uncountable sets provable in classical set theory (yes)? What about in intuitionistic set theory (no)?