Re: Template-style polymorphism in table structure

2016-09-05 Thread data pulverizer via Digitalmars-d-learn
On Monday, 5 September 2016 at 06:45:07 UTC, data pulverizer 
wrote:
On Sunday, 4 September 2016 at 14:49:30 UTC, Lodovico Giaretta 
wrote:
Your getCol(i) could become getCol!T(i) and return an instance 
of GenericVector!T directly, after checking that the required 
column has in fact that type:


GenericVector!T getCol!T(size_t i)
{
if(typeid(cols[i]) == typeid(GenericVector!T))
return cast(GenericVector!T)cols[i];
else
// assert(0) or throw exception
}
I just realized that typeid only gives the class and not the 
actual type, so the object will still need to be cast as you 
mentioned above, however your above function will not infer T, 
so the user will have to provide it. I wonder if there is a way 
to dispatch the right type by a dynamic cast or I fear that 
ZombineDev may be correct and the types will have to be 
limited, which I definitely want to avoid!


Just found this on dynamic dispatching 
(https://wiki.dlang.org/Dispatching_an_object_based_on_its_dynamic_type) but even if you took this approach, you'd still have to register all the types you would be using at the start of the script for all your methods. It's either that or explicitly limited type list as ZombineDev suggests.


Re: Template-style polymorphism in table structure

2016-09-05 Thread Lodovico Giaretta via Digitalmars-d-learn
On Monday, 5 September 2016 at 06:45:07 UTC, data pulverizer 
wrote:
On Sunday, 4 September 2016 at 14:49:30 UTC, Lodovico Giaretta 
wrote:
Your getCol(i) could become getCol!T(i) and return an instance 
of GenericVector!T directly, after checking that the required 
column has in fact that type:


GenericVector!T getCol!T(size_t i)
{
if(typeid(cols[i]) == typeid(GenericVector!T))
return cast(GenericVector!T)cols[i];
else
// assert(0) or throw exception
}
I just realized that typeid only gives the class and not the 
actual type, so the object will still need to be cast as you 
mentioned above, however your above function will not infer T, 
so the user will have to provide it. I wonder if there is a way 
to dispatch the right type by a dynamic cast or I fear that 
ZombineDev may be correct and the types will have to be 
limited, which I definitely want to avoid!


ZombineDev is definitely correct, in that one thing is the static 
type, and another thing is the dynamic type. The type of a 
variable, or the return type of a method are based on the static 
type, computed at compile time. The "true" dynamic type is only 
available at runtime.


That's why I was showing you the use of tuples. If your code does 
not have branches that assign different column types, having the 
types statically determined as template parameters is the best 
choice.


If you really need dynamic types, then there's no alternative: 
the user must explicitly cast things to the correct dynamic type 
(my version of getCol is just a nice wrapper to do that). In 
fact, idiomatic D code tries to avoid dynamic types when 
possible, preferring templates.


How it's better to store data from DB?

2016-09-05 Thread Suliman via Digitalmars-d-learn
Usually I am storing daba from DB as array of structures. 
Something like:


struct MyData
{
 int id;
 string name;
 int age;
}

MyData mydata;

Then I am creating array of structures:
MyData [] mydatas;

And fill data in my code. Then append it to `mydatas` to get it 
iterable.


But is it's good? Maybe there there is better way?

I need main -- be able iterate thru the data. Any other variants?


Re: How it's better to store data from DB?

2016-09-05 Thread Lodovico Giaretta via Digitalmars-d-learn

On Monday, 5 September 2016 at 10:00:26 UTC, Suliman wrote:
Usually I am storing daba from DB as array of structures. 
Something like:


struct MyData
{
 int id;
 string name;
 int age;
}

MyData mydata;

Then I am creating array of structures:
MyData [] mydatas;

And fill data in my code. Then append it to `mydatas` to get it 
iterable.


But is it's good? Maybe there there is better way?

I need main -- be able iterate thru the data. Any other 
variants?


It depends on your specific use case. My advice is to try *not* 
to store the data in memory.


I mean, if your first operation on the array is to filter it (but 
that should be done directly in the sql queries) or if you manage 
to do everything while iterating it only once, then you can avoid 
creating the array. Create a range that wraps the query result 
set and iterate it. This way you maintain in memory just one 
element at a time. This is akin to what you usually do in JDBC (I 
used that a lot, while in D I don't have any db experience).


But of course there are cases in which this advice is not 
applicable.


Re: Using OpenGL

2016-09-05 Thread Guillaume Piolat via Digitalmars-d-learn
On Monday, 5 September 2016 at 05:14:56 UTC, Nicholas Wilson 
wrote:

On Saturday, 3 September 2016 at 17:13:49 UTC, Darren wrote:


Now I wonder if I can load shaders from separate files (à la 
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/).


see:

https://p0nce.github.io/d-idioms/#Embed-a-dynamic-library-in-an-executable

for `import`ing external files at compile time.


It's often useful to be able to reload shaders at runtime.


Custom String vs D String performance

2016-09-05 Thread Patric via Digitalmars-d-learn
I´m playing remaking D functionalities with nogc structs, and to 
at least match D performance.
But in this particular case i´m unable to get near D performance. 
 Can someone point me out what i´m doing wrong, or if there is 
some magic behind the curtains on D strings?


https://dpaste.dzfl.pl/1c981fdc71ac


Re: Custom String vs D String performance

2016-09-05 Thread Daniel Kozak via Digitalmars-d-learn

Dne 5.9.2016 v 13:11 Patric via Digitalmars-d-learn napsal(a):

I´m playing remaking D functionalities with nogc structs, and to at 
least match D performance.
But in this particular case i´m unable to get near D performance.  Can 
someone point me out what i´m doing wrong, or if there is some magic 
behind the curtains on D strings?


https://dpaste.dzfl.pl/1c981fdc71ac


One of reason could be GC, because I belive in D string it reuse memory, 
but in your case you always do malloc




Re: Custom String vs D String performance

2016-09-05 Thread Daniel Kozak via Digitalmars-d-learn

Dne 5.9.2016 v 13:17 Daniel Kozak napsal(a):


Dne 5.9.2016 v 13:11 Patric via Digitalmars-d-learn napsal(a):

I´m playing remaking D functionalities with nogc structs, and to at 
least match D performance.
But in this particular case i´m unable to get near D performance.  
Can someone point me out what i´m doing wrong, or if there is some 
magic behind the curtains on D strings?


https://dpaste.dzfl.pl/1c981fdc71ac


One of reason could be GC, because I belive in D string it reuse 
memory, but in your case you always do malloc




BTW on my pc your Custom String is faster than D string



Re: Custom String vs D String performance

2016-09-05 Thread rikki cattermole via Digitalmars-d-learn

On 05/09/2016 11:11 PM, Patric wrote:

I´m playing remaking D functionalities with nogc structs, and to at
least match D performance.
But in this particular case i´m unable to get near D performance.  Can
someone point me out what i´m doing wrong, or if there is some magic
behind the curtains on D strings?

https://dpaste.dzfl.pl/1c981fdc71ac


Ok lots of bad assumptions in there so lets declare what they should be:

1. D supports three string types, string, wstring and dstring with the 
character types of char, wchar and dchar.
   Strings themselves have no special behavior in the compiler as they 
are arrays.
2. Types such as char are fixed in size, no point multiplying when its a 
constant 1.

3. A D string is length then pointer.

Ok, now on to implementation do not use StopWatch for benchmarking. Use 
benchmark[0]. This will execute the benchmark many times which removes 
one off errors.


Don't directly call malloc, it will never be free'd in this case.
``new char(length)`` would be better as it will automatically be handled 
by the GC.
You'll also want to reserve a block of memory to remove allocation from 
the cost as much as possible (after all you're not measuring that are 
you?). Don't forget to disable the GC as well so it doesn't try to 
collect during the tests.


[0] https://dlang.org/phobos/std_datetime.html#.benchmark
[1] http://dlang.org/spec/type.html
[2] http://dlang.org/spec/abi.html
[3] https://github.com/dlang/druntime/blob/master/src/object.d#L41


Re: Custom String vs D String performance

2016-09-05 Thread Guillaume Piolat via Digitalmars-d-learn

On Monday, 5 September 2016 at 11:11:23 UTC, Patric wrote:
I´m playing remaking D functionalities with nogc structs, and 
to at least match D performance.
But in this particular case i´m unable to get near D 
performance.
 Can someone point me out what i´m doing wrong, or if there is 
some magic behind the curtains on D strings?


https://dpaste.dzfl.pl/1c981fdc71ac


string s;
string a = "testing";
string b = "another";
foreach(_ ; 0..max){
s = a ~ b;
}

Potentially this makes no allocation at all. "testing" and 
"another" will be in readonly memory, "a ~ b" will be 
constant-folded, and s will get assigned the same slice 
every-time.


Now replace it with std::string in C++ and count the number of 
malloc ;)


Re: Custom String vs D String performance

2016-09-05 Thread Daniel Kozak via Digitalmars-d-learn



Dne 5.9.2016 v 13:20 rikki cattermole via Digitalmars-d-learn napsal(a):

...
Don't forget to disable the GC as well so it doesn't try to collect 
during the tests.



This is not true, in many cases it will hurt performance a lot


Re: Custom String vs D String performance

2016-09-05 Thread Patric via Digitalmars-d-learn
On Monday, 5 September 2016 at 11:20:08 UTC, rikki cattermole 
wrote:

On 05/09/2016 11:11 PM, Patric wrote:
I´m playing remaking D functionalities with nogc structs, and 
to at

least match D performance.
But in this particular case i´m unable to get near D 
performance.  Can
someone point me out what i´m doing wrong, or if there is some 
magic

behind the curtains on D strings?

https://dpaste.dzfl.pl/1c981fdc71ac


Ok lots of bad assumptions in there so lets declare what they 
should be:


1. D supports three string types, string, wstring and dstring 
with the character types of char, wchar and dchar.
   Strings themselves have no special behavior in the compiler 
as they are arrays.
2. Types such as char are fixed in size, no point multiplying 
when its a constant 1.

3. A D string is length then pointer.

Ok, now on to implementation do not use StopWatch for 
benchmarking. Use benchmark[0]. This will execute the benchmark 
many times which removes one off errors.


Don't directly call malloc, it will never be free'd in this 
case.
``new char(length)`` would be better as it will automatically 
be handled by the GC.
You'll also want to reserve a block of memory to remove 
allocation from the cost as much as possible (after all you're 
not measuring that are you?). Don't forget to disable the GC as 
well so it doesn't try to collect during the tests.


[0] https://dlang.org/phobos/std_datetime.html#.benchmark
[1] http://dlang.org/spec/type.html
[2] http://dlang.org/spec/abi.html
[3] 
https://github.com/dlang/druntime/blob/master/src/object.d#L41


I´m aware of 1, and 2.

I look one million times on datetime and did´nt see the 
benchmark, thanks xD


My intention is to not use gc at all, so no "new" for me.
Yes the optimal case will be reserve the memory beforehand, but 
i´m benchmarking the opBinary.


So now a bit better example :)
https://dpaste.dzfl.pl/b9356f57a8c8


