Associative array literal. Why doesn't it compile?

2015-08-14 Thread Adel Mamin via Digitalmars-d-learn
Compiler dmd_2.068.0-0_amd64.deb on Ubuntu 12.04 Linux: auto lookup = [ one:1, two:2 ]; The dmd error: Error: non-constant expression [one:1, two:2] Why doesn't it compile? As a workaround I could do the assignment one element at a time in a loop. It would be uglier though.

Type inference of a function parameter

2015-07-29 Thread Adel Mamin via Digitalmars-d-learn
Hello, Why dmd cannot inference the type of 'arr' in my_func() parameter? test.d: import std.stdio; void my_func(auto arr) { writeln(arr); } void main() { auto arr = new int[5]; arr = [1, 2, 3, 4, 5]; my_func(arr); } dmd test.d test.d(3): Error: undefined identifier arr Adel

.sizeof dynamically allocated array

2015-06-11 Thread Adel Mamin via Digitalmars-d-learn
import std.stdio; void main() { ubyte[] a1 = new ubyte[65]; ubyte[65] a2; writeln(a1.sizeof = , a1.sizeof); // prints 16 writeln(a2.sizeof = , a2.sizeof); // prints 65 } Why a1.sizeof is 16?

Shortest way to allocate an array and initialize it with a specific value.

2015-06-10 Thread Adel Mamin via Digitalmars-d-learn
ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes. auto a2 = new ubyte[5]; // Fine. Five 0 bytes. Now, let's say, I want to allocate an array of a size, derived at run time, and initialize it to some non-zero value at the same time. What would be the shortest way of doing it?