C++ has a notion of templates, e.g.
The Java generics API is somewhat more concise (ignoring things like inheritance which just don't exist in ASH):
Currently, I find myself duplicating code to change types of maps, e.g.
I'd be happier if I could just write
The functional programmer in me also notes that this could be combined with call to implement various well-known functional programming paradigms, e.g.
Initial comment regarding potential implementation: my understanding is that the compiler creates a new instance of the function for each distinct template specialization.
PHP:
template <typename T>
T max(T a, T b) {
return a > b ? a : b;
}
...
max<int>(4, 13); // returns 13
The Java generics API is somewhat more concise (ignoring things like inheritance which just don't exist in ASH):
PHP:
<T> T max(T a, T b) {
return a > b ? a : b;
}
PHP:
string as_list(boolean[string] stuff) {
buffer s;
foreach thing in stuff {
if (s.length() > 0) {
s.append(", ");
}
s.append(thing);
}
return s.to_string();
}
string as_list(boolean[item] stuff) {
buffer s;
foreach thing in stuff {
if (s.length() > 0) {
s.append(", ");
}
s.append(thing);
}
return s.to_string();
}
...
as_list(item_array);
as_list(string_array);
PHP:
<T> string as_list(boolean[T] stuff) {
...
}
...
as_list<item>(item_array);
as_list<string>(string_array);
The functional programmer in me also notes that this could be combined with call to implement various well-known functional programming paradigms, e.g.
PHP:
<A> A[int] filter(string function_name, A[int] input) {
A[int] output;
foreach i in input {
if (call boolean function_name(input[i])) {
output[count(output)] = input[i];
}
}
return output;
}
Initial comment regarding potential implementation: my understanding is that the compiler creates a new instance of the function for each distinct template specialization.
Last edited: