January 12, 2021
On Tuesday, 12 January 2021 at 21:38:59 UTC, sighoya wrote:
> What about this?
> No magic, but I don't know the performance impact.
>
> ```
> import std.meta;
> import std.conv;
>
> template same(Types...)
> {
>     static if (Types.length >= 2)
>     {
>         static if (is(Types[0] == Types[$ - 1]))
>         {
>             const same = same!(Types[1 .. $]);
>         }
>         else
>         {
>             enum bool same = false;
>         }
>     }
>     else
>     {
>         enum bool same = true;
>     }
> }

I also like the recursive version better. I borrowed the core concept from Phobos. I assume they don't use recursion because they are afraid of running out of stack space? Or maybe it is because of performance?


> struct Tuple(Types...)
> {
>
>     static if (same!Types)
>     {
>         public Types[0][Types.length] elements;

Yes, this is what I also felt was the right approach. But then I realized that AliasSeq (which is what you get if you use "Types expand") is hardwired in the compiler in a hackish way so that you get various features from it, like implicit splatting (expanding the sequence into arguments)?  That kinda makes it difficult to get the right semantics as I know of no way to do it without AliasSeq, but maybe there is some way, perhaps one can convert it into an AliasSeq somehow.

I kinda don't like this kind of compiler-magic for one specific type as it limits what you can do, but I guess it was done for easy-of-implementation.

>         static foreach (int i, T; Types)
>         {
>             mixin("public " ~ T.stringof ~ " " ~ "elem" ~ i.stringof ~ ";");
>         }

Yep, I had this version at some point :-)

I think the way you approached it is the intuitive way, but the problem is really that AliasSeq get special treatment by the compiler so it is currently difficult to work around it?

One should probably look closer at the language semantics and try to come up with a more generic mechanism as that would make for more powerful meta programming.



1 2 3
Next ›   Last »