Re: Template specialized functions creating runtime instructions?

2019-08-20 Thread ads via Digitalmars-d-learn

On Wednesday, 21 August 2019 at 00:04:37 UTC, H. S. Teoh wrote:
On Tue, Aug 20, 2019 at 11:48:04PM +, ads via 
Digitalmars-d-learn wrote: [...]
2) Deducing the string as you describe would require CTFE 
(compile-time function evaluation), which usually isn't done 
unless the result is *required* at compile-time.  The typical 
way to force this to happen is to store the result into an enum:


enum myStr = fizzbuzz!...(...);
writeln(myStr);

Since enums have to be known at compile-time, this forces CTFE 
evaluation of fizzbuzz, which is probably what you're looking 
for here.


T


Thank you for clearing those up. However even if I force CTFE 
(line 35), it doesn't seem to help much.


https://godbolt.org/z/MytoLF


Re: Template specialized functions creating runtime instructions?

2019-08-20 Thread ads via Digitalmars-d-learn

On Tuesday, 20 August 2019 at 23:48:04 UTC, ads wrote:

https://godbolt.org/z/hWENgc


A somewhat similar translation in C++ also creates a lot of 
runtime instructions

https://godbolt.org/z/psyUtq


Template specialized functions creating runtime instructions?

2019-08-20 Thread ads via Digitalmars-d-learn
This piece of code creates a fizzbuzz string with template 
parameters.


auto fizzbuzz(uint N)() {
string accumulate;
return fizzbuzz!N(accumulate);
}

auto fizzbuzz(uint N)(ref string result) if (N % 3 && N % 5) {
import std.conv : to;

result ~= N.to!string ~ "\n";
return fizzbuzz!(N - 1)(result);
}

auto fizzbuzz(uint N)(ref string result) if (!(N % 15)) {
result ~= "FizzBuzz\n";
return fizzbuzz!(N - 1)(result);
}

auto fizzbuzz(uint N)(ref string result) if (!(N % 3) && N % 5) {
result ~= "Fizz\n";
return fizzbuzz!(N - 1)(result);
}

auto fizzbuzz(uint N)(ref string result) if (!(N % 5) && N % 3) {
result ~= "Buzz\n";
return fizzbuzz!(N - 1)(result);
}

auto fizzbuzz(uint N : 0)(ref string result) {
return result;
}

void main() {
import std.stdio : writeln;

fizzbuzz!50().writeln();
}


https://godbolt.org/z/hWENgc

In the generated assembly, it looks like it is creating a lot of 
runtime instructions, contrary to my belief that templated codes 
are purely compile-time. I was expecting that the compiler would 
deduce the fizzbuzz string until 50 in compile-time and just 
print it in the run-time. Why is this not the case?


Can't add a const ubyte to a dynamic array of ubyte?

2019-08-20 Thread ads via Digitalmars-d-learn

import std.stdio;

ubyte[] extend(in uint[] arr)
{
ubyte[] result;
foreach (n; arr)
{
if (n < 10)
{
result ~= n;
// source/app.d(10,11): Error: cannot 
append type const(uint) to type ubyte[]

}
else
{
import std.conv : to;

foreach (digit; n.to!string)
{
result ~= digit.to!ubyte;
}
}
}
return result;
}

unittest
{
import std.algorithm : equal;

assert(extend([1, 25, 70, 0]).equal([1, 2, 5, 7, 0, 0]));
}


How can I get around this? I want to ensure that the array is not 
mutated in the function in the signature too.