TypeFunction example creating a conversion matrix

user1234 user1234 at 12.de
Thu Oct 1 08:23:22 UTC 2020


On Thursday, 1 October 2020 at 08:21:24 UTC, Stefan Koch wrote:
> Hi People,
>
> To further show the intuitive type function syntax I just 
> created a way to print a conversion matrix.
>
> Here is the code:
>
> 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;
> }
>
> alias Byte = byte;
> alias Ubyte = ubyte;
> alias Short = short;
> alias Ushort = ushort;
> alias Int = int;
> alias Uint = uint;
> alias Long = long;
> alias Ulong = ulong;
>
> void main()
> {
>     import std.stdio;
>     static immutable convMatrix = makeConvMatrix(Byte, Ubyte, 
> Short, Ushort, Int, Uint, Long, Ulong);
>
>     printf("%s\n", convMatrix.ptr);
> }
>
> And here is the output:
>
> ----
>  	byte 	ubyte 	short 	ushort 	int 	uint 	long 	ulong
> byte	yes 	yes 	yes 	yes 	yes 	yes 	yes 	yes
> ubyte	yes 	yes 	yes 	yes 	yes 	yes 	yes 	yes
> short	no 	no 	yes 	yes 	yes 	yes 	yes 	yes
> ushort	no 	no 	yes 	yes 	yes 	yes 	yes 	yes
> int	no 	no 	no 	no 	yes 	yes 	yes 	yes
> uint	no 	no 	no 	no 	yes 	yes 	yes 	yes
> long	no 	no 	no 	no 	no 	no 	yes 	yes
> ulong	no 	no 	no 	no 	no 	no 	yes 	yes
>
> --

nice.


More information about the Digitalmars-d mailing list