Re: `clear`ing a dynamic array

2015-10-25 Thread qsdfghjk via Digitalmars-d-learn
On Saturday, 24 October 2015 at 13:18:26 UTC, Shriramana Sharma 
wrote:
Hello. I had first expected that dynamic arrays (slices) would 
provide a `.clear()` method but they don't seem to. Obviously I 
can always effectively clear an array by assigning an empty 
array to it, but this has unwanted consequences that `[]` 
actually seems to allocate a new dynamic array and any other 
identifiers initially pointing to the same array will still 
show the old contents and thus it would no longer test true for 
`is` with this array. See the following code:


import std.stdio;
void main()
{
  int a[] = [1,2,3,4,5];
  int b[] = a;
  writeln(a);
  writeln(b);
  //a.clear();
  a = [];
  writeln(a);
  writeln(b);
}

which outputs:

[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[]
[1, 2, 3, 4, 5]

How to make it so that after clearing `a`, `b` will also point 
to the same empty array? IOW the desired output is:


[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[]
[]

... and any further items added to `a` should also reflect in 
`b`.


If you don't want to mess with pointers (as sugggested in the 
first answer) you can also use std.typecons.RefCounted:


---
import std.stdio;
import std.typecons;

RefCounted!(int[]) b;

void main()
{
  int[] a = [1,2,3,4,5];
  b = a;
  writeln(a);
  writeln(b);
  a = [];
  writeln(a);
  writeln(b);
}


Re: Array of templated classes or structs

2015-10-24 Thread qsdfghjk via Digitalmars-d-learn

On Saturday, 24 October 2015 at 15:57:09 UTC, Dandyvica wrote:

Hi guys,

Apart from deriving from the same class and declaring an array 
of that

root class, is there a way to create an array of templates?

This seems not possible since template are compile-time 
generated, but just to be sure. For example, it seems logical 
to get an array of complex numbers but Complex needs to be 
declared with a type.


Thanks for any hint.


You can declare an array whose the element type matches to one of 
the template parameter:


---
struct Foo(T)
{
Foo!T[] foos;
// typeof(this)[] foos // equivalent
}
---

since it's an array (fixed size whatever is the element type: 
size_t len + size_t pointer) it doesn't matter if the template 
declaration is partial.


Re: Array of templated classes or structs

2015-10-24 Thread qsdfghjk via Digitalmars-d-learn

On Saturday, 24 October 2015 at 17:06:13 UTC, Dandyvica wrote:

On Saturday, 24 October 2015 at 16:58:58 UTC, qsdfghjk wrote:

On Saturday, 24 October 2015 at 15:57:09 UTC, Dandyvica wrote:

Hi guys,

Apart from deriving from the same class and declaring an 
array of that

root class, is there a way to create an array of templates?

This seems not possible since template are compile-time 
generated, but just to be sure. For example, it seems logical 
to get an array of complex numbers but Complex needs to be 
declared with a type.


Thanks for any hint.


You can declare an array whose the element type matches to one 
of the template parameter:


---
struct Foo(T)
{
Foo!T[] foos;
// typeof(this)[] foos // equivalent
}
---

since it's an array (fixed size whatever is the element type: 
size_t len + size_t pointer) it doesn't matter if the template 
declaration is partial.


In that case, all elements have the same type right? I'd like 
different types,

but with the same template.


Then no, it's not possible, although some ugly workaround may 
allow the thing (array of pointer and something used to cast the 
pointer at runtime, like an AliasSeq). But by definition if you 
have serveral type in an aray it's not array...it's an agregate.


Re: No shortcircuit for static if or template constraints?

2015-10-24 Thread qsdfghjk via Digitalmars-d-learn

On Saturday, 24 October 2015 at 23:59:02 UTC, qsdfghjk wrote:

On Saturday, 24 October 2015 at 23:34:19 UTC, stewart wrote:

On Saturday, 24 October 2015 at 23:26:09 UTC, stewart wrote:

[...]



Oh and the workaround I'm using is this:

---
void func(T)(T vals) {
static if(isInputRange!T) {
static if(isIntegral!(ForeachType!T)) {
// Do something with range
}
} else {
// do something with scalar
}
}
---

which is a bit ugly.


Maybe this could work:

---
enum isSupportedRange(T) = isInputRange!T && is(ForeachType!T) 
&& (isIntegral!(ElementType!T));

---

ElementType() should return exactly what you excpeted with 
ForeachType().


Oh no! there's been a copy & paste error. I actually meant:

---
enum isSupportedRange(T) = isInputRange!T  && 
(isIntegral!(ElementType!T));


Re: No shortcircuit for static if or template constraints?

2015-10-24 Thread qsdfghjk via Digitalmars-d-learn

On Saturday, 24 October 2015 at 23:34:19 UTC, stewart wrote:

On Saturday, 24 October 2015 at 23:26:09 UTC, stewart wrote:

[...]



Oh and the workaround I'm using is this:

---
void func(T)(T vals) {
static if(isInputRange!T) {
static if(isIntegral!(ForeachType!T)) {
// Do something with range
}
} else {
// do something with scalar
}
}
---

which is a bit ugly.


Maybe this could work:

---
enum isSupportedRange(T) = isInputRange!T && is(ForeachType!T) && 
(isIntegral!(ElementType!T));

---

ElementType() should return exactly what you excpeted with 
ForeachType().