Re: observation: D getting a bit complex

2015-08-30 Thread Jack Stouffer via Digitalmars-d-learn

On Sunday, 30 August 2015 at 02:42:30 UTC, Spacen Jasset wrote:
immutable(ElementEncodingType!(ElementType!Range))[] 
buildPath(Range)(Range segments) if (isInputRange!Range  
isSomeString!(ElementType!Range));
pure nothrow @safe immutable(C)[] buildPath(C)(const(C)[][] 
paths...) if (isSomeChar!C);


I understand how you feel. When I was learning D, the docs where 
almost impenetrable because of this. But when I got into some of 
the more advanced features of D, I found these explicit function 
signatures invaluable. This essentially tells you that the 
function takes either a range of strings, or it can take 
indefinite number of strings as different arguments.


Also, examples underneath the function signature help new comers 
understand how to call the function without having to parse it.


Re: Error Compiling with -debug swtich

2015-08-30 Thread BBasile via Digitalmars-d-learn

On Thursday, 27 August 2015 at 22:09:07 UTC, Jordan Wilson wrote:

Hello,

Just wondering why compiling the following fails with the 
-debug switch, but appears to compile and execute fine without 
it:


import std.stdio;
import std.algorithm;
import std.container;

int main(string[] args) {
Array!string letters = [b,a,c];
sort(letters[]);
writeln (letters[]); // [a,b,c]
return 0;
}

With the -debug switch, I get:
src\phobos\std\range\package.d(7180): Error: 
'std.range.SortedRange!(RangeT!(Array!string), a  
b).SortedRange.dbgVerifySorted' is not nothrow
src\phobos\std\algorithm\sorting.d(982): Error: template 
instance std.range.assumeSorted!(a  b, 
RangeT!(Array!string)) error instantiating


Without the switch, everything seems to work fine...(I'm using 
DMD 2.068.0)


Thanks,

Jordan


filed a BR: https://issues.dlang.org/show_bug.cgi?id=14981


Re: observation: D getting a bit complex

2015-08-30 Thread BBasile via Digitalmars-d-learn

On Sunday, 30 August 2015 at 10:42:24 UTC, Spacen Jasset wrote:

On Sunday, 30 August 2015 at 07:36:55 UTC, BBasile wrote:

On Sunday, 30 August 2015 at 02:42:30 UTC, Spacen Jasset wrote:
immutable(ElementEncodingType!(ElementType!Range))[] 
buildPath(Range)(Range segments) if (isInputRange!Range  
isSomeString!(ElementType!Range));
pure nothrow @safe immutable(C)[] buildPath(C)(const(C)[][] 
paths...) if (isSomeChar!C);


this is stodgy, particularly in a console with line wrapping 
at 80 chars.



To be fair it was the docs page I was reading not a compiler 
diagnostic, but I did get something a bit shorter from the 
compiler once.


Oh i see. Then i don't agree. Doc is very nice. The problem is 
that you have to know std.traits and std.range to understand the 
constraints. It's not always obvious but i'd say it's about 30 or 
40 functions.





Re: Reading and converting binary file 2 bits at a time

2015-08-30 Thread Gary Willoughby via Digitalmars-d-learn

On Sunday, 30 August 2015 at 00:02:16 UTC, anonymous wrote:
On Saturday, 29 August 2015 at 23:34:47 UTC, Gary Willoughby 
wrote:
But it might not be safe: 
http://forum.dlang.org/thread/ztefzijqhwrouzlag...@forum.dlang.org


That link just takes me to this thread here again.


Here's the correct link.
http://forum.dlang.org/thread/sugxdshytelayxnst...@forum.dlang.org


Re: observation: D getting a bit complex

2015-08-30 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn

On Sunday, 30 August 2015 at 07:36:55 UTC, BBasile wrote:

