Argumnentation against external function operator overloading is unconvincing

krzaq via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 24 09:10:53 PDT 2016


On Wednesday, 21 September 2016 at 21:14:15 UTC, H. S. Teoh wrote:
>
> The problem here is that generic_code.d doesn't (and 
> shouldn't!) import
> usertype.d, so usertype.opBinary is not visible in 
> generic_code.d. So
> when algorithm() tries to look up the '+' operator in `t + u`, 
> it can't
> find the declaration and fails.  There is no way to find the 
> correct
> opBinary() because it's not part of UserType, so algorithm() 
> has no way
> to access that symbol.
>
> Using the operator in module main is OK, because main 
> (rightfully) imports usertype.d, so the operator is visible. 
> But any generic code that main imports will have a problem 
> because they can't (and shouldn't!) know ahead of time which 
> modules contain the declaration they need.
>
> [...]
>
> T

Why wouldn't templates just pull the "context" into them? I'm 
sorry if this is a naïve question.


More information about the Digitalmars-d mailing list