Daniel Kozak:
Ok, now it gets a bit weird.

On DPaste it shows:
14 ms and 343 μs - DString
3 ms and 928 μs - CustomString

And on my PC (with dub release mode) :

7 ms, 885 μs, and 9 hnsecs - DString
18 ms, 740 μs, and 8 hnsecs - CustomString

(!!!)


Re: Custom String vs D String performance

2016-09-05 Thread Daniel Kozak via Digitalmars-d-learn



Dne 5.9.2016 v 13:45 Patric via Digitalmars-d-learn napsal(a):

On Monday, 5 September 2016 at 11:20:08 UTC, rikki cattermole wrote:

On 05/09/2016 11:11 PM, Patric wrote:

I´m playing remaking D functionalities with nogc structs, and to at
least match D performance.
But in this particular case i´m unable to get near D performance.  Can
someone point me out what i´m doing wrong, or if there is some magic
behind the curtains on D strings?

https://dpaste.dzfl.pl/1c981fdc71ac


Ok lots of bad assumptions in there so lets declare what they should be:

1. D supports three string types, string, wstring and dstring with 
the character types of char, wchar and dchar.
   Strings themselves have no special behavior in the compiler as 
they are arrays.
2. Types such as char are fixed in size, no point multiplying when 
its a constant 1.

