How to create a custom max() function without the ambiguity error.

Andrea Fontana nospam at
Fri Dec 6 13:33:40 UTC 2019

On Friday, 6 December 2019 at 12:34:17 UTC, realhet wrote:
> Hi,
> I'm trying to use a popular function name "max", and extend it 
> for my special types:
> For example:
> module utils;
> MyType max(in MyType a, in MyType a){...}  //static function
> struct V3f{
>   V3f max(in V2f b){...} //member function
> }
> module gl3n;
> vec3 max(in vec3 a, in vec3 a) //another static function in 
> different module
> I also want to use std.algorithm.comparison.max as well.
> I know that the ambiguity error can be avoided by public 
> importing all the various max implementations into one place, 
> but how to do this when I want to use 3 modules? Need a 4th 
> module to combine them all? Is there a nicer way?
> Another question that is it possible to extend the the template 
> function "to" in more than one module and use it easily from my 
> main project modules?

What about:

int max(int a, int b){ return 0; }

void main()
     .max(3,4).writeln;  // 0
     std.algorithm.comparison.max(3,4).writeln; // 4

More information about the Digitalmars-d-learn mailing list