On Sunday, 30 August 2015 at 02:42:30 UTC, Spacen Jasset wrote:
immutable(ElementEncodingType!(ElementType!Range))[] 
buildPath(Range)(Range segments) if (isInputRange!Range  
isSomeString!(ElementType!Range));
pure nothrow @safe immutable(C)[] buildPath(C)(const(C)[][] 
paths...) if (isSomeChar!C);


this is stodgy, particularly in a console with line wrapping at 
80 chars.


Could have been written as

auto buildPath(Range)(Range segments)
if (isInputRange!Range  isSomeString!(ElementType!Range));

but having an explicit return type is super valuable information. 
But the opportunity to omit it makes implementing a first working 
version so fast that it is pure joy.


And the constraints you need not read - unless you want to 
understand why your call to the function failed. C++ is just 
lacking without them. Having them avoids that you always have to 
handle ridiculous input within your functions and allows to 
concentrate on meaningful code.


Re: observation: D getting a bit complex

2015-08-30 Thread BBasile via Digitalmars-d-learn

On Sunday, 30 August 2015 at 02:42:30 UTC, Spacen Jasset wrote:
immutable(ElementEncodingType!(ElementType!Range))[] 
buildPath(Range)(Range segments) if (isInputRange!Range  
isSomeString!(ElementType!Range));
pure nothrow @safe immutable(C)[] buildPath(C)(const(C)[][] 
paths...) if (isSomeChar!C);


this is stodgy, particularly in a console with line wrapping at 
80 chars.




Re: observation: D getting a bit complex

2015-08-30 Thread cym13 via Digitalmars-d-learn
On Sunday, 30 August 2015 at 09:55:02 UTC, Dominikus Dittes 
Scherkl wrote:
And the constraints you need not read - unless you want to 
understand why your call to the function failed. C++ is just 
lacking without them. Having them avoids that you always have 
to handle ridiculous input within your functions and allows to 
concentrate on meaningful code.


Maybe we should color thoses things, having syntax highlighting 
for template and functions signatures would be great.


Re: stuck on opDiv / opBinary

2015-08-30 Thread Spacen Jasset via Digitalmars-d-learn

On Sunday, 30 August 2015 at 18:12:40 UTC, BBasile wrote:

On Sunday, 30 August 2015 at 17:02:58 UTC, Spacen Jasset wrote:

[...]


try

---
Vector3 opBinary(string op)(Vector3 rhs)
{
static if (op ==/){}
else static assert(0, op ~  not implemented);
}
---

you used the char litteral delims ('') instead of the strings 
ones ().


Ah yes, it's didnt' complain. But anyway that was the wrong 
funciton I implemented.


I found the problem. I failed to implement a opDivAssign


Re: How to get the current Timezone

2015-08-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, August 29, 2015 05:25:33 rumbu via Digitalmars-d-learn wrote:
 On Friday, 28 August 2015 at 23:03:16 UTC, Jonathan M Davis wrote:

 
  I _really_ wish that Microsoft would just use the TZ database
  like everyone else...
 
  - Jonathan M Davis

 Starting with Windows 8.1, it does, but only in Windows Runtime
 (so called modern/store apps).

Well, that's good news but ultimately pretty useless. If it's really going
ot be of any use to applications in general, then the OS itself needs to be
using the TZ database files, and that needs to be accessible via their
normal C API, not just the WinRT stuff. But maybe this is a good sign for
the future.

- Jonathan M Davis



Re: stuck on opDiv / opBinary

2015-08-30 Thread BBasile via Digitalmars-d-learn

On Sunday, 30 August 2015 at 17:02:58 UTC, Spacen Jasset wrote:
I have just added an opDiv to this class, but it doesn't seem 
to pick it up.
math/vector.d(30): Error: 'this /= mag' is not a scalar, it is 
a Vector3


I can't see why that is, becuase my opMul works in the same 
place. Can anyone point out what I have done wrong?


