Re: SDL and new Thread open two windows

2017-09-01 Thread SrMordred via Digitalmars-d-learn
On Saturday, 2 September 2017 at 03:47:24 UTC, Adam D. Ruppe 
wrote:

On Saturday, 2 September 2017 at 03:41:47 UTC, SrMordred wrote:

Whats going on , and how to prevent this ?


Are you using a static this anywhere?


Hm, right.
I was constructing the window there.
Forgot that static this will execute on all threads.
Thanks!

Removing the code from "static this" solved.
putting "__gshared" also.
But "shared" crashes the programs...



Re: SDL and new Thread open two windows

2017-09-01 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 2 September 2017 at 03:41:47 UTC, SrMordred wrote:

Whats going on , and how to prevent this ?


Are you using a static this anywhere?


SDL and new Thread open two windows

2017-09-01 Thread SrMordred via Digitalmars-d-learn

Im using SDL2 with derelict, on ubuntu.
Last DMD.

When using spawn or new Thread like :
spawn( (){
  while(true){
Thread.sleep(500.msecs);
  });

the program open two SDL windows.

Whats going on , and how to prevent this ?



Re: 24-bit int

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn

On Saturday, 2 September 2017 at 02:37:08 UTC, Mike Parker wrote:
On Saturday, 2 September 2017 at 01:19:52 UTC, EntangledQuanta 
wrote:


The whole point is so that there is no wasted space, so if it 
requires that then it's not a waste of space but a bug.


Audio that is in24 is 3 bytes per sample, not 4. Every 3 bytes 
are a sample, not every 3 out of 4.


Basically a byte[] cast to a int24 array should be 1/3 the 
size and every 3 bytes are the same as an int24.


Thanks for pointing this out if it is necessary.


It's not a bug, but a feature. Data structure alignment is 
important for efficient reads, so several languages (D, C, C++, 
Ada, and more) will automatically pad structs so that they can 
maintain specific byte alignments. On a 32-bit system, 4-byte 
boundaries are the default. So a struct with 3 ubytes is going 
to be padded with an extra byte at the end. Telling the 
compiler to align on a 1-byte boundary (essentially disabling 
alignment) will save you space, but will will generally cost 
you cycles in accessing the data.


You fail to read correctly. A bug in his code. If he is treating 
in24's as int32's and simply ignoring the upper byte then it is 
not a true int24 and all the work he did would be pointless. I 
can do that by simply reading an int32 and masking the high bit.





Re: 24-bit int

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn
On Saturday, 2 September 2017 at 02:49:41 UTC, Ilya Yaroshenko 
wrote:
On Friday, 1 September 2017 at 19:39:14 UTC, EntangledQuanta 
wrote:
Is there a way to create a 24-bit int? One that for all 
practical purposes acts as such? This is for 24-bit stuff like 
audio. It would respect endianness, allow for arrays int24[] 
that work properly, etc.


Hi,

Probably you are looking for bitpack ndslice topology:
http://docs.algorithm.dlang.io/latest/mir_ndslice_topology.html#.bitpack

sizediff_t[] data;
// creates a packed signed integer slice with max allowed value 
equal to `2^^24 - 1`.

auto packs = data[].sliced.bitpack!24;

packs has the same API as D arrays

Package is Mir Algorithm
http://code.dlang.org/packages/mir-algorithm

Best,
Ilya


Thanks. Seems useful.



Re: 24-bit int

2017-09-01 Thread Ilya Yaroshenko via Digitalmars-d-learn
On Friday, 1 September 2017 at 19:39:14 UTC, EntangledQuanta 
wrote:
Is there a way to create a 24-bit int? One that for all 
practical purposes acts as such? This is for 24-bit stuff like 
audio. It would respect endianness, allow for arrays int24[] 
that work properly, etc.


Hi,

Probably you are looking for bitpack ndslice topology:
http://docs.algorithm.dlang.io/latest/mir_ndslice_topology.html#.bitpack

sizediff_t[] data;
// creates a packed signed integer slice with max allowed value 
equal to `2^^24 - 1`.

auto packs = data[].sliced.bitpack!24;

packs has the same API as D arrays

Package is Mir Algorithm
http://code.dlang.org/packages/mir-algorithm

Best,
Ilya


Re: 24-bit int

2017-09-01 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 2 September 2017 at 01:19:52 UTC, EntangledQuanta 
wrote:


The whole point is so that there is no wasted space, so if it 
requires that then it's not a waste of space but a bug.


Audio that is in24 is 3 bytes per sample, not 4. Every 3 bytes 
are a sample, not every 3 out of 4.


Basically a byte[] cast to a int24 array should be 1/3 the size 
and every 3 bytes are the same as an int24.


Thanks for pointing this out if it is necessary.


It's not a bug, but a feature. Data structure alignment is 
important for efficient reads, so several languages (D, C, C++, 
Ada, and more) will automatically pad structs so that they can 
maintain specific byte alignments. On a 32-bit system, 4-byte 
boundaries are the default. So a struct with 3 ubytes is going to 
be padded with an extra byte at the end. Telling the compiler to 
align on a 1-byte boundary (essentially disabling alignment) will 
save you space, but will will generally cost you cycles in 
accessing the data.




Re: Template substitution for function parameters

2017-09-01 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 1 September 2017 at 11:33:15 UTC, Biotronic wrote:
On Friday, 1 September 2017 at 10:15:09 UTC, Nicholas Wilson 
wrote:

So I have the following types

struct DevicePointer(T) { T* ptr; }

struct Buffer(T)
{
void* driverObject;
T[] hostMemory;
}

and a function

auto enqueue(alias k)(HostArgsOf!k) { ... }

where k would be a function like

void foo( DevicePointer!float a, float b , int c) { ... }

How can I write HostArgsOf such that HostArgsOf!foo yields:
AliasSeq!(Buffer!float, float, int)
preferably in such a way that I can add additional 
transformations to it later on?


i.e. it substitutes the template DevicePointer for the 
template Buffer in Parameters!foo,
The templates can be assumed to not be nested templates, i.e. 
DevicePointer!(DevicePointer!(float)) will never occur neither 
will Buffer!(Buffer!(float) or any cross templates)


template k(alias fn) {
import std.meta, std.traits;
alias k = staticMap!(ReplaceTemplate!(DevicePointer, 
Buffer), Parameters!fn);

}

template ReplaceTemplate(alias needle, alias replacement) {
template ReplaceTemplate(alias T) {
static if (is(T : needle!Args, Args...)) {
alias ReplaceTemplate = replacement!Args;
} else {
alias ReplaceTemplate = T;
}
}
}


Hmm, it seems I oversimplified the example a bit and this doesn't 
quite work for my actual usecase.


struct DevicePointer(int n,T) { T* ptr; }

alias GlobalPointer(T) = DevicePointer!(1,T);

k!foo yields
DevicePointer!(cast(AddrSpace)1u, float), float, int
instead of
Buffer!float, float, int

I think because the is(T : needle!Args, Args...) fails.


Re: replace switch for mapping

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn

I came up with a library solution that isn't pretty ;/

I offer it up to the gods, but being gods, they probably don't 
care.



template EnumMapper(alias func, string[] args, eT...)
{
	import std.meta, std.typecons, std.traits, std.string, 
std.algorithm, std.array, std.conv;


	private auto recSwitch(string[] args, int depth, alias N, 
T...)(string[] attrs = null)

{   
string str;
auto tab = replicate("\t", depth);
static if (T.length == 0)
{
string at;
foreach(k, a; args)
{
at ~= "cast(Parameters!("~func~"!("~attrs.join(", 
")~"))["~to!string(k)~"])"~a;

if (k < args.length-1) at ~= ", ";
}
			return tab~"\treturn "~func~"!("~attrs.join(", 
")~")("~at~");\n";

}
else
{

			str ~= tab~"switch("~N[0]~")\n"~tab~"{\n"~tab~"\tdefault: 
break;\n";

foreach(v; __traits(allMembers, T[0]))
{   
mixin(`enum attr = __traits(getAttributes, 
T[0].`~v~`).stringof[6..$-1].strip();`);

static if (attr != "")
{
str ~= tab~"\t"~"case "~v~":\n";
  
attrs ~= attr[1..$-1];
	str ~= recSwitch!(args, depth + 2 , N[1..$], 
T[1..$])(attrs);			

attrs = attrs[0..$-1];
str ~= tab~"\t\tbreak;\n";

}
}
str ~= tab~"}\n";

return str;
}
}