3. A D string is length then pointer.

Ok, now on to implementation do not use StopWatch for benchmarking. 
Use benchmark[0]. This will execute the benchmark many times which 
removes one off errors.


Don't directly call malloc, it will never be free'd in this case.
``new char(length)`` would be better as it will automatically be 
handled by the GC.
You'll also want to reserve a block of memory to remove allocation 
from the cost as much as possible (after all you're not measuring 
that are you?). Don't forget to disable the GC as well so it doesn't 
try to collect during the tests.


[0] https://dlang.org/phobos/std_datetime.html#.benchmark
[1] http://dlang.org/spec/type.html
[2] http://dlang.org/spec/abi.html
[3] https://github.com/dlang/druntime/blob/master/src/object.d#L41


I´m aware of 1, and 2.

I look one million times on datetime and did´nt see the benchmark, 
thanks xD


My intention is to not use gc at all, so no "new" for me.
Yes the optimal case will be reserve the memory beforehand, but i´m 
benchmarking the opBinary.


So now a bit better example :)
https://dpaste.dzfl.pl/b9356f57a8c8


Daniel Kozak:
Ok, now it gets a bit weird.

On DPaste it shows:
14 ms and 343 μs - DString
3 ms and 928 μs - CustomString

And on my PC (with dub release mode) :

7 ms, 885 μs, and 9 hnsecs - DString
18 ms, 740 μs, and 8 hnsecs - CustomString

(!!!)

Do you have windows or linux? Which version of dmd you have?


Re: Custom String vs D String performance

2016-09-05 Thread rikki cattermole via Digitalmars-d-learn

On 05/09/2016 11:45 PM, Patric wrote:

On Monday, 5 September 2016 at 11:20:08 UTC, rikki cattermole wrote:

On 05/09/2016 11:11 PM, Patric wrote:

I´m playing remaking D functionalities with nogc structs, and to at
least match D performance.
But in this particular case i´m unable to get near D performance.  Can
someone point me out what i´m doing wrong, or if there is some magic
behind the curtains on D strings?

https://dpaste.dzfl.pl/1c981fdc71ac


Ok lots of bad assumptions in there so lets declare what they should be:

1. D supports three string types, string, wstring and dstring with the
character types of char, wchar and dchar.
   Strings themselves have no special behavior in the compiler as they
are arrays.
2. Types such as char are fixed in size, no point multiplying when its
a constant 1.
3. A D string is length then pointer.

Ok, now on to implementation do not use StopWatch for benchmarking.
Use benchmark[0]. This will execute the benchmark many times which
removes one off errors.

Don't directly call malloc, it will never be free'd in this case.
``new char(length)`` would be better as it will automatically be
handled by the GC.
You'll also want to reserve a block of memory to remove allocation
from the cost as much as possible (after all you're not measuring that
are you?). Don't forget to disable the GC as well so it doesn't try to
collect during the tests.

[0] https://dlang.org/phobos/std_datetime.html#.benchmark
[1] http://dlang.org/spec/type.html
[2] http://dlang.org/spec/abi.html
[3] https://github.com/dlang/druntime/blob/master/src/object.d#L41


I´m aware of 1, and 2.

I look one million times on datetime and did´nt see the benchmark,
thanks xD

My intention is to not use gc at all, so no "new" for me.
Yes the optimal case will be reserve the memory beforehand, but i´m
benchmarking the opBinary.

So now a bit better example :)
https://dpaste.dzfl.pl/b9356f57a8c8


Daniel Kozak:
Ok, now it gets a bit weird.

On DPaste it shows:
14 ms and 343 μs - DString
3 ms and 928 μs - CustomString

And on my PC (with dub release mode) :

7 ms, 885 μs, and 9 hnsecs - DString
18 ms, 740 μs, and 8 hnsecs - CustomString

(!!!)


Be a bit careful there, when a struct is moved around its destructor 
will be called. This is why I suggested you to use the GC to allocate 
memory or else ouch.


So you'll find very quickly that you will be having segfaults since you 
will be doing use after free.


Oh and you'll want to turn that into a slice at some point ``char[] str 
= ptr[0 .. length];``.


Re: Custom String vs D String performance

2016-09-05 Thread Patric via Digitalmars-d-learn

On Monday, 5 September 2016 at 11:53:12 UTC, Daniel Kozak wrote:

Do you have windows or linux? Which version of dmd you have?


Now on my job pc:
DMD32 D Compiler v2.071.0-b2

In home i have 64 last version, and with my first version it 
shows the DString faster than my custom.


Re: Custom String vs D String performance

2016-09-05 Thread Patric via Digitalmars-d-learn
On Monday, 5 September 2016 at 11:55:09 UTC, rikki cattermole 
wrote:


Be a bit careful there, when a struct is moved around its 
destructor will be called. This is why I suggested you to use 
the GC to allocate memory or else ouch.


So you'll find very quickly that you will be having segfaults 
since you will be doing use after free.





Iep, the full code have the postblit operator to correct this :)




Re: Custom String vs D String performance

2016-09-05 Thread Patric via Digitalmars-d-learn

On Monday, 5 September 2016 at 12:03:49 UTC, Patric wrote:

On Monday, 5 September 2016 at 11:53:12 UTC, Daniel Kozak wrote:

Do you have windows or linux? Which version of dmd you have?


Now on my job pc:
DMD32 D Compiler v2.071.0-b2

In home i have 64 last version, and with my first version it 
shows the DString faster than my custom.


Well, if my opBinary implementation is right (in the performance 
sense also), so is a matter of Platform not of implementation, 
and i´m ok with that :)


But I find strange this kind of discrepancy.


delegates, lambdas and functions pitfall

2016-09-05 Thread dom via Digitalmars-d-learn
I am about to write my own stupid and simple http client .. and i 
have added a callback function that has the received content as a 
parameter.


class AsyncHttpGet
{
this(string host, ushort port, string path, void 
delegate(string) callback )

{ ... }
}

My first attempt was to write:

auto f = new AsyncHttpGet("www.dprogramming.com", 80, 
"/index.php", (string content) => {

  ...
});

but this is does not work because my AsyncHttpGet takes a normal 
delegate and this => seems to add nothrow @nogc @safe to my 
delegate type.


The correct syntax is only marginally differnt, but took me quite 
a while to figure out:

( the missing arrow )

auto f = new AsyncHttpGet("www.dprogramming.com", 80, 
"/index.php", (string content)

{
 ... // this is of type function
});

i noticed that delegates are "more powerful" than functions. once 
the passed function e.g. needs to capture a value from the 
outside it becomes a delegate type. I have also read that a 
delegate can contain a reference to a class method bound to an 
instance.


int dummy = 0;
auto f = new AsyncHttpGet("www.dprogramming.com", 80, 
"/index.php", (string content)

{
 dummy = 1; // this is of type delegate
});

but what is the difference between a lambda (=>) and a 
functions/delegates?
i think this is a major pitfall for newcomers, and should be 
adressed somehow.


Re: delegates, lambdas and functions pitfall

2016-09-05 Thread Daniel Kozak via Digitalmars-d-learn

Dne 5.9.2016 v 14:15 dom via Digitalmars-d-learn napsal(a):

I am about to write my own stupid and simple http client .. and i have 
added a callback function that has the received content as a parameter.


class AsyncHttpGet
{
this(string host, ushort port, string path, void delegate(string) 
callback )

{ ... }
}

My first attempt was to write:

auto f = new AsyncHttpGet("www.dprogramming.com", 80, "/index.php", 
(string content) => {

  ...
});

but this is does not work because my AsyncHttpGet takes a normal 
delegate and this => seems to add nothrow @nogc @safe to my delegate 
type.


The correct syntax is only marginally differnt, but took me quite a 
while to figure out:

( the missing arrow )

auto f = new AsyncHttpGet("www.dprogramming.com", 80, "/index.php", 
(string content)

{
 ... // this is of type function
});

i noticed that delegates are "more powerful" than functions. once the 
passed function e.g. needs to capture a value from the outside it 
becomes a delegate type. I have also read that a delegate can contain 
a reference to a class method bound to an instance.


int dummy = 0;
auto f = new AsyncHttpGet("www.dprogramming.com", 80, "/index.php", 
(string content)

{
 dummy = 1; // this is of type delegate
});

but what is the difference between a lambda (=>) and a 
functions/delegates?
i think this is a major pitfall for newcomers, and should be adressed 
somehow.


(string content) => { } // this is delegate which returns another 
delegates {} is shorthand here for (){}


when you use => syntax you can just return expression

(string content) => content; is same as (string content) { return content; }




Re: delegates, lambdas and functions pitfall

2016-09-05 Thread Daniel Kozak via Digitalmars-d-learn



Dne 5.9.2016 v 14:15 dom via Digitalmars-d-learn napsal(a):

...
but what is the difference between a lambda (=>) and a 
functions/delegates?
i think this is a major pitfall for newcomers, and should be adressed 
somehow.

Yes, RTFM :)


Re: delegates, lambdas and functions pitfall

2016-09-05 Thread Daniel Kozak via Digitalmars-d-learn

Dne 5.9.2016 v 14:30 Daniel Kozak napsal(a):




Dne 5.9.2016 v 14:15 dom via Digitalmars-d-learn napsal(a):

...
but what is the difference between a lambda (=>) and a 
functions/delegates?
i think this is a major pitfall for newcomers, and should be adressed 
somehow.

Yes, RTFM :)

But to be fair I have made this mistake myself many times :)


Re: delegates, lambdas and functions pitfall

2016-09-05 Thread Lodovico Giaretta via Digitalmars-d-learn

On Monday, 5 September 2016 at 12:15:35 UTC, dom wrote:

[...]


You misunderstood the error message and the lambda syntax (it 
also happened to me the first time).


The grammar says that you can use one of these syntaxes:

1) `(arguments) {block of code}`

2) `(arguments) => expression`, which is equivalent to 
`(arguments) {return expression;}`


3) `{block of code}`, which is equivalent to `(){block of code}`.

So if you write `(arguments) => {block of code}`, this is 
equivalent to (see rule 2) `(arguments) { return {block of code}; 
}`.


And because of rule 3, it becomes `(arguments) { return (){block 
of code} }`. So what you have is a function that returns a 
function. That's why it does not match your signature.