Class Matrix {

void normalise()
{
const float mag = magnitude();
if (mag) {
//this.scalarDivide(mag);
this /= mag; // Not work
this *= 1/mag; // Does work.

}
}

//Vector3 opBinary(string op)(Vector3 rhs) if (op=='/')
Vector3 opDiv(float scalar)
{
Vector3 v = this;
v.scalarDivide(scalar);
return v;
}

}


try

---
Vector3 opBinary(string op)(Vector3 rhs)
{
static if (op ==/){}
else static assert(0, op ~  not implemented);
}
---

you used the char litteral delims ('') instead of the strings 
ones ().


Re: What is the D way to map a binary file to a structure?

2015-08-30 Thread Atila Neves via Digitalmars-d-learn

On Saturday, 29 August 2015 at 12:56:08 UTC, cym13 wrote:

Hi,

Let's say I have a simple binary file whose structure is 
well-known. Here is

an example which stores points:

struct Point {
long x;
long y;
long z;
}

struct BinFile {
uintmagicNumber;  // Some identifier
ulong   pointsNumber;
Point[] points;   // Array of pointsNumber points.
}

What is the best way to read some file and fill a structure 
with it? Would
reading the file into a void[] and then casting it to the 
struct work with

things like internal struct padding?


https://github.com/atilaneves/cerealed

Just pass the bytes obtained from reading the file to 
`Decerealiser`.


Atila


Re: observation: D getting a bit complex

2015-08-30 Thread Spacen Jasset via Digitalmars-d-learn

On Sunday, 30 August 2015 at 07:36:55 UTC, BBasile wrote:

On Sunday, 30 August 2015 at 02:42:30 UTC, Spacen Jasset wrote:
immutable(ElementEncodingType!(ElementType!Range))[] 
buildPath(Range)(Range segments) if (isInputRange!Range  
isSomeString!(ElementType!Range));
pure nothrow @safe immutable(C)[] buildPath(C)(const(C)[][] 
paths...) if (isSomeChar!C);


this is stodgy, particularly in a console with line wrapping at 
80 chars.



To be fair is was the docs page I was reading not a compiler 
diagnostic, but I did get something a bit shorter from the 
compiler once.


It's slighty hard to see that you can give that function a 
string, and that it can return a string like thing -- although on 
that score too I've found I have to use char[] in some places 
instead of string which is to do with immutability, that I've not 
quite worked that out yet either -- It's just a feeling I have 
when looking at this now that it is quite complex.


I only mention this as it was something that I think other people 
may notice too, when starting out, or coming back to D.


Maybe I've just been doing too much python recently.





Re: observation: D getting a bit complex

2015-08-30 Thread anonymous via Digitalmars-d-learn
On Sunday 30 August 2015 04:42, Spacen Jasset wrote:

 immutable(ElementEncodingType!(ElementType!Range))[]
 buildPath(Range)(Range segments) if (isInputRange!Range 
 isSomeString!(ElementType!Range));
 pure nothrow @safe immutable(C)[] buildPath(C)(const(C)[][]
 paths...) if (isSomeChar!C);
 
 http://dlang.org/phobos/std_path.html

It's less horrible in the /library/ docs:


immutable(ElementEncodingType!(ElementType!Range))[] buildPath(Range)(
  Range segments
)
if (isInputRange!Range  isSomeString!(ElementType!Range));

immutable(C)[] buildPath(C)(
  const(C)[][] paths
) pure nothrow @safe
if (isSomeChar!C);


http://dlang.org/library/std/path/build_path.html

The /library/ docs are supposed to replace the current /phobos/ ones, but I 
don't know what's the latest on that.


Re: stuck on opDiv / opBinary

2015-08-30 Thread John Colvin via Digitalmars-d-learn

On Sunday, 30 August 2015 at 17:02:58 UTC, Spacen Jasset wrote:
I have just added an opDiv to this class, but it doesn't seem 
to pick it up.
math/vector.d(30): Error: 'this /= mag' is not a scalar, it is 
a Vector3