private auto genMapper(string[] args, alias N, T...)()
{
string str;
foreach(e; AliasSeq!(eT[0..eT.length/2]))
str ~= "with("~e.stringof~") ";
auto code = recSwitch!(args, 0, N, T)();
return str~"\n"~code;
}

auto EnumMapper()
{
return "import std.traits;\n"~genMapper!(args, 
[eT[eT.length/2..$]], eT[0..eT.length/2])();

}
}


Because D only half-assley implements __traits for templates, a 
lot of it is hacks and kludges.


It is used like


struct enumA
{
int value;
alias value this;
@("float") enum Float = cast(enumA)0;
@("int") enum Int = cast(enumA)1;
}

struct enumB
{
int value;
alias value this;
@("double") enum Double = cast(enumB)0;
@("byte") enum Byte = cast(enumB)1;
}

auto foo(T1, T2)(T1 a, T2 b)
{
import std.conv;
return to!string(a)~" - "~to!string(b);

}

void main()
{
auto res = ()
{
int a = 4;
double b = 1.23;
enumA enumAVal = enumA.Float;
enumB enumBVal = enumB.Byte;
	mixin(EnumMapper!("foo", ["a", "b"], enumA, enumB, 
"enumAVal", "enumBVal")());

return "";
}();

writeln(res);
getchar();
}



and basically generates the nested switch structure:

-
with(enumA) with(enumB)
switch(enumAVal)
{
default: break;
case Float:
switch(enumBVal)
{
default: break;
case Double:
	return foo!(float, double)(cast(Parameters!(foo!(float, 
double))[0])a, cast(Parameters!(foo!(float, double))[1])b);

break;
case Byte:
	return foo!(float, byte)(cast(Parameters!(foo!(float, 
byte))[0])a, cast(Parameters!(foo!(float, byte))[1])b);

break;
}
break;
case Int:
switch(enumBVal)
{
default: break;
case Double:
	return foo!(int, double)(cast(Parameters!(foo!(int, 
double))[0])a, cast(Parameters!(foo!(int, double))[1])b);

break;
case Byte:
	return foo!(int, byte)(cast(Parameters!(foo!(int, 
byte))[0])a, cast(Parameters!(foo!(int, byte))[1])b);

break;
}
break;
}
-


and so it maps the arbitrary (a,b) to the correct foo. The idea 
is simple: Given a templated function, we want map the arbitrary 
values, assuming they can be properly cast to the templated 
function depending on the enum values.


the enum values control which foo is called. But this works at 
runtime!


This is useful when one has many different representations of 
data that all can be overloaded, but one doesn't kno

Re: 24-bit int

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn
On Saturday, 2 September 2017 at 00:43:00 UTC, Nicholas Wilson 
wrote:

On Friday, 1 September 2017 at 22:10:43 UTC, Biotronic wrote:
On Friday, 1 September 2017 at 19:39:14 UTC, EntangledQuanta 
wrote:
Is there a way to create a 24-bit int? One that for all 
practical purposes acts as such? This is for 24-bit stuff 
like audio. It would respect endianness, allow for arrays 
int24[] that work properly, etc.


I haven't looked at endianness beyond it working on my 
computer. If you have special needs in that regard, consider 
this a starting point:



struct int24 {
ubyte[3] _payload;

this(int x) {
value = x;
}

...
}

--
  Biotronic


You may also want to put an align(1) on it so that you dont 
waste 25% of the allocated memory in an array of int24's


The whole point is so that there is no wasted space, so if it 
requires that then it's not a waste of space but a bug.


Audio that is in24 is 3 bytes per sample, not 4. Every 3 bytes 
are a sample, not every 3 out of 4.


Basically a byte[] cast to a int24 array should be 1/3 the size 
and every 3 bytes are the same as an int24.


Thanks for pointing this out if it is necessary.




Re: 24-bit int

2017-09-01 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 1 September 2017 at 22:10:43 UTC, Biotronic wrote:
On Friday, 1 September 2017 at 19:39:14 UTC, EntangledQuanta 
wrote:
Is there a way to create a 24-bit int? One that for all 
practical purposes acts as such? This is for 24-bit stuff like 
audio. It would respect endianness, allow for arrays int24[] 
that work properly, etc.


I haven't looked at endianness beyond it working on my 
computer. If you have special needs in that regard, consider 
this a starting point:



struct int24 {
ubyte[3] _payload;

this(int x) {
value = x;
}

...
}

--
  Biotronic


You may also want to put an align(1) on it so that you dont waste 
25% of the allocated memory in an array of int24's


Re: Bug in D!!!

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn

On Friday, 1 September 2017 at 23:25:04 UTC, Jesse Phillips wrote:
I've love being able to inherit and override generic functions 
in C#. Unfortunately C# doesn't use templates and I hit so many 
other issues where Generics just suck.


I don't think it is appropriate to dismiss the need for the 
compiler to generate a virtual function for every instantiated 
T, after all, the compiler can't know you have a finite known 
set of T unless you tell it.


But lets assume we've told the compiler that it is compiling 
all the source code and it does not need to compile for future 
linking.


First the compiler will need to make sure all virtual functions 
can be generated for the derived classes. In this case the 
compiler must note the template function and validate all 
derived classes include it. That was easy.


