Re: No aa.byKey.length?

2016-04-01 Thread Ali Çehreli via Digitalmars-d-learn

On 04/01/2016 01:50 PM, Yuxuan Shui wrote:

Why?

This is annoying when I need to feed it into a function that requires
hasLength.


Sounds easy to implement. Please file an enhancement request:

  https://issues.dlang.org/

Ali



No aa.byKey.length?

2016-04-01 Thread Yuxuan Shui via Digitalmars-d-learn

Why?

This is annoying when I need to feed it into a function that 
requires hasLength.


Re: How can convert the folowing to D.

2016-04-01 Thread ZombineDev via Digitalmars-d-learn

On Friday, 1 April 2016 at 00:34:49 UTC, learner wrote:

Hi,

I have the following code in C++.

rectangles.erase(rectangles.begin() + index);

where rectangles is:
std::vector rectangles;

how can I do something similar in D.

Learner.


Also, if you are using std.container.array (which similar to 
std::vector in C++), you can use its linearRemove method, by 
giving it a range to remove (a slice of itself), like this:


import std.container.array;
import std.range : iota, drop, take;
import std.stdio : writeln;

void main()
{
auto arr = Array!int(10.iota);  // initialize with [0, 10)

arr[].writeln();

// same as arr.linearRemove(arr[5 .. 7]);
arr.linearRemove(arr[].drop(5).take(2));

arr[].writeln();
}

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 7, 8, 9]

The arr[] syntax is just a shorthand of arr.opSlice() which 
returns a range iterating over the elements of the array. Given 
this range, you can use the algorithms from 
http://dlang.org/phobos/std_range to perform further 
manipulations.


Re: How can convert the folowing to D.

2016-04-01 Thread learner via Digitalmars-d-learn

On Friday, 1 April 2016 at 01:09:32 UTC, Ali Çehreli wrote:

On 03/31/2016 05:34 PM, learner wrote:

Hi,

I have the following code in C++.

rectangles.erase(rectangles.begin() + index);

where rectangles is:
std::vector rectangles;

how can I do something similar in D.

Learner.



import std.stdio;

void main() {
int[] a = [ 1, 2, 3, 4, 5 ];
writefln("Before: %s", a);
size_t index = 2;
a = a[index .. $];   // <-- HERE
writefln("After : %s", a);
}

Note that it's a cheap operation; the elements are still in 
memory and not destroyed. If you want to run their destructors 
you can call destroy() explicitly:


import std.stdio;
import std.algorithm;
import std.range;

struct Rect {
int i;

~this() {
writefln("Destroying %s", i);
}
}

void main() {
Rect[] a = iota(5).map!(i => Rect(i)).array;

writefln("Before: %s", a);

size_t index = 2;

// If you need to run the destructors now:
a[0 .. index].each!((ref e) => e.destroy);

a = a[index .. $];

writefln("After : %s", a);
}

Prints

Destroying 0
Destroying 1
Destroying 2
Destroying 3
Destroying 4
Before: [Rect(0), Rect(1), Rect(2), Rect(3), Rect(4)]
Destroying 0
Destroying 1
After : [Rect(2), Rect(3), Rect(4)]

Ali


thanks


Re: Solution to "statement is not reachable" depending on template variables?

2016-04-01 Thread ZombineDev via Digitalmars-d-learn

On Friday, 1 April 2016 at 01:21:32 UTC, Walter Bright wrote:

On 3/16/2016 4:18 AM, Johan Engelen wrote:
   I've found discussions, but not an actual "recommended" 
solution for the
problem of "statement is not reachable" warnings in templates 
with early

returns, e.g.:
```
bool nobool(T...)() {
 foreach (i, U; T) {
 static if (is(U == bool)) {
 return false;
 }
 }
 return true;  // emits "Warning: statement is not 
reachable"

}



bool nobool(T...)() {
 bool result = true;
 foreach (i, U; T) {
 static if (is(U == bool)) {
 result = false;
 break;
 }
 else
 {
 ...
 }
 }
 return result;  // emits "Warning: statement is not 
reachable"

}

Note that the optimizer will remove the dead loads.


Actually, I would expect every call of this fuction to be 
replaced by a boolean constant, since the result depends only on 
compile-time information.


What happened to "obvious code should be correct"? Can we try to 
find a solution that doesn't look like a hack? I don't think that 
we should ask users of the language to uglify their code just to 
workaround a deficiency in the compiler. Instead, we should 
improve the lowering of the static foreach construct so the 
information that it is syntactically a loop is preserved after 
loop unrolling.


Re: Solution to "statement is not reachable" depending on template variables?

2016-04-01 Thread Johan Engelen via Digitalmars-d-learn

On Friday, 1 April 2016 at 01:21:32 UTC, Walter Bright wrote:

On 3/16/2016 4:18 AM, Johan Engelen wrote:
   I've found discussions, but not an actual "recommended" 
solution for the
problem of "statement is not reachable" warnings in templates 
with early

returns, e.g.:
```
bool nobool(T...)() {
 foreach (i, U; T) {
 static if (is(U == bool)) {
 return false;
 }
 }
 return true;  // emits "Warning: statement is not 
reachable"

}



bool nobool(T...)() {
 bool result = true;
 foreach (i, U; T) {
 static if (is(U == bool)) {
 result = false;
 break;
 }
 else
 {
 ...
 }
 }
 return result;  // emits "Warning: statement is not 
reachable"

}


As you can see from my first post, that is the solution I used 
first  (without the "break;" addition), but it works until...



Note that the optimizer will remove the dead loads.


... until someone adds a (imho useful) diagnostic of dead 
stores/loads. :-)
In that case, I guess inverting all bools and then inverting the 
return value at the end will work (using default initialization 
of result); but this is not possible in a slightly more 
complicated case with non-bool return types.


In another piece of code, the return type is a struct with an 
immutable member. That's when I gave up on working around the 
warning and just disabled it.

Simplified, something like this:

struct IMM {
immutable bool result;
}
IMM immut(T...)() {
IMM retval = IMM(true);
foreach (i, U; T) {
static if (is(U == bool)) {
retval = IMM(false);  //  "Error: cannot modify 
struct retval IMM with immutable member".

}
}
return retval;
}