The fact that the compiler also adds nothrow, @nogc, @safe is not 
important in this case, as those attributes can be implicitly 
casted away.


Assign any event kind to a single templatized function ?

2016-09-05 Thread Basile B. via Digitalmars-d-learn
It's almost a "yeah". However this doesn't work with ref 
parameters. Any idea how to make this work, keeping the 
simplicity of the concept ?


°°
module runnable;

import std.stdio;

struct Foo
{
void delegate(int) event1;
void delegate(int,int) event2;
void delegate(int,ref int) event3;
}

struct Handler
{
void handle(A...)(A a){writeln(a);}
void handleref(A...)(/*auto ref*/ A a){writeln(a);}
}

void main(string[] args)
{
import std.traits;
Foo foo;
Handler handler;
foo.event1 = &handler.handle!(Parameters!(foo.event1));
foo.event2 = &handler.handle!(Parameters!(foo.event2));
foo.event3 = &handler.handleref!(Parameters!(foo.event2)); // 
?

}
°°


Re: Assign any event kind to a single templatized function ?

2016-09-05 Thread Basile B. via Digitalmars-d-learn

On Monday, 5 September 2016 at 13:44:53 UTC, Basile B. wrote:

Typo, last line should be:



foo.event3 = &handler.handleref!(Parameters!(foo.event3));



But it still doesnt work.




Re: Assign any event kind to a single templatized function ?

2016-09-05 Thread ag0aep6g via Digitalmars-d-learn

On 09/05/2016 03:44 PM, Basile B. wrote:

°°
module runnable;

import std.stdio;

struct Foo
{
void delegate(int) event1;
void delegate(int,int) event2;
void delegate(int,ref int) event3;
}

struct Handler
{
void handle(A...)(A a){writeln(a);}
void handleref(A...)(/*auto ref*/ A a){writeln(a);}
}

void main(string[] args)
{
import std.traits;
Foo foo;
Handler handler;
foo.event1 = &handler.handle!(Parameters!(foo.event1));
foo.event2 = &handler.handle!(Parameters!(foo.event2));
foo.event3 = &handler.handleref!(Parameters!(foo.event2)); // ?
}
°°


You can pass the delegate type itself by alias. Then Parameters carries 
over the ref. Not sure if that's well-defined or if it just happens to work.



void handlef(F)(Parameters!F a){writeln(a);}
...
foo.event3 = &handler.handlef!(typeof(foo.event3));


Re: Assign any event kind to a single templatized function ?

2016-09-05 Thread ag0aep6g via Digitalmars-d-learn

On 09/05/2016 04:00 PM, ag0aep6g wrote:

You can pass the delegate type itself by alias.

[...]

void handlef(F)(Parameters!F a){writeln(a);}


Don't know why I wrote "by alias". Clearly no alias there.


Re: Assign any event kind to a single templatized function ?

2016-09-05 Thread Basile B. via Digitalmars-d-learn

On Monday, 5 September 2016 at 14:00:04 UTC, ag0aep6g wrote:

On 09/05/2016 03:44 PM, Basile B. wrote:

[...]


You can pass the delegate type itself by alias. Then Parameters 
carries over the ref. Not sure if that's well-defined or if it 
just happens to work.



void handlef(F)(Parameters!F a){writeln(a);}
...
foo.event3 = &handler.handlef!(typeof(foo.event3));


Nice !


Getting the superclass of a type?

2016-09-05 Thread pineapple via Digitalmars-d-learn
I'd like to be able to write something like this, but I haven't 
been able to find anything in the docs


class Base{}
class Sub: Base{}
static assert(is(SuperClassOf!Sub == Base));



Re: Getting the superclass of a type?

2016-09-05 Thread Lodovico Giaretta via Digitalmars-d-learn

On Monday, 5 September 2016 at 15:20:10 UTC, pineapple wrote:
I'd like to be able to write something like this, but I haven't 
been able to find anything in the docs


class Base{}
class Sub: Base{}
static assert(is(SuperClassOf!Sub == Base));


https://dlang.org/phobos/std_traits.html#.BaseClassesTuple


Removing array element in foreach, safe?

2016-09-05 Thread dom via Digitalmars-d-learn

is this code safe? if not how do i do it correctly?

static AsyncHttpGet[] openRequests;
static void updateRequests()
{
foreach(idx, req; openRequests)
{
if(req.state != Fiber.State.TERM)
req.call();
else
openRequests.remove(idx);
}
}

thx :)


Re: Getting the superclass of a type?

2016-09-05 Thread Johannes Loher via Digitalmars-d-learn
Am 05.09.2016 um 17:43 schrieb Lodovico Giaretta:
> On Monday, 5 September 2016 at 15:20:10 UTC, pineapple wrote:
>> I'd like to be able to write something like this, but I haven't been
>> able to find anything in the docs
>>
>> class Base{}
>> class Sub: Base{}
>> static assert(is(SuperClassOf!Sub == Base));
> 
> https://dlang.org/phobos/std_traits.html#.BaseClassesTuple

Another option would be to use the following:

class Base {}
class Sub: Base {}
class Other {}

static assert(is(Sub : Base));
static assert(!is(Other : Base));

However, the ":" syntax also works for automatic conversion (see
http://ddili.org/ders/d.en/is_expr.html and search for "is (T : OtherT)").


Re: delegates, lambdas and functions pitfall

2016-09-05 Thread dom via Digitalmars-d-learn

On Monday, 5 September 2016 at 12:30:37 UTC, Daniel Kozak wrote:



Dne 5.9.2016 v 14:15 dom via Digitalmars-d-learn napsal(a):

...
but what is the difference between a lambda (=>) and a 
functions/delegates?
i think this is a major pitfall for newcomers, and should be 
adressed somehow.

Yes, RTFM :)