Next up each instantiation of the function needs a new v-table 
entry in all derived classes. Current compiler implementation 
will compile each module independently of each other; so this 
feature could be specified to work within the same module or 
new semantics can be written up of how the compiler modifies 
already compiled modules and those which reference the compiled 
modules (the object sizes would be changing due to the v-table 
modifications)


With those three simple changes to the language I think that 
this feature will work for every T.


Specifying that there will be no further linkage is the same as 
making T finite. T must be finite.


C# uses generics/IR/CLR so it can do things at run time that is 
effectively compile time for D.


By simply extending the grammar slightly in an intuitive way, we 
can get the explicit finite case, which is easy:


foo(T in [A,B,C])()

and possibly for your case

foo(T in )() would work

or

foo(T in )()

the `in` keyword makes sense here and is not used nor ambiguous, 
I believe.


Regardless of the implementation, the idea that we should throw 
the baby out with the bathwater is simply wrong. At least there 
are a few who get that. By looking in to it in a serious manner 
an event better solution might be found. Not looking at all 
results in no solutions and no progress.





Re: Bug in D!!!

2017-09-01 Thread Jesse Phillips via Digitalmars-d-learn
I've love being able to inherit and override generic functions in 
C#. Unfortunately C# doesn't use templates and I hit so many 
other issues where Generics just suck.


I don't think it is appropriate to dismiss the need for the 
compiler to generate a virtual function for every instantiated T, 
after all, the compiler can't know you have a finite known set of 
T unless you tell it.


But lets assume we've told the compiler that it is compiling all 
the source code and it does not need to compile for future 
linking.


First the compiler will need to make sure all virtual functions 
can be generated for the derived classes. In this case the 
compiler must note the template function and validate all derived 
classes include it. That was easy.


Next up each instantiation of the function needs a new v-table 
entry in all derived classes. Current compiler implementation 
will compile each module independently of each other; so this 
feature could be specified to work within the same module or new 
semantics can be written up of how the compiler modifies already 
compiled modules and those which reference the compiled modules 
(the object sizes would be changing due to the v-table 
modifications)


With those three simple changes to the language I think that this 
feature will work for every T.


Re: get parameter names

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn

On Friday, 1 September 2017 at 22:21:18 UTC, Biotronic wrote:
On Friday, 1 September 2017 at 20:58:20 UTC, EntangledQuanta 
wrote:


template(A, B...)
{
   auto foo(C...)(C c)
   {
   ... get c's parameter names, should be alpha, beta
   }
}


foo!(., .)(alpha, beta)

