3 ms·
D language lets you define your template(s) in the following format: auto add(T)(T lhs, T rhs) { return lhs + rhs; } and to instantiate it you
by stephen82 7y ago
D language lets you define your template(s) in the following format:
auto add(T)(T lhs, T rhs) {
return lhs + rhs;
}
and to instantiate it you do the following:
add!int(5, 10);
add!float(5.0f, 10.0f);
// won't compile; Animal doesn't implement +
add!Animal(dog, cat);
Personally, as a C++ adventurist I find the instantiating syntax a bit confusing, but quite tolerable.
I wonder whether anyone attempted to use "|T|" as their template syntax...Ruby uses it in iterators (I think) and if I'm not mistaken Rust uses it with closures.
Wouldn't be a bit cleaner to have something like the following?
|T| something (T foo) -> T {
// do something in here
return foo;
}
Personally I prefer it.
- Gaelan 7y ago> Ruby uses it in iterators (I think) and if I'm not mistaken Rust uses it with closures. Ruby and Rust both use them for closures (I assume Rust took them from Ruby). Nearly all of Ruby's iteration constructs are based on closures, so it makes sense that you understood them as for iterators.