TypeFunction example creatiing a conversion matrix

Stefan Koch uplink.coder at gmail.com
Thu Oct 1 08:57:12 UTC 2020


On Thursday, 1 October 2020 at 08:21:24 UTC, Stefan Koch wrote:
>
> string makeConvMatrix(alias[] types ...)
> {
>     string result;
>     foreach(t;types)
>     {
>         result ~= "\t" ~ t.stringof;
>     }
>     result ~= "\n";
>     foreach(t1;types)
>     {
>         result ~= t1.stringof;
>         foreach(t2;types)
>         {
>             result ~=  "\t" ~ (is(t1:t2) ? "yes" : "no");
>         }
>         result ~= "\n";
>     }
>     return result;
> }
>

Let's compare this now to a template function which does the same 
thing.

---
     string makeConvMatrix(types ...)()
     {
         string result = " ";
         foreach(t;types)
         {
             result ~= "\t" ~ t.stringof;
         }
         result ~= "\n";
         foreach(t1;types)
         {
             result ~= t1.stringof;
             foreach(t2;types)
             {
                 result ~=  "\t" ~ (is(t1:t2) ? "yes" : "no");
             }
             result ~= "\n";
         }
         return result;
     }
---

At those scales there is no difference in compile time.
However there is a difference in file size.

Type function version:

stat  makeConvMatrix.o
   File: 'makeConvMatrix.o'
   Size: 2876

VS template version

stat  makeConvMatrix_tmpl.o
   File: 'makeConvMatrix_tmpl.o'
   Size: 11760



More information about the Digitalmars-d mailing list