how to define infix function
Ali Çehreli
acehreli at yahoo.com
Sat Jun 2 22:01:02 UTC 2018
On 06/02/2018 02:44 PM, greatsam4sure wrote:
> is it possible to define infix function in D
>
> 3.min(5)// 3: where min is a function, works in D
> 3 min 5 // does not work.
This is called universal function call syntax (UFCS) in D. The idea is
simple: You can pull the first argument out as if the function is a
member function of that argument. So, in your case it already works
because there is already a min() function that works with two (actually,
many) parameters:
import std.algorithm;
void main() {
assert(min(3, 5) == 3);
assert(3.min(5) == 3);
}
However, many people don't like overusing it; it works well only in
cases where the first parameter is arguably the "main character" in the
operation. For example, min doesn't look good because all parameters
have equal roles in that function.
Ali
More information about the Digitalmars-d-learn
mailing list