18.02.2012 0:33, Joshua Niehus пишет:
Not as fancy as the other submits, but it might be worthy of the front
page:

import std.stdio, std.traits;

void main(string[] args) {
    auto foo(T)(T n) {
        return delegate(T i) {
            static if (isSomeString!(T))
                auto m = mixin("n ~ i");
            else
                auto m = mixin("n + i");
            return m;
        };
    }
    writeln(foo("Hello")(" World!"));
    writeln(foo(18)(24)); }


Some remarks:
0. `string[] args` isn't needed;
1. `delegate` is not needed;
2. `isSomeString!(T)` can be replaced by `isSomeString!T`;
3. `static if` can be replaced by `auto m = mixin(isSomeString!T ? "n ~ i" : "n + i");`;
4. With nnew `=>` syntax function body can be replaced by
`return (T i) => mixin(isSomeString!T ? "n ~ i" : "n + i");`
or
`return (T i) => mixin("n" ~ (isSomeString!T ? '~' : '+') ~ 'i');`


So your code can looks like this:
---
import std.stdio, std.traits;

void main() {
    auto foo(T)(T n) {
        return (T i) => mixin("n" ~ (isSomeString!T ? '~' : '+') ~ 'i');
    }
    writeln(foo("Hello")(" World!"));
    writeln(foo(18)(24));
}
---
or even like this:
---
import std.stdio, std.traits;

void bar(alias foo)() {
    writeln(foo("Hello")(" World!"));
    writeln(foo(18)(24));
}

void main() {
bar!(n => (typeof(n) i) => mixin("n" ~ (isSomeString!(typeof(n)) ? '~' : '+') ~ 'i'))();
}
---

IMHO, all these three versions should be on the site grouped somehow (i.e. one can navigate between analogous).

Reply via email to