yes you are right, but laziness you know :D

those pages aren't especially beginner friendly x)
https://dlang.org/spec/expression.html#Lambda


Re: delegates, lambdas and functions pitfall

2016-09-05 Thread dom via Digitalmars-d-learn
On Monday, 5 September 2016 at 12:32:49 UTC, Lodovico Giaretta 
wrote:

On Monday, 5 September 2016 at 12:15:35 UTC, dom wrote:

[...]


You misunderstood the error message and the lambda syntax (it 
also happened to me the first time).


The grammar says that you can use one of these syntaxes:

1) `(arguments) {block of code}`

2) `(arguments) => expression`, which is equivalent to 
`(arguments) {return expression;}`


3) `{block of code}`, which is equivalent to `(){block of 
code}`.


So if you write `(arguments) => {block of code}`, this is 
equivalent to (see rule 2) `(arguments) { return {block of 
code}; }`.


And because of rule 3, it becomes `(arguments) { return 
(){block of code} }`. So what you have is a function that 
returns a function. That's why it does not match your signature.


The fact that the compiler also adds nothrow, @nogc, @safe is 
not important in this case, as those attributes can be 
implicitly casted away.


thank you for that detailed answer.




Re: Getting the superclass of a type?

2016-09-05 Thread pineapple via Digitalmars-d-learn
On Monday, 5 September 2016 at 15:43:52 UTC, Lodovico Giaretta 
wrote:

On Monday, 5 September 2016 at 15:20:10 UTC, pineapple wrote:
I'd like to be able to write something like this, but I 
haven't been able to find anything in the docs


class Base{}
class Sub: Base{}
static assert(is(SuperClassOf!Sub == Base));


https://dlang.org/phobos/std_traits.html#.BaseClassesTuple


Aha, thank you!


Re: Unicode function name? ∩

2016-09-05 Thread Tofu Ninja via Digitalmars-d-learn

On Monday, 29 August 2016 at 12:53:26 UTC, Jesper Tholstrup wrote:

On Sunday, 28 August 2016 at 05:21:03 UTC, Tofu Ninja wrote:

Are unicode function names not supported in dmd?
bool ∩(A, B)(A a, B b){
return intersect(a, b);
}
Error: character 0x2229 is not a valid token



Personally, I would prefer 'intersect' as a function name over 
'∩' anytime.


Which benifits does the symbols add?


I was just trying it to see if it works. But I think the symbol 
is nice for readability, typing it is another matter.


dub generate visuald

2016-09-05 Thread Tofu Ninja via Digitalmars-d-learn
I am  not sure what changed but I can no longer build using 
visuald after generating from dub. When I try to build from 
visual studio I get the following error


LINK : warning LNK4001: no object files specified; libraries used
LINK : warning LNK4068: /MACHINE not specified; defaulting to X86
LINK : fatal error LNK1561: entry point must be defined

I did update visuald recently so I suppose that's what caused it. 
Updated dub to 1.0 trying to fix this. Anyone else get this, any 
solution. I can build directly from dub with no problem, but 
building from VS gives that error.


Inexplicable invalid memory operation when program terminates

2016-09-05 Thread pineapple via Digitalmars-d-learn
I have a program which I have stripped down to a single offending 
line which, when present in my program, causes an invalid memory 
operation to occur after main has evaluated:


import mach.sdl.window;
void main(){
auto win = new Window(300, 300);
}

The program is a bit larger than that, but I do not get the error 
when I comment out that instantiation and the things dependent on 
it.


I commented out the body of the constructor that is being called, 
and I still get the error.


Am I missing something or is this an obnoxious bug with the GC?




Re: Removing array element in foreach, safe?

2016-09-05 Thread Daniel Kozak via Digitalmars-d-learn

On Monday, 5 September 2016 at 15:53:39 UTC, dom wrote:

is this code safe? if not how do i do it correctly?

static AsyncHttpGet[] openRequests;
static void updateRequests()
{
foreach(idx, req; openRequests)
{
if(req.state != Fiber.State.TERM)
req.call();
else
openRequests.remove(idx);
}
}

thx :)



openRequests = openRequests.filter!(a=>a.state != 
Fiber.State.TERM).array;

openRequests.each!((a){ a.call(); });


Re: Removing array element in foreach, safe?

2016-09-05 Thread Daniel Kozak via Digitalmars-d-learn

On Monday, 5 September 2016 at 17:38:10 UTC, Daniel Kozak wrote:

On Monday, 5 September 2016 at 15:53:39 UTC, dom wrote:

is this code safe? if not how do i do it correctly?

static AsyncHttpGet[] openRequests;
static void updateRequests()
{
foreach(idx, req; openRequests)
{
if(req.state != Fiber.State.TERM)
req.call();
else
openRequests.remove(idx);
}
}

thx :)



openRequests = openRequests.filter!(a=>a.state != 
Fiber.State.TERM).array;

openRequests.each!((a){ a.call(); });


or

openRequests = openRequests.filter!(a=>a.state != 0).map!((a) 
{a.call(); return a;}).array;


Re: Inexplicable invalid memory operation when program terminates

2016-09-05 Thread Lodovico Giaretta via Digitalmars-d-learn

On Monday, 5 September 2016 at 17:33:17 UTC, pineapple wrote:
I have a program which I have stripped down to a single 
offending line which, when present in my program, causes an 
invalid memory operation to occur after main has evaluated:


import mach.sdl.window;
void main(){
auto win = new Window(300, 300);
}

The program is a bit larger than that, but I do not get the 
error when I comment out that instantiation and the things 
dependent on it.


I commented out the body of the constructor that is being 
called, and I still get the error.


Am I missing something or is this an obnoxious bug with the GC?


