Re: static array in templated struct/class

2014-08-05 Thread Daniel Gibson via Digitalmars-d-learn

Am 05.08.2014 21:13, schrieb Marc Schütz schue...@gmx.net:

On Tuesday, 5 August 2014 at 18:36:35 UTC, ddos wrote:

alias Vec4f = TVector!(float,4);
alias Vec3f = TVector!(float,3);

class TVector(T,int n)
{
T[n] val;
...

TVector as class does work as expected, as a struct i get the
following errors, but why?

struct Vector.TVector!(float, 4).TVector no size yet for forward
reference
struct Vector.TVector!(float, 3).TVector no size yet for forward
reference


Can you show the complete code? I cannot reproduce this with either
latest DMD git, DMD 2.065, or LDC 0.13.0:

 alias Vec4f = TVector!(float,4);
 alias Vec3f = TVector!(float,3);

 struct TVector(T,int n)
 {
 T[n] val;
 }

 Vec3f a;
 Vec4f b;


struct vs class?


Re: Pointer, if pointed type is unknown?

2014-08-02 Thread Daniel Gibson via Digitalmars-d-learn

Am 02.08.2014 22:21, schrieb seany:

In my previous post
(http://forum.dlang.org/thread/jcaomfduqeofobcul...@forum.dlang.org) I
asked for an array of pointers to different types, as a stack for my
own, where I can generate and insert runtime variables.

However, seems like I need to know the type of variable the pointer is
pointing to. That is,

double pi = 3.14159;
size_t ptr = cast(ulong)pi; // so far oky

double pi_2 = *ptr; // this is where things break down

won't work.

What can I do if the type of the variable is not known?



if you really think you have to do this (and not use variants or 
something): use an appropriate cast,

i.e. double pi_2 = *(cast(double*)ptr);
furthermore: why not use void* instead of size_t?

Cheers,
Daniel


Re: D JSON (WAT?!)

2014-07-24 Thread Daniel Gibson via Digitalmars-d-learn

Am 24.07.2014 17:29, schrieb Pavel:

On Thursday, 24 July 2014 at 15:22:46 UTC, Adam D. Ruppe wrote:

On Thursday, 24 July 2014 at 15:15:37 UTC, Pavel wrote:

 string s = parsed[fail].str;


Since there is no entry fail in the object, it returns a null
JSON_VALUE pointer. Trying to get the string out of it is then seen as
a null pointer access and kills the program.

Check for null on a key before trying to get a value out.


Ok, added:

writeln(parsed[fail] == null);

Now compiler complains:

Error: incompatible types for ((parsed.opIndex(fail)) == (null)):
'JSONValue' and 'typeof(null)'


WAT?!



is instead of == ?


Re: D JSON (WAT?!)

2014-07-24 Thread Daniel Gibson via Digitalmars-d-learn

Am 24.07.2014 17:54, schrieb Pavel:


Guess what, here's a new snippet:

import std.stdio;
import std.json;

void main() {
   scope(failure) writeln(FaILED!!);
   string jsonStr = `{ name: 1, type: r }`;
   auto parsed = parseJSON(jsonStr).object;
   writeln(fail in parsed);
}

Output is:
null

WAT?!

Ofcourse, writing like:

writeln(cast(bool)(fail in parsed));

Produces false... but why on earth boolean expression would output null?

PS: Sorry, for such an emotional boom, I'm so frustrated right now.


Relax :-)

And see http://dlang.org/hash-map.html

in doesn't just return if something is in a map.
If it is, it returns a pointer to the value, otherwise null.
Thus null.

Cheers,
Daniel


Re: How to copy an object to separate allocated memory?

2014-07-24 Thread Daniel Gibson via Digitalmars-d-learn

Am 24.07.2014 19:05, schrieb Gary Willoughby:

I was reading Ali's book (http://ddili.org/ders/d.en/index.html) and saw
this piece of code on how to get the true size of an object:

MyClass* buffer = cast(MyClass*)GC.calloc(__traits(classInstanceSize,
MyClass) * 10);

That got me thinking, how would i actually 'fill' this memory with
instances of that class?


std.conv.emplace()


Re: Map one tuple to another Tuple of different type

2014-07-21 Thread Daniel Gibson via Digitalmars-d-learn

Am 21.07.2014 17:04, schrieb TheFlyingFiddle:

On Monday, 21 July 2014 at 01:42:58 UTC, Daniel Gibson wrote:

Am 21.07.2014 03:34, schrieb Vlad Levenfeld:

To get a foreach to run at compile-time, you have to give it something
whose value is known to the compiler (so, T and typeof(argTuple) would
suffice, and 0..T.length really should as well).


Yup


I use this when i want a compile time foreach(from a constant number).
It's slightly longer but has worked great for me thus far.

template staticIota(size_t s, size_t e, size_t step = 1)
{
 import std.typetuple : TypeTuple;
 static if(s  e)
 alias staticIota = TypeTuple!(s, staticIota!(s + step, e));
 else
 alias staticIota = TypeTuple!();
}


Yeah, I had a similar workaround:

template TupleIndicesImpl(alias len, I...)
{
  static if(len == I.length) // also handles len == 0
alias TupleIndicesImpl = I;
  else static if(I.length == 0)
alias TupleIndicesImpl = TupleIndicesImpl!(len, 0);
  else // I contains 0 ... I.length - 1, so add I.length
alias TupleIndicesImpl = TupleIndicesImpl!(len, I, I.length);
}

template TupleIndices(alias len)
{
  alias TupleIndices = TupleIndicesImpl!(len);
}

foreach(i; TupleIndices!(myTuple.length) { ... }

At least for iterating over a tuple Vlad's way suggestion
(foreach(i, U; TupleType)) is nicer and more concise.

However, having something like staticIota in the stdlib would probably 
make sense.


Cheers,
Daniel


Re: Map one tuple to another Tuple of different type

2014-07-21 Thread Daniel Gibson via Digitalmars-d-learn

Am 21.07.2014 20:09, schrieb H. S. Teoh via Digitalmars-d-learn:

On Mon, Jul 21, 2014 at 06:36:04PM +0200, Daniel Gibson via Digitalmars-d-learn 
wrote:
[...]

However, having something like staticIota in the stdlib would probably
make sense.

[...]

It's already in std.typecons.

(Admittedly, that's not exactly the most obvious place to look for it...)


T



static.typecons is actually where I would have expected it, as it 
constructs a tuple.. but it isn't mentioned on 
http://dlang.org/library/std/typecons.html or 
http://dlang.org/phobos/std_typecons.html
and at least in my /usr/include/dmd/phobos/std/typecons.d (2.065) it's 
private:

private template staticIota(int beg, int end)
{ ... }
And it seems like I can't use it.
Anyway, good to know that it exists and hopefully future versions of D2 
make that function public, so thanks for showing up another alternative 
to solve my problem :-)


BTW: The name Iota is horrible.. it doesn't describe at all what the 
function does.
And But C++11 STL has a function of the same name that does the same 
thing or some obscure programming language from the 60ies (APL) used 
the Greek iota letter to do this is no excuse, one shouldn't expect 
potential D users to know about that (even after using C++ for years I 
never encountered std::iota..)

Maybe Numerate or something like that would be more descriptive..

Cheers,
Daniel