I can't see why that is, becuase my opMul works in the same 
place. Can anyone point out what I have done wrong?


Class Matrix {

void normalise()
{
const float mag = magnitude();
if (mag) {
//this.scalarDivide(mag);
this /= mag; // Not work
this *= 1/mag; // Does work.

}
}

//Vector3 opBinary(string op)(Vector3 rhs) if (op=='/')
Vector3 opDiv(float scalar)
{
Vector3 v = this;
v.scalarDivide(scalar);
return v;
}

}


Don't use opMul or opDiv etc, use opBinary

Could you provide a more complete example? It's hard to identify 
what the problem is from this snippet.


Re: observation: D getting a bit complex

2015-08-30 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Aug 30, 2015 at 07:36:53AM +, BBasile via Digitalmars-d-learn wrote:
 On Sunday, 30 August 2015 at 02:42:30 UTC, Spacen Jasset wrote:
 immutable(ElementEncodingType!(ElementType!Range))[]
 buildPath(Range)(Range segments) if (isInputRange!Range 
 isSomeString!(ElementType!Range));
 pure nothrow @safe immutable(C)[] buildPath(C)(const(C)[][] paths...) if
 (isSomeChar!C);
 
 this is stodgy, particularly in a console with line wrapping at 80
 chars.

https://issues.dlang.org/show_bug.cgi?id=13676


T

-- 
People who are more than casually interested in computers should have at least 
some idea of what the underlying hardware is like. Otherwise the programs they 
write will be pretty weird. -- D. Knuth


Re: stuck on opDiv / opBinary

2015-08-30 Thread Timon Gehr via Digitalmars-d-learn

On 08/30/2015 07:02 PM, Spacen Jasset wrote:

I have just added an opDiv to this class, but it doesn't seem to pick it
up.
math/vector.d(30): Error: 'this /= mag' is not a scalar, it is a Vector3

I can't see why that is, becuase my opMul works in the same place. Can
anyone point out what I have done wrong?
...


import std.math: sqrt;
import std.algorithm: map,sum,canFind;

struct Vector3{
float[3] xyz;
void normalise(){ this/=magnitude();  }
float magnitude(){ return sqrt(xyz[].map!(a=a*a).sum); }
enum scalarOps=[*,/];
enum isScalarOp(string op)=scalarOps.canFind(op);
void scalar(string op)(float scalar)if(isScalarOp!op){
foreach(ref a;xyz) mixin(`a `~op~`=scalar;`);
}
Vector3 opBinary(string op)(float scalar)if(isScalarOp!op){
Vector3 v=this;
v.scalar!op(scalar);
return v;
}
auto opOpAssign(string op)(float rhs)if(isScalarOp!op){
return mixin(`this=this `~op~` rhs`);
}
}



Re: observation: D getting a bit complex

2015-08-30 Thread bachmeier via Digitalmars-d-learn

On Sunday, 30 August 2015 at 02:42:30 UTC, Spacen Jasset wrote:
The following reminds me of the good old C++ template errors 
the C++ compiler spits out.


Whilst D has fixed that problem, some things have gotten more 
complex. I just wanted to find a replacement for D1 path join, 
and found this, but it doesn't seem very easy to wade though 
this stuff.



immutable(ElementEncodingType!(ElementType!Range))[] 
buildPath(Range)(Range segments) if (isInputRange!Range  
isSomeString!(ElementType!Range));
pure nothrow @safe immutable(C)[] buildPath(C)(const(C)[][] 
paths...) if (isSomeChar!C);


http://dlang.org/phobos/std_path.html

It would take me a lot of time to appeciate what that all 
means, although I can imagine what it is for.


...just and observation. The complexity is building.


With C++ I rarely have trouble finding a good explanation, it's 
just a really complex language that doesn't get easier when you 
use it. For D, the documentation is a work in progress, 
amplifying the learning curve for the things that make life 
easier for experienced programmers.


The official documentation is going to have to take a better 
approach when dealing with ranges and generic programming because 
the current approach doesn't work at all.


stuck on opDiv / opBinary

2015-08-30 Thread Spacen Jasset via Digitalmars-d-learn
I have just added an opDiv to this class, but it doesn't seem to 
pick it up.
math/vector.d(30): Error: 'this /= mag' is not a scalar, it is a 
Vector3


I can't see why that is, becuase my opMul works in the same 
place. Can anyone point out what I have done wrong?


Class Matrix {

void normalise()
{
const float mag = magnitude();
if (mag) {
//this.scalarDivide(mag);
this /= mag; // Not work
this *= 1/mag; // Does work.

}
}

//Vector3 opBinary(string op)(Vector3 rhs) if (op=='/')
Vector3 opDiv(float scalar)
{
Vector3 v = this;
v.scalarDivide(scalar);
return v;
}

}




Re: stuck on opDiv / opBinary

2015-08-30 Thread Spacen Jasset via Digitalmars-d-learn

On Sunday, 30 August 2015 at 20:09:25 UTC, Timon Gehr wrote:

On 08/30/2015 07:02 PM, Spacen Jasset wrote:

[...]


import std.math: sqrt;
import std.algorithm: map,sum,canFind;

struct Vector3{
float[3] xyz;
void normalise(){ this/=magnitude();  }
float magnitude(){ return sqrt(xyz[].map!(a=a*a).sum); }
enum scalarOps=[*,/];
enum isScalarOp(string op)=scalarOps.canFind(op);
void scalar(string op)(float scalar)if(isScalarOp!op){
foreach(ref a;xyz) mixin(`a `~op~`=scalar;`);
}
Vector3 opBinary(string op)(float scalar)if(isScalarOp!op){
Vector3 v=this;
v.scalar!op(scalar);
return v;
}
auto opOpAssign(string op)(float rhs)if(isScalarOp!op){
return mixin(`this=this `~op~` rhs`);
}
}


Thanks, that is interesting. I am curently porting, rather than 
trying to rewrite anything at the moment, but I will try this out 
later.


how does vibe's PrivateAccessProxy work

2015-08-30 Thread yawniek via Digitalmars-d-learn

can someone explain a bit how the @before hooks works in detail,

i mainly have problems understanding why ensureAuth in belows 
example refers to

SampleService.  as an instance:

https://github.com/rejectedsoftware/vibe.d/blob/a1efc05c09135ca8aca21ccec72790ddfaca67c9/examples/web/source/app.d#L114

so how does PrivateAccessProxy work:
https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/internal/meta/funcattr.d#L225


Re: What is the D way to map a binary file to a structure?

2015-08-30 Thread mzfhhhh via Digitalmars-d-learn

On Saturday, 29 August 2015 at 12:56:08 UTC, cym13 wrote:

Hi,

Let's say I have a simple binary file whose structure is 
well-known. Here is

an example which stores points:

struct Point {
long x;
long y;
long z;
}

struct BinFile {
uintmagicNumber;  // Some identifier
ulong   pointsNumber;
Point[] points;   // Array of pointsNumber points.
}

What is the best way to read some file and fill a structure 
with it? Would
reading the file into a void[] and then casting it to the 
struct work with

things like internal struct padding?


struct Point {
 long x;
 long y;
 long z;
}

struct BinFile {
uintmagicNumber;  // Some identifier
ulong   pointsNumber;
Point[] points;   // Array of pointsNumber points.

this(ubyte [] buf)
{
auto f = cast(BinFile *)buf.ptr;
this = cast(BinFile)*f;
this.points = 
(cast(Point*)f.points)[0..cast(uint)this.pointsNumber];

}
}




Can't chain reduce(seed, range)

