Remix.run Logo
Doxin a day ago

Honestly the main reason UFCS is good is when you don't "own" the type your function would need to be a member of. Most common one I run into is the "to" function from the stdlib. You can do this:

    import std.conv;
    
    int foo=3;
    string bar = foo.to!string
But now lets say you want to convert ints to MyCustomInts? You can hardly attach a new "to" member to int, but with UFCS it's easy. Just declare a to function anywhere in scope:

    MyCustomInt to(T)(T v) if(is(T==int)){
        return MyCustomInt.from_int_value(v)
        // or however you actually do the conversion
    }
and it'll magically work:

    int foo=3;
    MyCustomInt bar = foo.to!MyCustomInt;