www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - canFind doesn't work on Array, replacing [] with array doesn't work,

reply Joerg Joergonson <JJoergonson gmail.com> writes:
Have working code that uses []. Trying to replace with Array!

1. Can't use make in field initialization. Complains about malloc 
in static context.

2. can't decrement the length. So Darr.length = Darr.length - 1; 
fails
This means we can't remove the element easily. I see a removeBack 
but since I can't get the code to compile I don't know if this is 
a replacement. If so, why now allow setting the length?

can't set the length to 0. Why not? Why not just allow length to 
be set?

3. can't use canFind from algorithm. Complains it can't find a 
matching case. I tried many variations to get this to work.
Jun 18 2016
parent reply ag0aep6g <anonymous example.com> writes:
On Saturday, 18 June 2016 at 17:02:40 UTC, Joerg Joergonson wrote:
 3. can't use canFind from algorithm. Complains it can't find a 
 matching case. I tried many variations to get this to work.
canFind takes a range. Array isn't a range itself, but you can get one by slicing it with []: ---- import std.container.array; import std.algorithm; void main() { Array!int a = [1, 2, 3]; assert(a[].canFind(2)); assert(!a[].canFind(0)); } ----
Jun 18 2016
parent Joerg Joergonson <JJoergonson gmail.com> writes:
On Saturday, 18 June 2016 at 17:46:26 UTC, ag0aep6g wrote:
 On Saturday, 18 June 2016 at 17:02:40 UTC, Joerg Joergonson 
 wrote:
 3. can't use canFind from algorithm. Complains it can't find a 
 matching case. I tried many variations to get this to work.
canFind takes a range. Array isn't a range itself, but you can get one by slicing it with []: ---- import std.container.array; import std.algorithm; void main() { Array!int a = [1, 2, 3]; assert(a[].canFind(2)); assert(!a[].canFind(0)); } ----
Thanks. I've decided to implement a wrapper around Array so it is a drop in replacement for [].
Jun 18 2016