2015-08-30 Thread Yuxuan Shui via Digitalmars-d-learn
Why is reduce defined as 'auto reduce(S, R)(S seed, R r)', 
instead of reduce(R r, S seed)? I can't chain it.


Maybe provide both?


Re: Array initialization with Struct templates

2015-08-30 Thread Ali Çehreli via Digitalmars-d-learn

On 08/30/2015 10:38 PM, Jonathan M Davis via Digitalmars-d-learn wrote:

On Monday, August 31, 2015 04:57:05 WhatMeWorry via Digitalmars-d-learn wrote:


This seemingly trivial array initialization has caused me hours
of grief.

enum Purpose { POSITIONAL, COLOR_ONLY, COLOR_AND_ALPHA,
GENERIC_TRIPLE, GENERIC_QUAD }
Purpose purpose;

struct Chameleon(T, Purpose p)  // template
{
  static if (is (p == POSITIONAL)) {
  T  x, y, z;
  } else static if (is (p == COLOR_ONLY)) {
  T  r, g, b;
  } else static if (is (p == COLOR_AND_ALPHA)) {
  T  r, g, b, a;
  }  else static if (is (p == GENERIC_TRIPLE)) {
  T  a, b, c;
  } else static if (is (p == GENERIC_QUAD)) {
  T  a, b, c, d;
  }
};

struct VertexData
{
  Chameleon!(float, purpose.POSITIONAL)  position;
  Chameleon!(float, purpose.COLOR_ONLY)  color;
}

alias Vert = VertexData;

VertexData[] vertices =
[
  Vert(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)  // compiler error here
];

I keep getting:

Error: cannot implicitly convert expression (1.0) of type
double to Chameleon!(double, cast(Purpose)0)

I even tried  Vert(1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f)

but it has the exact same error.  Any ideas?  Thanks in advance.


VertexData doesn't have a constructor that takes 6 doubles or 6 floats. It
has a compiler-generated constructor that's equivalent to


this(Chameleon!(float, purpose.POSITIONAL) position,
  Chameleon!(float, purpose.COLOR_ONLY) color)
{
 this.position = position;
 this.color = color;
}

So, you're going to need to pass it a Chameleon!(float, purpose.POSITIONAL)
and a Chameleon!(float, purpose.COLOR_ONLY color), not 6 doubles - either
that, or you're going to need to declare a constructor for VertexData which
takes 6 doubles or floats and converts them to what's require to assign to
its member variables.

- Jonathan M Davis



Additionally, the OP uses the is expression which compares the equality 
of types. However, 'Purpose p' is a value template parameter. A simple 
== comparison works:


enum Purpose { POSITIONAL, COLOR_ONLY, COLOR_AND_ALPHA, GENERIC_TRIPLE, 
GENERIC_QUAD }

Purpose purpose;

struct Chameleon(T, Purpose p)  // template
{
static if (p == Purpose.POSITIONAL) {  // <-- NOT is expression
T  x, y, z;
} else static if (p == Purpose.COLOR_ONLY) {
T  r, g, b;
} else static if (p == Purpose.COLOR_AND_ALPHA) {
T  r, g, b, a;
}  else static if (p == Purpose.GENERIC_TRIPLE) {
T  a, b, c;
} else static if (p == Purpose.GENERIC_QUAD) {
T  a, b, c, d;
}
};

struct VertexData
{
Chameleon!(float, purpose.POSITIONAL)  position;
Chameleon!(float, purpose.COLOR_ONLY)  color;
}

alias Vert = VertexData;

VertexData[] vertices =
[
Vert(Chameleon!(float, purpose.POSITIONAL)(1.0f, 1.0f, 1.0f),
 Chameleon!(float, purpose.COLOR_ONLY)(0.0, 0.0, 0.0))
];

void main()
{}





Re: Can't chain reduce(seed, range)