InvalidMemoryOperationError is raised when you try to allocate 
memory inside a destructor called by the GC. I see from your 
repository that the destructor of Window uses a function called 
log defined by yourself. That function uses writeln internally. 
writeln is not @nogc, so it may be that that call to log in the 
destructor is actually allocating memory.


Re: Inexplicable invalid memory operation when program terminates

2016-09-05 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Sep 05, 2016 at 05:33:17PM +, pineapple via Digitalmars-d-learn 
wrote:
> I have a program which I have stripped down to a single offending line
> which, when present in my program, causes an invalid memory operation
> to occur after main has evaluated:
> 
> import mach.sdl.window;
> void main(){
> auto win = new Window(300, 300);
> }
> 
> The program is a bit larger than that, but I do not get the error when
> I comment out that instantiation and the things dependent on it.
> 
> I commented out the body of the constructor that is being called, and
> I still get the error.
> 
> Am I missing something or is this an obnoxious bug with the GC?
[...]

What does the destructor do?


T

-- 
"The whole problem with the world is that fools and fanatics are always so 
certain of themselves, but wiser people so full of doubts." -- Bertrand 
Russell. "How come he didn't put 'I think' at the end of it?" -- Anonymous


Re: Inexplicable invalid memory operation when program terminates

2016-09-05 Thread pineapple via Digitalmars-d-learn

On Monday, 5 September 2016 at 17:33:17 UTC, pineapple wrote:

Am I missing something or is this an obnoxious bug with the GC?


Oh, I've been trying to figure this out on and off for days and 
of course five minutes after I post I fix the problem. I'm not 
really sure why, but it did fix it.


In the example the window class destructor was being called. I 
found that if I did `delete win;` at the end there it worked 
fine, but otherwise the GC was tripping up? I removed a console 
output statement and rewrote a thing using a foreach loop instead 
of a filter range and it stopped complaining.


I'm realizing now, too, that the reason I was getting the same 
error in commits long before I added that particular thing the GC 
disliked was an entirely different one.


That was not fun to debug.

On Monday, 5 September 2016 at 17:42:18 UTC, Lodovico Giaretta 
wrote:
InvalidMemoryOperationError is raised when you try to allocate 
memory inside a destructor called by the GC. I see from your 
repository that the destructor of Window uses a function called 
log defined by yourself. That function uses writeln internally. 
writeln is not @nogc, so it may be that that call to log in the 
destructor is actually allocating memory.


That was in fact part of the problem


Re: Inexplicable invalid memory operation when program terminates

2016-09-05 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Sep 05, 2016 at 05:48:11PM +, pineapple via Digitalmars-d-learn 
wrote:
[...]
> In the example the window class destructor was being called. I found
> that if I did `delete win;` at the end there it worked fine, but
> otherwise the GC was tripping up? I removed a console output statement
> and rewrote a thing using a foreach loop instead of a filter range and
> it stopped complaining.
[...]

The problem is caused by trying to allocate memory inside the dtor when
the dtor is invoked by the GC. The delete operator is being deprecated,
but IIRC it doesn't involve a GC collection run, so the problem is
hidden. Basically, class dtors should never do anything involving the
GC, and should not depend on any reference member variables still being
valid.


T

-- 
Never wrestle a pig. You both get covered in mud, and the pig likes it.


Re: dub generate visuald

2016-09-05 Thread Guillaume Piolat via Digitalmars-d-learn

On Monday, 5 September 2016 at 17:25:45 UTC, Tofu Ninja wrote:


I can build directly from dub with no problem, but building 
from VS gives that error.


Building with dub uses the dmd settings in sc.ini
Building with VisualD can override those settings.


Re: dependency analysis for makefile construction

2016-09-05 Thread ag0aep6g via Digitalmars-d-learn

On 09/04/2016 12:07 AM, dan wrote:

Are there any FOSS tools for doing dependency analysis of (e.g.) all the
d files in a directory, to let you know when a .o file needs to be
regenerated?

