Am I evil for this?

Dennis dkorpel at gmail.com
Fri Oct 14 21:49:31 UTC 2022


On Friday, 14 October 2022 at 20:24:47 UTC, jmh530 wrote:
> So for instance, you can make a function
> ```
> `%foo%` <- function(x, y) { #does something }
> ```
> and use it like `x %foo% y`

You can actually do that in D with some hacks:

```D
struct Operator(alias fn, string operator = "/")
{
     static auto opBinaryRight(string op : operator, T...)(T 
value1)
     {
         struct Result
         {
             auto opBinary(string op : operator, U...)(U value2)
                 if (__traits(compiles, fn(value1, value2)))
             {
                 return fn(value1, value2);
             }
         }

         Result result;
         return result;
     }
}

void main()
{
     import std.algorithm.comparison;
     alias min = Operator!(std.algorithm.comparison.min, "%");
     assert(1 %min% 3 == 1);
}
```

Credit to Simen Kjærås:
https://forum.dlang.org/post/ldiwiffdyzeswggytudh@forum.dlang.org



More information about the Digitalmars-d mailing list