Search
Page
TypeFunction example creatiing a conversion matrix
Re: TypeFunction example creating a conversion matrix
Oct 01
user1234
Oct 01
user1234
Oct 01
user1234
Oct 01
jmh530
Oct 01
user1234
Oct 01
user1234
Oct 01
user1234
Oct 01
jmh530
Oct 01
Araq
```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

--
```
```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.
```
```On Thursday, 1 October 2020 at 08:21:24 UTC, Stefan Koch wrote:

> string makeConvMatrix(alias[] types ...)

Wait .... What the Hell?
Why did this compile?

I must have fixed parsing `alias[]` ?

```
```On Thursday, 1 October 2020 at 08:26:53 UTC, Stefan Koch wrote:
> On Thursday, 1 October 2020 at 08:21:24 UTC, Stefan Koch wrote:
>
>> string makeConvMatrix(alias[] types ...)
>
> Wait .... What the Hell?
> Why did this compile?
>
> I must have fixed parsing `alias[]` ?

By the way why cant you pass directly the types (i.e lowercase builtins) ?
```
```On Thursday, 1 October 2020 at 08:38:43 UTC, user1234 wrote:
> On Thursday, 1 October 2020 at 08:26:53 UTC, Stefan Koch wrote:
>> On Thursday, 1 October 2020 at 08:21:24 UTC, Stefan Koch wrote:
>>
>>> string makeConvMatrix(alias[] types ...)
>>
>> Wait .... What the Hell?
>> Why did this compile?
>>
>> I must have fixed parsing `alias[]` ?
>
> By the way why cant you pass directly the types (i.e lowercase builtins) ?

parser limitations.

The grammar won't allow you to do that.

```
```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

```
```On Thursday, 1 October 2020 at 08:52:46 UTC, Stefan Koch wrote:
> On Thursday, 1 October 2020 at 08:38:43 UTC, user1234 wrote:
>> On Thursday, 1 October 2020 at 08:26:53 UTC, Stefan Koch wrote:
>>> On Thursday, 1 October 2020 at 08:21:24 UTC, Stefan Koch wrote:
>>>
>>>> string makeConvMatrix(alias[] types ...)
>>>
>>> Wait .... What the Hell?
>>> Why did this compile?
>>>
>>> I must have fixed parsing `alias[]` ?
>>
>> By the way why cant you pass directly the types (i.e lowercase builtins) ?
>
> parser limitations.
>
> The grammar won't allow you to do that.

Ah indeed. Eventually you could imagine a syntax that makes the parser take a special path but I suppose that for now  this is just a detail.
```
```On Thursday, 1 October 2020 at 08:57:12 UTC, Stefan Koch wrote:
>
> 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

Let me illustrate the user visible diffrence:
type function:
string makeConvMatrix(alias[] types ...)  // 0.245 times the size of template object file
Included symbols (extracted with nm)
0000000000000000 R _D14makeConvMatrix12__ModuleInfoZ
U _d_dso_registry
U __start_minfo
U __stop_minfo

template:
string makeConvMatrix(types ...)()        // 4 times the size of typefunction object file
included symbols (extracted with nm)
U _D12TypeInfo_Aya6__initZ
0000000000000000 R _D14makeConvMatrix12__ModuleInfoZ
0000000000000000 W _D14makeConvMatrix__TQtTgThTsTtTiTkTlTmZQBmFNaNbNfZAya
U _d_arrayappendT
U _d_dso_registry
U _GLOBAL_OFFSET_TABLE_
U __start_minfo
U __stop_minfo
0000000000000000 r _TMP0
0000000000000002 r _TMP1
0000000000000038 r _TMP10
000000000000003d r _TMP11
0000000000000042 r _TMP12
0000000000000048 r _TMP13
000000000000004e r _TMP14
0000000000000052 r _TMP15
0000000000000059 r _TMP16
000000000000005d r _TMP17
0000000000000062 r _TMP18
0000000000000067 r _TMP19
0000000000000008 r _TMP2
000000000000000f r _TMP3
0000000000000016 r _TMP4
000000000000001e r _TMP5
0000000000000023 r _TMP6
0000000000000029 r _TMP7
000000000000002f r _TMP8
0000000000000036 r _TMP9

```
```On Thursday, 1 October 2020 at 09:28:34 UTC, Stefan Koch wrote:
> On Thursday, 1 October 2020 at 08:57:12 UTC, Stefan Koch wrote:
>
> Let me illustrate the user visible diffrence:
> type function:
> string makeConvMatrix(alias[] types ...)  // 0.245 times the size of template object file
> Included symbols (extracted with nm)
> 0000000000000000 R _D14makeConvMatrix12__ModuleInfoZ
>                  U _d_dso_registry
>                  U __start_minfo
>                  U __stop_minfo
>
> template:
> string makeConvMatrix(types ...)()        // 4 times the size of typefunction object file
> included symbols (extracted with nm)
>                  U _D12TypeInfo_Aya6__initZ
> 0000000000000000 R _D14makeConvMatrix12__ModuleInfoZ
> 0000000000000000 W _D14makeConvMatrix__TQtTgThTsTtTiTkTlTmZQBmFNaNbNfZAya
>                  U _d_arrayappendT

And btw the fact that you don't see U _d_arrayappendT in the imported symbols of the type function ... means it works with -betterC!
```
```On Thursday, 1 October 2020 at 09:33:28 UTC, Stefan Koch wrote:
> On Thursday, 1 October 2020 at 09:28:34 UTC, Stefan Koch wrote:
>> On Thursday, 1 October 2020 at 08:57:12 UTC, Stefan Koch wrote:
>>
>> Let me illustrate the user visible diffrence:
>> type function:
>> string makeConvMatrix(alias[] types ...)  // 0.245 times the size of template object file
>> Included symbols (extracted with nm)
>> 0000000000000000 R _D14makeConvMatrix12__ModuleInfoZ
>>                  U _d_dso_registry
>>                  U __start_minfo
>>                  U __stop_minfo
>>
>> template:
>> string makeConvMatrix(types ...)()        // 4 times the size of typefunction object file
>> included symbols (extracted with nm)
>>                  U _D12TypeInfo_Aya6__initZ
>> 0000000000000000 R _D14makeConvMatrix12__ModuleInfoZ
>> 0000000000000000 W _D14makeConvMatrix__TQtTgThTsTtTiTkTlTmZQBmFNaNbNfZAya
>>                  U _d_arrayappendT
>
> And btw the fact that you don't see U _d_arrayappendT in the imported symbols of the type function ... means it works with -betterC!

BTW how is doing the DIP for this ;)
```
« First   ‹ Prev