Parameters declared as the alias of a template won't accept the arguments of the same type.

Ali Çehreli acehreli at yahoo.com
Tue May 3 15:41:08 UTC 2022


	On 5/2/22 13:36, Stanislav Blinov wrote:

 >> That's fine because D does not promise to solve such problems. It
 >> follows simple deduction rules.
 >
 > Words, dear guru. Words have a meaning. It is very possible to deduce
 > here (note the premise). D just isn't trying. That's what I said. You
 > can say "it doesn't promise". Doesn't exactly change the meaning, does
 > it? :)

What I mean is, problem solving the way you expect is not in D's charter.

 >> > The function takes an int.
 >>
 >> How would the solution be? Wouldn't the compiler have to parse all
 >> accessible template bodies to figure out which ones fit?
 >
 > Err... yes? That's what it *is* doing already, for all templates.

No, the compiler does not parse all templates. Only the ones that needs 
instantiation.

 >> Imagine the following two other templates:
 >>
 >> template YourAlias(T) {
 >>   alias YourAlias = int;  // Is it this one?
 >> }
 >>
 >> template HerAlias(T) {
 >>   // ...
 >>   alias HerAlias = HisAlias!SomeNameInThisScope;  // Or thi?
 >> }
 >
 > No. HerAlias and YourAlias are different symbols.

You are right. I confused myself there but it is not that different when 
we stay with MyAlias alone. I am changing 'int' to YourAlias!T:

   template MyAlias(T){
     alias MyAlias = YourAlias!T;
   }

Do you still expect the compiler to dig into YourAlias and solve a 
problem to see whether YourAlias can be the same as aliasing 'int'? 
Would that enough trying by the compiler? If not, where should the 
compiler stop? In any case, that is not what D is. That's what I meant.

Not being even a junior guru, I don't know what categories of 
programming languages do that. Declarative? Logic? All I know is D is 
not among them.

 >> The compiler would have to solve this problem by digging into
 >> HisAlias's SomeNameInThisScope instantiation as well. As far as know,
 >> the D language does not work that way. Prolog and friends perhaps?
 >
 > Nope. MyAlias is an alias. A template alias, but an alias nonetheless.
 > It should be resolved prior to trying the overloads.

Not going to happen.

 >> > Doesn't take a rocket
 >> > scientist to figure that one out.
 >>
 >> I think this is one of those cases where it is easier for a human.
 >> Although, I think I would have difficulty if there were more than one
 >> template parameter.
 >
 > That's what we have compilers for.

We also have NP-completeness.

 >> > I might as well forego the templates and
 >> > just write explicit overloads. At which point I would
 >> question why use
 >> > templates at all.
 >>
 >> Templates allow single implementations to work for many types,
 >> manifest constants, aliases, etc. I wouldn't want to write (or
 >> mix-in?) sort() for MyType. Templates are wonderful and their
 >> implementation in D is refreshing.
 >
 > It is, until is isn't. Question du jeour is one of those.

No, the question is whether D is in a category of programming languages 
that it isn't.

Ali



More information about the Digitalmars-d-learn mailing list