Function template advice
Jordan Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jan 18 14:52:53 PST 2017
On Wednesday, 18 January 2017 at 22:39:02 UTC, Ali Çehreli wrote:
> On 01/18/2017 02:02 PM, Jordan Wilson wrote:
>> [...]
>
> Yes, can be better with something similar to the following:
>
> struct Foo {
> string bar;
> }
>
> string value(U : Foo)(U u) {
> return u.bar;
> }
>
> string value(U : string)(U u) {
> return u;
> }
>
> auto sameGroup(T,S) (T a, S b) {
> static assert (is(T == string) || is(T == Foo));
> static assert (is(S == string) || is(S == Foo));
>
> import std.algorithm;
> return equal (value(a), value(b));
> }
>
> void main() {
> assert(sameGroup("a", "b") == false);
> assert(sameGroup("a", Foo("a")) == true);
> assert(sameGroup(Foo("x"), "b") == false);
> assert(sameGroup(Foo("z"), Foo("z")) == true);
> }
>
> Ali
Nice, yes looks better thanks.
Jordan
More information about the Digitalmars-d-learn
mailing list