I need the actual identifiers passed to foo. I can get the 
types(obviously C) but when I try to get the identifier 
names(__traits(identifier or other methods) I stuff get 
_param_k or errors.


I need both C's types and the parameter identifier names past, 
else I'd just pass as strings.


Like Jonathan M Davis points out, this is impossible for 
regular parameters. For template alias parameters, on the other 
hand, this works:


void bar(alias fn)() {
assert(fn.stringof == "alpha");
}

unittest {
int alpha;
bar!(alpha);
}

--
  Biotronic


The problem I have with this is that when I try to pass variables 
in the template complains that there is no "this"


So, what I have resorted to doing is passing the type and the 
name, which seems redundant:


bar!(int, "alpha")

rather than

bar!(alpha) or bar(alpha)

alpha is a variable in a object in my case.

I've tried basically something like the following

void bar(alias fn)()
{
   typeof(fn) should return int and
   fn.stringof should return "alpha"
}

although my code is more complex since I have multiple template 
parameters(using a variadic).


Re: 24-bit int

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn

On Friday, 1 September 2017 at 22:10:43 UTC, Biotronic wrote:
On Friday, 1 September 2017 at 19:39:14 UTC, EntangledQuanta 
wrote:

[...]


I haven't looked at endianness beyond it working on my 
computer. If you have special needs in that regard, consider 
this a starting point:


[...]


Thanks, I'll check it out and see.


Re: get parameter names

2017-09-01 Thread Biotronic via Digitalmars-d-learn
On Friday, 1 September 2017 at 20:58:20 UTC, EntangledQuanta 
wrote:


template(A, B...)
{
   auto foo(C...)(C c)
   {
   ... get c's parameter names, should be alpha, beta
   }
}


foo!(., .)(alpha, beta)

I need the actual identifiers passed to foo. I can get the 
types(obviously C) but when I try to get the identifier 
names(__traits(identifier or other methods) I stuff get 
_param_k or errors.


I need both C's types and the parameter identifier names past, 
else I'd just pass as strings.


Like Jonathan M Davis points out, this is impossible for regular 
parameters. For template alias parameters, on the other hand, 
this works:


void bar(alias fn)() {
assert(fn.stringof == "alpha");
}

unittest {
int alpha;
bar!(alpha);
}

--
  Biotronic


Re: 24-bit int

2017-09-01 Thread Biotronic via Digitalmars-d-learn
On Friday, 1 September 2017 at 19:39:14 UTC, EntangledQuanta 
wrote:
Is there a way to create a 24-bit int? One that for all 
practical purposes acts as such? This is for 24-bit stuff like 
audio. It would respect endianness, allow for arrays int24[] 
that work properly, etc.


I haven't looked at endianness beyond it working on my computer. 
If you have special needs in that regard, consider this a 
starting point:



struct int24 {
ubyte[3] _payload;

this(int x) {
value = x;
}

@property
int value() {
int val = *cast(int*)&_payload & 0xFF;

if (val & 0x80) {
val |= 0xFF00;
}

return val;
}

@property
int value(int x) {
_payload = (cast(ubyte*)&x)[0..3];
return value;
}

auto opUnary(string op)() {
static if (op == "++") {
value = value + 1;
} else static if (op == "--") {
value = value - 1;
} else static if (op == "+") {
return value;
} else static if (op == "-") {
return -value;
} else static if (op == "~") {
return ~value;
} else {
static assert(false, "Unary operator '"~op~"' is not 
supported by int24.");

}
}

auto opOpAssign(string op)(int x) {
static assert(__traits(compiles, {mixin("value = value 
"~op~" x;");}), "Binary operator '"~op~"' is not supported by 
int24.");


mixin("value = value "~op~" x;");
return this;
}

alias value this;
}

unittest {
int24[3] a;
assert(a.sizeof == 9);

// Max value
a[1] = 8388607;
assert(a[1] == 8388607);
// Test for buffer overflow:
assert(a[0] == 0);
assert(a[2] == 0);

// Overflow
a[1] = 8388608;
assert(a[1] == -8388608);
// Test for buffer overflow:
assert(a[0] == 0);
assert(a[2] == 0);

// negative value
a[1] = -1;
assert(a[1] == -1);
// Test for buffer overflow:
assert(a[0] == 0);
assert(a[2] == 0);

// Unary operators
a[1] = 0;
assert(~a[1] == -1);
a[1]--;
assert(a[1] == -1);
assert(-a[1] == 1);
assert(+a[1] == -1);
a[1]++;
assert(a[1] == 0);

// Binary operators
a[1] = 0;
a[1] = a[1] + 1;
assert(a[1] == 1);
a[1] += 1;
assert(a[1] == 2);
a[1] = a[1] - 1;
assert(a[1] == 1);
a[1] -= 1;
assert(a[1] == 0);

a[1] = 3;
a[1] = a[1] * 2;
assert(a[1] == 6);
a[1] = a[1] / 2;
assert(a[1] == 3);
a[1] *= 2;
assert(a[1] == 6);
a[1] /= 2;
assert(a[1] == 3);
a[1] = a[1] << 1;
assert(a[1] == 6);
a[1] <<= 1;
assert(a[1] == 12);
a[1] = a[1] >> 1;
assert(a[1] == 6);
a[1] >>= 1;
assert(a[1] == 3);

a[1] |= 4;
assert(a[1] == 7);
a[1] &= 5;
assert(a[1] == 5);
a[1] = a[1] | 2;
assert(a[1] == 7);
a[1] = a[1] & 3;
assert(a[1] == 3);
}

--
  Biotronic


Re: -betterC not working

2017-09-01 Thread SrMordred via Digitalmars-d-learn

On Wednesday, 30 August 2017 at 23:12:07 UTC, SrMordred wrote:
On Wednesday, 30 August 2017 at 22:45:27 UTC, Adam D. Ruppe 
wrote:

On Wednesday, 30 August 2017 at 22:18:07 UTC, SrMordred wrote:

DMD64 D Compiler v2.075.1


-betterC as described recently is not yet released.

https://dlang.org/changelog/2.076.0_pre.html

is where it gets the new behavior, and that isn't scheduled 
for formal release until the end of the week.


(it seriously bothers me that Walter has been advertising this 
so much when the little bit that is implemented isn't even 
released yet, and what is implemented is barely usable.)



The betterC switch itself is not new, but it does virtually 
nothing until that latest prerelease compiler.


ooops, little missed detail ;)

At least its near.


Hello again.
Downloaded last version.

//fail at my example, so its working as intended
dmd -betterC source/app.d app.d

//compiles. so problem with dub only
dub run --config=application --arch=x86_64 --build=debug 
--compiler=dmd






Re: Help Required on Getopt

2017-09-01 Thread Jon Degenhardt via Digitalmars-d-learn

On Friday, 1 September 2017 at 19:04:39 UTC, Daniel Kozak wrote:
I have same issue.  How this help you?  Catching exception does 
not help. How do I catch exception and still print help message?


Your are correct, sorry about that. What my response showed is 
how to avoid printing the full stack trace and instead printing a 
more nicely formatted error message. And separately, how to print 
formatted help. But, you are correct in that you can't directly 
print the formatted help text from the catch block as shown.


In particular, the GetoptResult returned by getopt is not 
available. I don't have any examples that try to work around 
this. Presumably one could call getopt again to get the options 
list, then generate the formatted help. It'd be an annoyance, 
though perhaps judicious use of AliasSeq might make the code 
structure reasonable.


--Jon


Re: get parameter names

2017-09-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, September 01, 2017 20:58:20 EntangledQuanta via Digitalmars-d-
learn wrote:
> template(A, B...)
> {
> auto foo(C...)(C c)
> {
> ... get c's parameter names, should be alpha, beta
> }
> }
>
>
> foo!(., .)(alpha, beta)
>
> I need the actual identifiers passed to foo. I can get the
> types(obviously C) but when I try to get the identifier
> names(__traits(identifier or other methods) I stuff get _param_k
> or errors.
>
> I need both C's types and the parameter identifier names past,
> else I'd just pass as strings.

Those would actually be the arguments, not the parameters (c would be the
parameters), but regardless, there's no way to do that. The function being
called knows nothing about the caller. The only case that's even vaguely
like that are the __FILE__ and __LINE__ directives which evaluate at the
call site rather at the declaration site if they're used to default
initialize a parameter (whereas in C++, they evaluate at the declaration
site, which is a lot less useful). The function knows what its parameters
are, but it knows nothing about what the arguments that were used to
initialize the parameters were.

If you want the names of the arguments to be passed to the function, you're
going to have to pass them yourself.

- Jonathan M Davis



Easy bug: reference to local variable

2017-09-01 Thread Ali Çehreli via Digitalmars-d-learn

Nothing new here but I almost fell prey to this bug today. Spot the bug:

import std.stdio;
import std.range;

int[3] bar() {
return [ 1, 2, 3 ];
}

auto foo() {
auto a = bar();
return zip(a[], a[]);
}

void main() {
writeln(foo());
}

In the real code, bar() was a call to std.datetime.benchmark, which also 
returns a static array. (The compiler is helpful in the trivial case of 
foo returning a[] instead of hiding it through zip.)


DIP 1000 is targeting such bugs:

  https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md

Ali


get parameter names

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn


template(A, B...)
{
   auto foo(C...)(C c)
   {
   ... get c's parameter names, should be alpha, beta
   }
}


foo!(., .)(alpha, beta)

I need the actual identifiers passed to foo. I can get the 
types(obviously C) but when I try to get the identifier 
names(__traits(identifier or other methods) I stuff get _param_k 
or errors.


I need both C's types and the parameter identifier names past, 
else I'd just pass as strings.





Re: Simplest multithreading example

2017-09-01 Thread ag0aep6g via Digitalmars-d-learn

On 09/01/2017 07:27 AM, Brian wrote:

double [] hugeCalc(int i){
 // Code that takes a long time
}

so if I do


double[][int] _hugeCalcCache;
foreach(i ; I)
_hugeCalcCache[i] = hugeCalc(i);

of course the required time is I.length * (a long time), so I wanted to 
shorten this by splitting to different threads :


foreach(i ; parallel(I) )
_hugeCalcCache[i] = hugeCalc(i);

but as you can guess, it doesn't work that easily.


Works pretty well for me:


double [] hugeCalc(int i)
{
// Code that takes a long time
import core.thread: Thread;
import std.datetime: seconds;
Thread.sleep(1.seconds);
return [i];
}

void main()
{
static import std.range;
import std.parallelism: parallel;
auto I = std.range.iota(0, 10);
double[][int] _hugeCalcCache;
foreach(i ; parallel(I))
_hugeCalcCache[i] = hugeCalc(i);
}


That runs in about 3 seconds here. The serial version would of course 
take about 10 seconds. So, parallelism achieved!


Though I don't know if it's safe to access an associative array 
concurrently like that. I'd use a normal dynamic array instead and 
initialize it before going parallel:



auto _hugeCalcCache = new double[][](10);



Re: Bug in D!!!

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn
This happens when building, not running. This might be a Visual D 
issue as when I use dmd from the command line, it works fine ;/





24-bit int

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn
Is there a way to create a 24-bit int? One that for all practical 
purposes acts as such? This is for 24-bit stuff like audio. It 
would respect endianness, allow for arrays int24[] that work 
properly, etc.


Re: Bug in D!!!

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn

On Friday, 1 September 2017 at 19:25:53 UTC, Adam D Ruppe wrote:
On Friday, 1 September 2017 at 18:17:22 UTC, EntangledQuanta 
wrote:

I get an access violation, changed the code to


What is the rest of your code? access violation usually means 
you didn't new the class...


No, that is the code! I added nothing. Try it out and you'll see. 
I just upgraded to released dmd too.



alias I(A...) = A;

interface Foo {
static foreach(T; I!(int, float))
void set(T t); // define virt funcs for a list of 
types

}

class Ass : Foo {
static foreach(T; I!(int, float))
void set(T t) {
// simplement
}
}

void main()
{

}

try it.




Re: traits for function having actual source declaration?

2017-09-01 Thread bitwise via Digitalmars-d-learn
On Friday, 1 September 2017 at 18:06:04 UTC, Jonathan M Davis 
wrote:

[...]

You can use std.meta.Filter if you need to filter anything out 
of an AliasSeq like this, and whether you should be filtering 
out those functions is highly dependent on what you're doing - 
e.g. sometimes, you really want to get your hands on __ctor, 
whereas other times, that's not at all what you're looking for. 
But because it gives you everything, you have a choice, whereas 
if they were filtered out, then any code that needed to know 
about them would be screwed.


- Jonathan M Davis


I'm working on a runtime reflection library, so there won't be 
any access to the actual type. Any access to constructors will 
have to be done through an interface like these:


class Struct : Reflection {
size_t size() const;
void construct(void[] mem);
}

class Class : Reflection {
Object createInstance() const;
}

The library is basically done, but needs polishing. Also, I'm 
stuck waiting for things like __traits() being allowed to bypass 
protection, which was recently decided upon, but not yet 
implemented (hopefully this hasn't been overturned again).


  Thanks


Re: Bug in D!!!

2017-09-01 Thread Adam D Ruppe via Digitalmars-d-learn
On Friday, 1 September 2017 at 18:17:22 UTC, EntangledQuanta 
wrote:

I get an access violation, changed the code to


What is the rest of your code? access violation usually means you 
didn't new the class...


Re: Help Required on Getopt

2017-09-01 Thread Daniel Kozak via Digitalmars-d-learn
I have same issue.  How this help you?  Catching exception does not help.
How do I catch exception and still print help message?

Dne 1. 9. 2017 8:10 odpoledne napsal uživatel "Vino.B via
Digitalmars-d-learn" :

On Friday, 1 September 2017 at 17:23:01 UTC, Jon Degenhardt wrote:

> On Friday, 1 September 2017 at 13:13:39 UTC, Vino.B wrote:
>
>> Hi All,
>>
>>   When i run the below program without any arguments "D1.d -r" it is
>> throwing error, but i need it to show the help menu
>>
>> [snip...]
>>
>
> Hi Vino,
>
> To get good error message behavior you need to put the construct in a
> try-catch block. Then you can choose how to respond. An example here:
> https://github.com/eBay/tsv-utils-dlang/blob/master/tsv-appe
> nd/src/tsv-append.d#L138-L194. This code prints outs the error message
> from the exception. In your case: "Missing value for argument -r.". But,
> you could also print out the help text as well. There is an example of that
> as well in the above code block, look for the 'if (r.helpWanted)' test.
>
> --Jon
>

Hi,

  Thank you very much, that helped me to resolve the issue.


Re: Bug in D!!!

2017-09-01 Thread EntangledQuanta via Digitalmars-d-learn

On Friday, 1 September 2017 at 15:24:39 UTC, Adam D. Ruppe wrote:
static foreach is now in the new release! You can now do stuff 
like:


---
alias I(A...) = A;

interface Foo {
static foreach(T; I!(int, float))
void set(T t); // define virt funcs for a list 
of types

}

class Ass : Foo {
static foreach(T; I!(int, float))
void set(T t) {
// simplement
}
}
---


really easily.


I get an access violation, changed the code to

import std.meta;
static foreach(T; AliasSeq!("int", "float"))
mixin("void set("~T~" t);");


and also get an access violation ;/



Re: Help Required on Getopt

2017-09-01 Thread Vino.B via Digitalmars-d-learn

On Friday, 1 September 2017 at 17:23:01 UTC, Jon Degenhardt wrote:

On Friday, 1 September 2017 at 13:13:39 UTC, Vino.B wrote:

Hi All,

  When i run the below program without any arguments "D1.d -r" 
it is throwing error, but i need it to show the help menu


[snip...]


Hi Vino,

To get good error message behavior you need to put the 
construct in a try-catch block. Then you can choose how to 
respond. An example here: 
https://github.com/eBay/tsv-utils-dlang/blob/master/tsv-append/src/tsv-append.d#L138-L194. This code prints outs the error message from the exception. In your case: "Missing value for argument -r.". But, you could also print out the help text as well. There is an example of that as well in the above code block, look for the 'if (r.helpWanted)' test.


--Jon


Hi,

  Thank you very much, that helped me to resolve the issue.


Re: traits for function having actual source declaration?

2017-09-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, September 01, 2017 14:38:38 bitwise via Digitalmars-d-learn 
wrote:
> When I'm using __traits(allMembers), I get a all the invisible
> functions added by the compiler as well "__ctor", "__xdtor",
> "__cpctor", etc..
>
> Is there a way to filter them out?

You can use std.meta.Filter if you need to filter anything out of an
AliasSeq like this, and whether you should be filtering out those functions
is highly dependent on what you're doing - e.g. sometimes, you really want
to get your hands on __ctor, whereas other times, that's not at all what
you're looking for. But because it gives you everything, you have a choice,
whereas if they were filtered out, then any code that needed to know about
them would be screwed.

- Jonathan M Davis



Re: Simplest multithreading example

2017-09-01 Thread Ali Çehreli via Digitalmars-d-learn

On 08/31/2017 10:27 PM, Brian wrote:

> the 'real' problem is trying
> to split a huge calculation to different threads.

I still think you can take advantage of std.parallelism:

  https://dlang.org/phobos/std_parallelism.html

Unfortunately, its features like asyncBuf, map, and amap do not stand 
out in the documentation. Here's my interpretation of them:


  http://ddili.org/ders/d.en/parallelism.html

Ali



Re: traits for function having actual source declaration?

2017-09-01 Thread bitwise via Digitalmars-d-learn

On Friday, 1 September 2017 at 17:26:11 UTC, ketmar wrote:

[...]

they *should* listen. anyone who doesn't just aksing for 
troubles, and i see no reason to guard 'em further.


Yeah...eventually came to the same conclusion ;)

 Thanks


Re: traits for function having actual source declaration?

2017-09-01 Thread ketmar via Digitalmars-d-learn

bitwise wrote:


On Friday, 1 September 2017 at 14:38:38 UTC, bitwise wrote:
When I'm using __traits(allMembers), I get a all the invisible functions 
added by the compiler as well "__ctor", "__xdtor", "__cpctor", etc..


Is there a way to filter them out?


dlang's "Lexical" page says:

"Identifiers starting with __ (two underscores) are reserved."

So I suppose I could just filter out functions with leading underscores, 
but still seems unreliable, as people may not listen.


they *should* listen. anyone who doesn't just aksing for troubles, and i 
see no reason to guard 'em further.


Re: Help Required on Getopt

2017-09-01 Thread Jon Degenhardt via Digitalmars-d-learn

On Friday, 1 September 2017 at 13:13:39 UTC, Vino.B wrote:

Hi All,

  When i run the below program without any arguments "D1.d -r" 
it is throwing error, but i need it to show the help menu


[snip...]


Hi Vino,

To get good error message behavior you need to put the construct 
in a try-catch block. Then you can choose how to respond. An 
example here: 
https://github.com/eBay/tsv-utils-dlang/blob/master/tsv-append/src/tsv-append.d#L138-L194. This code prints outs the error message from the exception. In your case: "Missing value for argument -r.". But, you could also print out the help text as well. There is an example of that as well in the above code block, look for the 'if (r.helpWanted)' test.


--Jon


Re: Bug in D!!!

2017-09-01 Thread Adam D. Ruppe via Digitalmars-d-learn
static foreach is now in the new release! You can now do stuff 
like:


---
alias I(A...) = A;

interface Foo {
static foreach(T; I!(int, float))
void set(T t); // define virt funcs for a list of 
types

}

class Ass : Foo {
static foreach(T; I!(int, float))
void set(T t) {
// simplement
}
}
---


really easily.


Re: vibed services stop response after several days of work

2017-09-01 Thread Mike Wey via Digitalmars-d-learn

On 01-09-17 10:01, Suliman wrote:

I got same problem on Windows Server 2016 and on Linux Debian 8.5.
I have few very simple backend based on vibed 0.8.1, compiler dmd 2.075.1.

nginx servise is do port forwarding. Nothing more is used.

After several days of working I am begining to get "502 Bad Gateway" 
error. The service is not down, it's continue to working, but on Windows 
task manager I seen very low memory usage (0.4MB) and on Linux I see 
next: https://snag.gy/ban3jX.jpg (i am not familiar with Linux and do 
not sure if process is alive or not because htop do not show it)


The code is *very* simple https://github.com/bubnenkoff/dlang.ru

Is there any ideas how to diagnostic the problem?


For the linux screenshot ps also includes the grep process you are 
pipping the ps output to. So your vibe.d instance isn't running.


Are you running an release build? If so you might get a better idea of 
whats going on when using a plain or debug build.



--
Mike Wey


Re: vibed services stop response after several days of work

2017-09-01 Thread Suliman via Digitalmars-d-learn
It's seems that it's error in libevent on Linux. I tried to add 
to dub.sdl:

versions "libasync"

and it's seems that not it's working (but I need more time to 
test).


Re: traits for function having actual source declaration?

2017-09-01 Thread bitwise via Digitalmars-d-learn

On Friday, 1 September 2017 at 14:38:38 UTC, bitwise wrote:
When I'm using __traits(allMembers), I get a all the invisible 
functions added by the compiler as well "__ctor", "__xdtor", 
"__cpctor", etc..


Is there a way to filter them out?


dlang's "Lexical" page says:

"Identifiers starting with __ (two underscores) are reserved."

So I suppose I could just filter out functions with leading 
underscores, but still seems unreliable, as people may not listen.


traits for function having actual source declaration?

2017-09-01 Thread bitwise via Digitalmars-d-learn
When I'm using __traits(allMembers), I get a all the invisible 
functions added by the compiler as well "__ctor", "__xdtor", 
"__cpctor", etc..


Is there a way to filter them out?


Help Required on Getopt

2017-09-01 Thread Vino.B via Digitalmars-d-learn

Hi All,

  When i run the below program without any arguments "D1.d -r" it 
is throwing error, but i need it to show the help menu


Program:
import std.stdio;
import std.getopt;
string r;
void main (string[] args)
{
 getopt(args, std.getopt.config.caseInsensitive, 
std.getopt.config.stopOnFirstNonOption, "r" , &r);

 switch (r) {
 case "Test1" : Test1; break;
 case "Test2" : Test2; break;
 default : writeln("Unknown operation"); Help;
}
}
void Test1 ()
{ writeln("This is Test 1");  }
void Test2 ()
{ writeln("This is Test 2"); }
void Help ()
{ writeln("This is Help Menu"); }

Execution:
Works fine if i execute the program with below options
rdmd D1.d
rdmd D1.d -r Test1
rdmd D1.d -r Test2
rdmd D1.d -r Help

but fails with below error when i execute the program as rdmd 
D1.d -r


Requirement :
If i execute the program as "rdmd D1.d -r" is should show me the 
"Help" Menu rather than the below error/


Output:
C:\Admin\Desktop\Current\Script\D>rdmd D1.d -r

object.Exception@C:\D\dmd2\windows\bin\..\..\src\phobos\std\getopt.d(880): 
Missing value for argument -r.

0x00402493
0x004043AC
0x00402999
0x004025FE
0x004025C5
0x0040239B
0x00402236
0x0040B47F
0x0040B443
0x0040B344
0x00405E07
0x76CB336A in BaseThreadInitThunk
0x77CF9902 in RtlInitializeExceptionChain
0x77CF98D5 in RtlInitializeExceptionChain

From,
Vino.B


Re: Valid File Path

2017-09-01 Thread Vino.B via Digitalmars-d-learn
On Thursday, 31 August 2017 at 23:45:01 UTC, Jonathan M Davis 
wrote:
On Thursday, August 31, 2017 23:23:17 Vino via 
Digitalmars-d-learn wrote:

[...]


And why would that not be valid? isValidPath and 
isValidFilename are quite specific about what they think are 
valid path/file names, and having a # in a file name is 
perfectly legitimate. They do have some extra restrictions for 
Windows, since Windows is a lot pickier about its filenames 
than the rest of the world, but # is not one of the characters 
listed as invalid:


https://dlang.org/phobos/std_path.html#isValidPath 
https://dlang.org/phobos/std_path.html#isValidFilename


It would be invalid to have # as a drive name, but it's 
perfectly legal in a filename or directory name.


- Jonathan M Davis


Hi,

  Thank you very much for the explanation, was able to resolve 
the issue.


Re: gcd with doubles

2017-09-01 Thread Moritz Maxeiner via Digitalmars-d-learn

On Friday, 1 September 2017 at 09:33:08 UTC, Alex wrote:
On Sunday, 27 August 2017 at 23:13:24 UTC, Moritz Maxeiner 
wrote:

On Sunday, 27 August 2017 at 19:47:59 UTC, Alex wrote:

[...]


To expand on the earlier workaround: You can also adapt a 
floating point to string algorithm in order to dynamically 
determine an upper bound on the number of after decimal point 
digits required. Below is an untested adaption of the 
reference C implementation of errol0[1] for that purpose (MIT 
license as that is what the original code is under):


[...]


Hey, cool!
Thanks for the efforts :)


No problem, two corrections to myself, though:
1) It's a lower bound, not an upper bound (you need at least that 
many digits in order to not lose precision)
2) The code is missing `_ > ulong.min` checks along the existing 
`_ < ulong.max` checks


Re: Template substitution for function parameters

2017-09-01 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 1 September 2017 at 11:33:15 UTC, Biotronic wrote:
On Friday, 1 September 2017 at 10:15:09 UTC, Nicholas Wilson 
wrote:

So I have the following types

struct DevicePointer(T) { T* ptr; }

struct Buffer(T)
{
void* driverObject;
T[] hostMemory;
}

and a function

auto enqueue(alias k)(HostArgsOf!k) { ... }

where k would be a function like

void foo( DevicePointer!float a, float b , int c) { ... }

How can I write HostArgsOf such that HostArgsOf!foo yields:
AliasSeq!(Buffer!float, float, int)
preferably in such a way that I can add additional 
transformations to it later on?


i.e. it substitutes the template DevicePointer for the 
template Buffer in Parameters!foo,
The templates can be assumed to not be nested templates, i.e. 
DevicePointer!(DevicePointer!(float)) will never occur neither 
will Buffer!(Buffer!(float) or any cross templates)


template k(alias fn) {
import std.meta, std.traits;
alias k = staticMap!(ReplaceTemplate!(DevicePointer, 
Buffer), Parameters!fn);

}

template ReplaceTemplate(alias needle, alias replacement) {
template ReplaceTemplate(alias T) {
static if (is(T : needle!Args, Args...)) {
alias ReplaceTemplate = replacement!Args;
} else {
alias ReplaceTemplate = T;
}
}
}


Thanks!


Re: Template substitution for function parameters

2017-09-01 Thread Biotronic via Digitalmars-d-learn
On Friday, 1 September 2017 at 10:15:09 UTC, Nicholas Wilson 
wrote:

So I have the following types

struct DevicePointer(T) { T* ptr; }

struct Buffer(T)
{
void* driverObject;
T[] hostMemory;
}

and a function

auto enqueue(alias k)(HostArgsOf!k) { ... }

where k would be a function like

void foo( DevicePointer!float a, float b , int c) { ... }

How can I write HostArgsOf such that HostArgsOf!foo yields:
AliasSeq!(Buffer!float, float, int)
preferably in such a way that I can add additional 
transformations to it later on?


i.e. it substitutes the template DevicePointer for the template 
Buffer in Parameters!foo,
The templates can be assumed to not be nested templates, i.e. 
DevicePointer!(DevicePointer!(float)) will never occur neither 
will Buffer!(Buffer!(float) or any cross templates)


template k(alias fn) {
import std.meta, std.traits;
alias k = staticMap!(ReplaceTemplate!(DevicePointer, Buffer), 
Parameters!fn);

}

template ReplaceTemplate(alias needle, alias replacement) {
template ReplaceTemplate(alias T) {
static if (is(T : needle!Args, Args...)) {
alias ReplaceTemplate = replacement!Args;
} else {
alias ReplaceTemplate = T;
}
}
}


Re: Template substitution for function parameters

2017-09-01 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 1 September 2017 at 10:58:51 UTC, user1234 wrote:
On Friday, 1 September 2017 at 10:15:09 UTC, Nicholas Wilson 
wrote:

So I have the following types
...
i.e. it substitutes the template DevicePointer for the 
template Buffer in Parameters!foo,
The templates can be assumed to not be nested templates, i.e. 
DevicePointer!(DevicePointer!(float)) will never occur neither 
will Buffer!(Buffer!(float) or any cross templates)


Wow, this Question triggers an immediate headache...What about 
this ?


import std.traits: Parameters;
alias HostArgsOf(alias A) = AliasSeq!(Parameters!A[0], 
Parameters!A[1], Parameters!A[2]);


It will probably take the form

alias HostArgsOf(alias k) = staticMap!(something, Parameters!k);

I'm just not sure how to formulate the something.




Re: Bug in D!!!

2017-09-01 Thread Biotronic via Digitalmars-d-learn
On Thursday, 31 August 2017 at 15:48:12 UTC, EntangledQuanta 
wrote:

On Thursday, 31 August 2017 at 10:34:14 UTC, Kagamin wrote:
On Thursday, 31 August 2017 at 00:49:22 UTC, EntangledQuanta 
wrote:

I've already implemented a half ass library solution.


It can be improved alot.


Then, by all means, genius!


Enjoy!

mixin template virtualTemplates(alias parent, alias fn, T...) {
import std.meta;
alias name = Alias!(__traits(identifier, fn)[1..$]);
mixin virtualTemplates!(parent, name, fn, T);
}

mixin template virtualTemplates(alias parent, string name, alias 
fn, T...) {

import std.traits;
static if (is(parent == interface)) {
template templateOverloads(string name : name) {
alias templateOverloads = T;
}
alias Types = T;
} else {
alias Types = templateOverloads!name;
}

mixin(virtualTemplatesImpl(name, Types.length, is(parent == 
class)));

}

string virtualTemplatesImpl(string name, int n, bool implement) {
import std.format;
string result;
foreach (i; 0..n) {
auto body = implement ? format(" { return 
fn!(Types[%s])(args); }", i) : ";";
result ~= format("ReturnType!(fn!(Types[%s])) 
%s(Parameters!(fn!(Types[%s])) args)%s\n", i, name, i, body);

}
return result;
}

interface I {
void _Go(T)(T s);
void _Leave(T)(T s);
mixin virtualTemplates!(I, _Go, int, short, float, double);
mixin virtualTemplates!(I, "Abscond", _Leave, int, short, 
float, double);

}

class C : I {
void _Go(T)(T s) { }
void _Leave(T)(T s) { }
mixin virtualTemplates!(C, _Go);
mixin virtualTemplates!(C, "Abscond", _Leave);
}

unittest {
I c = new C();

c.Go(3.2);
c.Abscond(3.4f);
}

Does not support multiple template parameters, or template value 
parameters. Use at own risk for any and all purposes.


Re: Template substitution for function parameters

2017-09-01 Thread user1234 via Digitalmars-d-learn
On Friday, 1 September 2017 at 10:15:09 UTC, Nicholas Wilson 
wrote:

So I have the following types
...
i.e. it substitutes the template DevicePointer for the template 
Buffer in Parameters!foo,
The templates can be assumed to not be nested templates, i.e. 
DevicePointer!(DevicePointer!(float)) will never occur neither 
will Buffer!(Buffer!(float) or any cross templates)


Wow, this Question triggers an immediate headache...What about 
this ?


import std.traits: Parameters;
alias HostArgsOf(alias A) = AliasSeq!(Parameters!A[0], 
Parameters!A[1], Parameters!A[2]);


Re: vibed services stop response after several days of work

2017-09-01 Thread Suliman via Digitalmars-d-learn

On Friday, 1 September 2017 at 08:01:24 UTC, Suliman wrote:
I got same problem on Windows Server 2016 and on Linux Debian 
8.5.
I have few very simple backend based on vibed 0.8.1, compiler 
dmd 2.075.1.


nginx servise is do port forwarding. Nothing more is used.

After several days of working I am begining to get "502 Bad 
Gateway" error. The service is not down, it's continue to 
working, but on Windows task manager I seen very low memory 
usage (0.4MB) and on Linux I see next: 
https://snag.gy/ban3jX.jpg (i am not familiar with Linux and do 
not sure if process is alive or not because htop do not show it)


The code is *very* simple https://github.com/bubnenkoff/dlang.ru

Is there any ideas how to diagnostic the problem?


I got error https://paste.ofcode.org/exCL5S2vbp6qhYBqj7v6ez

Is it's issue with libevent?

Does even-loop depend on used OS?




Re: Synax for variadic template

2017-09-01 Thread Alex via Digitalmars-d-learn
On Friday, 1 September 2017 at 10:01:16 UTC, Nicholas Wilson 
wrote:


b.arr refers to an `(AliasSeq!(int, double))[]`, so with 
`b.arr[0] ~= 5;` you are trying to append a integer to an array 
of pairs of ints and doubles, which you can't do.


b.arr[0] ~= ElementType!(typeof(b.arr))(5,42.0);

should work (I hope, not tested) but you cannot append only the 
int.


If you are trying to abstract the array for struct of array vs. 
array of struct the take a look at 
https://maikklein.github.io/post/soa-d/


Ok... I think I need some type of soa, yes.
But in the article, there is too much of effort done, to convert 
types to arrays, at least at the stage of my code... At the end, 
if I compare the amount of code in the article and just "[]" 
more, as for the part of my example that works...


Thanks for the cool link, nevertheless... Cool to see, how it 
should be done :)


Template substitution for function parameters

2017-09-01 Thread Nicholas Wilson via Digitalmars-d-learn

So I have the following types

struct DevicePointer(T) { T* ptr; }

struct Buffer(T)
{
void* driverObject;
T[] hostMemory;
}

and a function

auto enqueue(alias k)(HostArgsOf!k) { ... }

where k would be a function like

void foo( DevicePointer!float a, float b , int c) { ... }

How can I write HostArgsOf such that HostArgsOf!foo yields:
AliasSeq!(Buffer!float, float, int)
preferably in such a way that I can add additional 
transformations to it later on?


i.e. it substitutes the template DevicePointer for the template 
Buffer in Parameters!foo,
The templates can be assumed to not be nested templates, i.e. 
DevicePointer!(DevicePointer!(float)) will never occur neither 
will Buffer!(Buffer!(float) or any cross templates)






Re: Synax for variadic template

2017-09-01 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 1 September 2017 at 09:38:59 UTC, Alex wrote:

Hi all!
Say, I have

struct A(T...)
{
T arr;
}

struct B(T...)
{
T[] arr;
}

void main()
{
A!(int[], double[]) a;
a.arr[0] ~= 5;
a.arr[0] ~= 6;
static assert(!__traits(compiles, a.arr[0] ~= 3.5));
a.arr[1] ~= 19.8;

assert(a.arr[0] == [5,6]);
assert(a.arr[1] == [19.8]);
assert(a.arr[1].length == 1);
assert(a.arr[0].length == 2);

B!(int, double) b;
//b.arr[0] ~= 5;
}

While struct A behaves like expected, how to get by with B?
What I want is just to abstract the array property out of 
template arguments...


Thanks in advance :)


b.arr refers to an `(AliasSeq!(int, double))[]`, so with 
`b.arr[0] ~= 5;` you are trying to append a integer to an array 
of pairs of ints and doubles, which you can't do.


b.arr[0] ~= ElementType!(typeof(b.arr))(5,42.0);

should work (I hope, not tested) but you cannot append only the 
int.


If you are trying to abstract the array for struct of array vs. 
array of struct the take a look at 
https://maikklein.github.io/post/soa-d/




Synax for variadic template

2017-09-01 Thread Alex via Digitalmars-d-learn

Hi all!
Say, I have

struct A(T...)
{
T arr;
}

struct B(T...)
{
T[] arr;
}

void main()
{
A!(int[], double[]) a;
a.arr[0] ~= 5;
a.arr[0] ~= 6;
static assert(!__traits(compiles, a.arr[0] ~= 3.5));
a.arr[1] ~= 19.8;

assert(a.arr[0] == [5,6]);
assert(a.arr[1] == [19.8]);
assert(a.arr[1].length == 1);
assert(a.arr[0].length == 2);

B!(int, double) b;
//b.arr[0] ~= 5;
}

While struct A behaves like expected, how to get by with B?
What I want is just to abstract the array property out of 
template arguments...


Thanks in advance :)





