Thread overview
A very small update on typefunctions
Jul 28, 2020
Stefan Koch
Jul 28, 2020
Stefan Koch
Jul 29, 2020
Per Nordlöw
Jul 29, 2020
Stefan Koch
Jul 29, 2020
jmh530
Aug 01, 2020
Stefan Koch
July 28, 2020
Hi there,

After a hunt of segfaults, I wanted to have some fun again.

So ....

I've done some work on my typefunctions.

And out came this working example.
It is very similar to one which I have posted previously.

auto getUDAs(alias T)
{
    return __traits(getAttributes, T);
}

// we can't parse alias[];
alias alias_array = typeof(getUDAs(SerializeTag));

struct SerializeTag {}
struct SuperTag {string s;}

struct TypeWithTags
{
    alias type;
    alias_array tags;
}


TypeWithTags typeWithFilteredTags(alias T)
{
    auto udas = T.getUDAs;
    alias_array syms_to_be_kept = [];
    syms_to_be_kept.length = udas.length;
    size_t n_syms;

    for(size_t i = 0; i < udas.length;i++)
    {
        alias uda;
        uda = udas[i];
        if (uda.stringof[0 .. 3] == "Sup")
        {
            syms_to_be_kept[n_syms++] = uda;
        }
    }

    return TypeWithTags(T, syms_to_be_kept[0 .. n_syms]);
}

@SuperTag("Smoking Super Style") @SerializeTag @SuperTag("Super!")
struct  ThisIsAType  { double y; }

alias x = ThisIsAType;

pragma(msg, "typeWithTags: ", typeWithFilteredTags(x));
//output: TypeWithTags((ThisIsAType), [SuperTag("Smoking Super Style"), SuperTag("Super!")]

However there is one important difference.
You can now assign to the length of an alias array.
Removing the use of the inefficient "~=" !

This means efficient implementation of type maps and type filters can now be done on the preview branch :)
Which has also been update to run on the latest "stable" release.

Stay tuned very soon I will show first performance comparison between staticMap + staticFilter template style.
And a handwritten typefunction.

Cheers,

Stefan
July 28, 2020
On Tuesday, 28 July 2020 at 23:12:28 UTC, Stefan Koch wrote:
> Hi there,
>
> After a hunt of segfaults, I wanted to have some fun again.
>
> So ....
>
> I've done some work on my typefunctions.

I would have almost forgotten to link to the branch it's here: https://github.com/UplinkCoder/dmd/tree/talias_stable
There is also an open draft PR so I can make sure to pass the test-suite.
https://github.com/dlang/dmd/pull/11146

In order to pass the testsuite the ability to use type as function arguments and therefore typefunctions themselves are currently hidden behind a switch.
To activate typefunctions you need to pass -sktf to the compiler.

Happy Hacking!
July 29, 2020
On Tuesday, 28 July 2020 at 23:12:28 UTC, Stefan Koch wrote:
> I've done some work on my typefunctions.

Nice. Does the evaluation of type functions reuse CTFE or is the logic completely separate?

Has there been any community feedback on the syntax for type functions?

Will type functions enable anything more than CTFE already does?

Is there / Will you support (static) foreach over type tuples to reduce recursive template instantiations in std.traits?

What features are missing to fully replace existing logic in std.traits with type functions? One way forward to make this thing more motivating is to incrementally convert std.traits to type functions and monitor the benefits in terms of compilation speed and memory usage all over Phobos. Especially the overhead of doing

    import std;

in an empty file.

Keep up your inspiring ideas and work!

/Per
July 29, 2020
On Tuesday, 28 July 2020 at 23:12:28 UTC, Stefan Koch wrote:
> Hi there,
>
> After a hunt of segfaults, I wanted to have some fun again.
>
> So ....
>
> [snip]

Looks pretty cool.

> [snip]
> alias x = ThisIsAType;
>
> pragma(msg, "typeWithTags: ", typeWithFilteredTags(x));
> //output: TypeWithTags((ThisIsAType), [SuperTag("Smoking Super Style"), SuperTag("Super!")]
> [snip]

Is the only way this works by using an alias statement like that? Assuming this works progresses to a more complete and finished state, would there be scope for adjusting the semantics of alias function parameters in some way to handle situations like this? Or alternately, being able to write something like
pragma(msg, "typeWithTags: ", ThiIsAType.alias.typeWithFilteredTags));
July 29, 2020
On Wednesday, 29 July 2020 at 08:41:01 UTC, Per Nordlöw wrote:
> On Tuesday, 28 July 2020 at 23:12:28 UTC, Stefan Koch wrote:
>> I've done some work on my typefunctions.
>
> Nice. Does the evaluation of type functions reuse CTFE or is the logic completely separate?
>
It reuses CTFE.

> Has there been any community feedback on the syntax for type functions?
>
You have to ask the community, so far I guess it's well received.

> Will type functions enable anything more than CTFE already does?
>
> Is there / Will you support (static) foreach over type tuples to reduce recursive template instantiations in std.traits?
>
Yes, that's precisely the main thing that type functions do.

> What features are missing to fully replace existing logic in std.traits with type functions? One way forward to make this thing more motivating is to incrementally convert std.traits to type functions and monitor the benefits in terms of compilation speed and memory usage all over Phobos. Especially the overhead of doing
>
>     import std;
>
> in an empty file.

Uhh I assume at first that won't be easily fixable.

It's meant for thing which are at larger scale than importing std.
I am talking about builds which take more than 10 minutes.

> Keep up your inspiring ideas and work!
>
> /Per


August 01, 2020
On Wednesday, 29 July 2020 at 14:39:02 UTC, jmh530 wrote:
> On Tuesday, 28 July 2020 at 23:12:28 UTC, Stefan Koch wrote:
>> Hi there,
>>
>> After a hunt of segfaults, I wanted to have some fun again.
>>
>> So ....
>>
>> [snip]
>
> Looks pretty cool.
>
>> [snip]
>> alias x = ThisIsAType;
>>
>> pragma(msg, "typeWithTags: ", typeWithFilteredTags(x));
>> //output: TypeWithTags((ThisIsAType), [SuperTag("Smoking Super Style"), SuperTag("Super!")]
>> [snip]
>
> Is the only way this works by using an alias statement like that? Assuming this works progresses to a more complete and finished state, would there be scope for adjusting the semantics of alias function parameters in some way to handle situations like this? Or alternately, being able to write something like
> pragma(msg, "typeWithTags: ", ThiIsAType.alias.typeWithFilteredTags));

Now I see what you are asking.
There is no need to assign the type to an alias first.
pragma(msg, "typeWithTags: ", ThisIsAType.typeWithFilteredTags);
Should work as well.

UFCS support is a little sketchy though in the current Implementation state.