2015-08-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, August 31, 2015 01:31:58 Yuxuan Shui via Digitalmars-d-learn wrote:
 Why is reduce defined as 'auto reduce(S, R)(S seed, R r)',
 instead of reduce(R r, S seed)? I can't chain it.

 Maybe provide both?

The reasons why the seed is first are historical. It predates UFCS being
added to the language, so there was no reason to put the range first when
when it was added to Phobos. I believe that there have been attempts to fix
it, but AFAIK, they ran into too many problems to be able to do it without
breaking existing code.

- Jonathan M Davis



Re: Array initialization with Struct templates

2015-08-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, August 31, 2015 04:57:05 WhatMeWorry via Digitalmars-d-learn wrote:

 This seemingly trivial array initialization has caused me hours
 of grief.

 enum Purpose { POSITIONAL, COLOR_ONLY, COLOR_AND_ALPHA,
 GENERIC_TRIPLE, GENERIC_QUAD }
 Purpose purpose;

 struct Chameleon(T, Purpose p)  // template
 {
  static if (is (p == POSITIONAL)) {
  T  x, y, z;
  } else static if (is (p == COLOR_ONLY)) {
  T  r, g, b;
  } else static if (is (p == COLOR_AND_ALPHA)) {
  T  r, g, b, a;
  }  else static if (is (p == GENERIC_TRIPLE)) {
  T  a, b, c;
  } else static if (is (p == GENERIC_QUAD)) {
  T  a, b, c, d;
  }
 };

 struct VertexData
 {
  Chameleon!(float, purpose.POSITIONAL)  position;
  Chameleon!(float, purpose.COLOR_ONLY)  color;
 }

 alias Vert = VertexData;

 VertexData[] vertices =
 [
  Vert(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)  // compiler error here
 ];

 I keep getting:

 Error: cannot implicitly convert expression (1.0) of type
 double to Chameleon!(double, cast(Purpose)0)

 I even tried  Vert(1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f)

 but it has the exact same error.  Any ideas?  Thanks in advance.

VertexData doesn't have a constructor that takes 6 doubles or 6 floats. It
has a compiler-generated constructor that's equivalent to


this(Chameleon!(float, purpose.POSITIONAL) position,
 Chameleon!(float, purpose.COLOR_ONLY) color)
{
this.position = position;
this.color = color;
}

So, you're going to need to pass it a Chameleon!(float, purpose.POSITIONAL)
and a Chameleon!(float, purpose.COLOR_ONLY color), not 6 doubles - either
that, or you're going to need to declare a constructor for VertexData which
takes 6 doubles or floats and converts them to what's require to assign to
its member variables.

- Jonathan M Davis



Array initialization with Struct templates

2015-08-30 Thread WhatMeWorry via Digitalmars-d-learn


This seemingly trivial array initialization has caused me hours 
of grief.


enum Purpose { POSITIONAL, COLOR_ONLY, COLOR_AND_ALPHA, 
GENERIC_TRIPLE, GENERIC_QUAD }

Purpose purpose;

struct Chameleon(T, Purpose p)  // template
{
static if (is (p == POSITIONAL)) {
T  x, y, z;
} else static if (is (p == COLOR_ONLY)) {
T  r, g, b;
} else static if (is (p == COLOR_AND_ALPHA)) {
T  r, g, b, a;
}  else static if (is (p == GENERIC_TRIPLE)) {
T  a, b, c;
} else static if (is (p == GENERIC_QUAD)) {
T  a, b, c, d;
}
};

struct VertexData
{
Chameleon!(float, purpose.POSITIONAL)  position;
Chameleon!(float, purpose.COLOR_ONLY)  color;
}

alias Vert = VertexData;

VertexData[] vertices =
[
Vert(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)  // compiler error here
];

I keep getting:

Error: cannot implicitly convert expression (1.0) of type 
double to Chameleon!(double, cast(Purpose)0)	


I even tried  Vert(1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f)

but it has the exact same error.  Any ideas?  Thanks in advance.