Thread overview
compiler assertion
Sep 28, 2012
Zhenya
Sep 28, 2012
Jonathan M Davis
Sep 28, 2012
Zhenya
Sep 29, 2012
Philippe Sigaud
Sep 29, 2012
Zhenya
September 28, 2012
Hi!
Is it normally,that this simple code does'nt compile with this assertion:
dmd: template.c:5542: Identifier* TemplateInstance::genIdent(Objects*): Assertion `global.errors' failed.

import std.stdio;
import std.typetuple;

template sum(T:TypeTuple!U,U...)
{
	static if(U.length == 0)
		enum sum = 0;
	else
		enum sum = U[0]+sum!(U[1..$]);
}

void main()
{
	enum s = sum!(1,2,3,4);
	writeln(s);
}

?

September 28, 2012
On Friday, September 28, 2012 22:19:56 Zhenya wrote:
> Hi!
> Is it normally,that this simple code does'nt compile with this
> assertion:
> dmd: template.c:5542: Identifier*
> TemplateInstance::genIdent(Objects*): Assertion `global.errors'
> failed.

It's always a bug in the compiler if you see a compiler assertion. What you're seeing is probably this bug:

http://d.puremagic.com/issues/show_bug.cgi?id=8421

- Jonathan M Davis
September 28, 2012
On Friday, 28 September 2012 at 20:27:07 UTC, Jonathan M Davis wrote:
> On Friday, September 28, 2012 22:19:56 Zhenya wrote:
>> Hi!
>> Is it normally,that this simple code does'nt compile with this
>> assertion:
>> dmd: template.c:5542: Identifier*
>> TemplateInstance::genIdent(Objects*): Assertion `global.errors'
>> failed.
>
> It's always a bug in the compiler if you see a compiler assertion. What you're
> seeing is probably this bug:
>
> http://d.puremagic.com/issues/show_bug.cgi?id=8421
>
> - Jonathan M Davis

Thank you,understood.
September 29, 2012
On Fri, Sep 28, 2012 at 10:32 PM, Zhenya <zheny@list.ru> wrote:

> Thank you,understood.

This should work, hopefully:

import std.stdio;
import std.typetuple;

template sum(U...)
{
        static if(U.length == 0)
                enum sum = 0;
        else
                enum sum = U[0]+sum!(U[1..$]);
}

void main()
{
        enum s = sum!(1,2,3,4);
        writeln(s);
}

You can also do it with a standard function, and forcing its execution at compile-time by using 'enum':


U[0] sum(U...)(U u) if (U.length > 0)
{
    U[0] result =0;
    foreach(value;u)
        result += value;
    return result;
}

void main()
{
        enum s = sum(1,2,3,4);
        writeln(s);
}

Note that doing that without any check on the U's is a bit dangerous.
September 29, 2012
On Saturday, 29 September 2012 at 13:03:31 UTC, Philippe Sigaud wrote:
> On Fri, Sep 28, 2012 at 10:32 PM, Zhenya <zheny@list.ru> wrote:
>
>> Thank you,understood.
>
> This should work, hopefully:
>
> import std.stdio;
> import std.typetuple;
>
> template sum(U...)
> {
>         static if(U.length == 0)
>                 enum sum = 0;
>         else
>                 enum sum = U[0]+sum!(U[1..$]);
> }
>
> void main()
> {
>         enum s = sum!(1,2,3,4);
>         writeln(s);
> }
>
> You can also do it with a standard function, and forcing its execution
> at compile-time by using 'enum':
>
>
> U[0] sum(U...)(U u) if (U.length > 0)
> {
>     U[0] result =0;
>     foreach(value;u)
>         result += value;
>     return result;
> }
>
> void main()
> {
>         enum s = sum(1,2,3,4);
>         writeln(s);
> }
>
> Note that doing that without any check on the U's is a bit dangerous.

I just wanted to do something like template,that takes value parametres and deduce it's types,so I guess that I can use code that you wrote for it with some type checking.