Re: gcd with doubles

2017-09-01 Thread Alex via Digitalmars-d-learn

On Sunday, 27 August 2017 at 23:13:24 UTC, Moritz Maxeiner wrote:

On Sunday, 27 August 2017 at 19:47:59 UTC, Alex wrote:

[...]


To expand on the earlier workaround: You can also adapt a 
floating point to string algorithm in order to dynamically 
determine an upper bound on the number of after decimal point 
digits required. Below is an untested adaption of the reference 
C implementation of errol0[1] for that purpose (MIT license as 
that is what the original code is under):


[...]


Hey, cool!
Thanks for the efforts :)



Re: Parse tree node allocator

2017-09-01 Thread Per Nordlöw via Digitalmars-d-learn

On Thursday, 31 August 2017 at 15:55:26 UTC, Stefan Koch wrote:

On Thursday, 31 August 2017 at 15:43:05 UTC, Per Nordlöw wrote:
Which allocator is best suited for allocating tree nodes (all 
of equal size around 40-60 bytes in size) in one shot and then 
delete them all in one go? My use case is parse trees.


Region Allocator.


A region over `Mallocator` or a paged allocator to avoid overhead 
in calling `malloc`?


vibed services stop response after several days of work

2017-09-01 Thread Suliman via Digitalmars-d-learn

I got same problem on Windows Server 2016 and on Linux Debian 8.5.
I have few very simple backend based on vibed 0.8.1, compiler dmd 
2.075.1.


nginx servise is do port forwarding. Nothing more is used.

After several days of working I am begining to get "502 Bad 
Gateway" error. The service is not down, it's continue to 
working, but on Windows task manager I seen very low memory usage 
(0.4MB) and on Linux I see next: https://snag.gy/ban3jX.jpg (i am 
not familiar with Linux and do not sure if process is alive or 
not because htop do not show it)


The code is *very* simple https://github.com/bubnenkoff/dlang.ru

Is there any ideas how to diagnostic the problem?