-
Enhancement
-
Resolution: Duplicate
-
P5
-
None
-
1.2.0, 6
-
generic, x86
-
generic, windows_xp
A DESCRIPTION OF THE REQUEST :
As an example I suggest that the following should be possible:
(class String)
public String replace(String regex, String replaceFunc(int pos, int len, String match)) {
...
String replacement = replaceFunc(...);
}
One would then be able to define callback functions "on the fly", e.g.
String y = x.replace("\\W", String myReplace1(int pos, int len, String match) {
if (!match.equals(" ")) return "." else return "_";
});
[myReplace1 would in this case be an additional method of the "this" object] Alternatively, method definitions could be anonymous, such as:
String y = x.replace("\\W", String (int pos, int len, String match) {
...
});
Or, one could pass in predefined functions of this or another object; these methods might also be static (?):
(class Other)
static String myReplace(int pos, int len, String match) {
if (!match.equals(" ")) return "." else return "_";
}
...
String y = x.replace("\\W", Other.myReplace);
Or, one could pass in instances of the Method class obtained via reflection; type safety must then be checked at runtime:
Method replace = ...;
String y = x.replace("\\W", replace);
As a matter of fact, these method parameters should support generics.
JUSTIFICATION :
This would be an important step towards functional programming.
I don't know if this would raise extremely difficult problems. For now it seems pretty straightforward except maybe for the reflection thing. Of course, the reflection API would have to be extended.
Maybe this can serve as a basis for discussion.
As an example I suggest that the following should be possible:
(class String)
public String replace(String regex, String replaceFunc(int pos, int len, String match)) {
...
String replacement = replaceFunc(...);
}
One would then be able to define callback functions "on the fly", e.g.
String y = x.replace("\\W", String myReplace1(int pos, int len, String match) {
if (!match.equals(" ")) return "." else return "_";
});
[myReplace1 would in this case be an additional method of the "this" object] Alternatively, method definitions could be anonymous, such as:
String y = x.replace("\\W", String (int pos, int len, String match) {
...
});
Or, one could pass in predefined functions of this or another object; these methods might also be static (?):
(class Other)
static String myReplace(int pos, int len, String match) {
if (!match.equals(" ")) return "." else return "_";
}
...
String y = x.replace("\\W", Other.myReplace);
Or, one could pass in instances of the Method class obtained via reflection; type safety must then be checked at runtime:
Method replace = ...;
String y = x.replace("\\W", replace);
As a matter of fact, these method parameters should support generics.
JUSTIFICATION :
This would be an important step towards functional programming.
I don't know if this would raise extremely difficult problems. For now it seems pretty straightforward except maybe for the reflection thing. Of course, the reflection API would have to be extended.
Maybe this can serve as a basis for discussion.
- duplicates
-
JDK-4193581 Pointer to function
-
- Closed
-
-
JDK-6315058 Anonymous functions
-
- Closed
-
-
JDK-5061325 Add language support for functors (function objects)
-
- Closed
-
- relates to
-
JDK-6433012 A simplified syntax for using Runnable
-
- Closed
-
-
JDK-6500704 Add language support for converting methods to interfaces
-
- Closed
-