This presumably would depend mostly on the import statements (including
import of any file to be used in string construction, as in 'auto
my_string = import("my_file");').


dmd itself has two related switches:

* -deps prints out imports and such.
* -v prints out the same information in different format, and also a lot 
more other stuff, including which dmd binary is being used and the 
config file.


rdmd uses -v to figure out the dependencies of the root module. Here's 
how it parses the output:

https://github.com/dlang/tools/blob/master/rdmd.d#L643-L691

I'm not aware of a standalone tool that does something like this. If you 
want to write one, you could do like rdmd and use `dmd -deps`/`dmd -v`, 
or you could use a standalone D parser like libdparse.


http://code.dlang.org/packages/libdparse

[...]

(I'm using gdc, and building with gnumake.)


I'm not sure how similar or different gdc is to dmd with regards to the 
-deps/-v switches. A quick test suggests that gdc has a --deps switch 
that behaves like dmd's -deps. It seems to be undocumented. The output 
of gdc's -v does apparently not include the imports, though.


Re: dub generate visuald

2016-09-05 Thread Tofu Ninja via Digitalmars-d-learn
On Monday, 5 September 2016 at 18:22:02 UTC, Guillaume Piolat 
wrote:

On Monday, 5 September 2016 at 17:25:45 UTC, Tofu Ninja wrote:


I can build directly from dub with no problem, but building 
from VS gives that error.


Building with dub uses the dmd settings in sc.ini
Building with VisualD can override those settings.


There is an option to disable that in the visuald settings, but 
that does not seem to change anything.


Re: dependency analysis for makefile construction

2016-09-05 Thread Basile B. via Digitalmars-d-learn

On Monday, 5 September 2016 at 18:22:08 UTC, ag0aep6g wrote:

On 09/04/2016 12:07 AM, dan wrote:

Are there any FOSS tools for doing dependency analysis of [...]

[...]
I'm not aware of a standalone tool that does something like 
this. If you want to write one, you could do like rdmd and use 
`dmd -deps`/`dmd -v`, or you could use a standalone D parser 
like libdparse.


http://code.dlang.org/packages/libdparse


I have one in dastworx, based on dparse:

https://github.com/BBasile/Coedit/blob/master/dastworx/src/imports.d#L64

It would be very easy to make it a standalone tool (dastworx is a 
standalone tool but its main() is specific to Coedit) or to add 
such an anlayzer to Dscanner.


about 200 SLOCs not more.


Re: dub generate visuald

2016-09-05 Thread Tofu Ninja via Digitalmars-d-learn

On Monday, 5 September 2016 at 17:25:45 UTC, Tofu Ninja wrote:
I am  not sure what changed but I can no longer build using 
visuald after generating from dub. When I try to build from 
visual studio I get the following error


LINK : warning LNK4001: no object files specified; libraries 
used
LINK : warning LNK4068: /MACHINE not specified; defaulting to 
X86

LINK : fatal error LNK1561: entry point must be defined

I did update visuald recently so I suppose that's what caused 
it. Updated dub to 1.0 trying to fix this. Anyone else get 
this, any solution. I can build directly from dub with no 
problem, but building from VS gives that error.


I moved my project to a folder that has no spaces in the path, 
which has changed the error. Now I get:

core.exception.RangeError@pipedmd(286): Range violation



Re: dub generate visuald

2016-09-05 Thread Tofu Ninja via Digitalmars-d-learn

On Monday, 5 September 2016 at 19:03:00 UTC, Tofu Ninja wrote:

On Monday, 5 September 2016 at 17:25:45 UTC, Tofu Ninja wrote:
I am  not sure what changed but I can no longer build using 
visuald after generating from dub. When I try to build from 
visual studio I get the following error


LINK : warning LNK4001: no object files specified; libraries 
used
LINK : warning LNK4068: /MACHINE not specified; defaulting to 
X86

LINK : fatal error LNK1561: entry point must be defined

I did update visuald recently so I suppose that's what caused 
it. Updated dub to 1.0 trying to fix this. Anyone else get 
this, any solution. I can build directly from dub with no 
problem, but building from VS gives that error.


I moved my project to a folder that has no spaces in the path, 
which has changed the error. Now I get:

core.exception.RangeError@pipedmd(286): Range violation


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

U it seems that bug has been in visuald for months now... 
and people wonder why D isn't popular... shit ain't useable.


Re: Removing array element in foreach, safe?

2016-09-05 Thread dom via Digitalmars-d-learn

On Monday, 5 September 2016 at 17:38:10 UTC, Daniel Kozak wrote:

On Monday, 5 September 2016 at 15:53:39 UTC, dom wrote:

is this code safe? if not how do i do it correctly?

static AsyncHttpGet[] openRequests;
static void updateRequests()
{
foreach(idx, req; openRequests)
{
if(req.state != Fiber.State.TERM)
req.call();
else
openRequests.remove(idx);
}
}

thx :)



openRequests = openRequests.filter!(a=>a.state != 
Fiber.State.TERM).array;

openRequests.each!((a){ a.call(); });


thx mate!


Re: dependency analysis for makefile construction

2016-09-05 Thread dan via Digitalmars-d-learn

On Monday, 5 September 2016 at 18:49:25 UTC, Basile B. wrote:

On Monday, 5 September 2016 at 18:22:08 UTC, ag0aep6g wrote:

On 09/04/2016 12:07 AM, dan wrote:
Are there any FOSS tools for doing dependency analysis of 
[...]

[...]
I'm not aware of a standalone tool that does something like 
this. If you want to write one, you could do like rdmd and use 
`dmd -deps`/`dmd -v`, or you could use a standalone D parser 
like libdparse.


http://code.dlang.org/packages/libdparse


I have one in dastworx, based on dparse:

https://github.com/BBasile/Coedit/blob/master/dastworx/src/imports.d#L64



Thanks Basile and also ag0aep6g for your replies, which give me 
several things to try.


dan


Re: Unicode function name? ∩

2016-09-05 Thread Illuminati via Digitalmars-d-learn

On Monday, 29 August 2016 at 12:53:26 UTC, Jesper Tholstrup wrote:

On Sunday, 28 August 2016 at 05:21:03 UTC, Tofu Ninja wrote:

Are unicode function names not supported in dmd?
bool ∩(A, B)(A a, B b){
return intersect(a, b);
}
Error: character 0x2229 is not a valid token



Personally, I would prefer 'intersect' as a function name over 
'∩' anytime.


Which benifits does the symbols add?


It's concise and has a very specific meaning. If one is working 
with mathematical objects and set up correctly then it can be 
more easily interpreted because it is `inline` with standard 
mathematical syntax.


It's the same reason mathematicians prefer ∩ over intersect. You 
would prefer it too if you got used to it. The whole point of 
symbols is to simplify, ∩ is more simple than intersect as the 
first requires 1 symbol and the second requires 8 symbols.





VisualD core.exception.RangeError@pipedmd(286): Range violation

2016-09-05 Thread Tofu Ninja via Digitalmars-d-learn
I get "core.exception.RangeError@pipedmd(286): Range violation" 
whenever I try to build from visual D. Is there any workaround 
for this?


It was reported[1] almost 9 months ago, does not seem like it's 
going to be fixed anytime soon. Visual D is completely broken for 
me right now because of it. Only reason I use Visual D is because 
it's the only useable debugger on windows, now I can't even do 
that...


Lost a day of work trying to fix this, starting to get really 
annoyed...


[1] https://issues.dlang.org/show_bug.cgi?id=15606