Re: cannot call impure function

2020-10-04 Thread Michael via Digitalmars-d-learn

On Sunday, 4 October 2020 at 17:43:13 UTC, Michael wrote:

On Sunday, 4 October 2020 at 17:37:52 UTC, Mathias LANG wrote:

On Sunday, 4 October 2020 at 17:05:33 UTC, Michael wrote:

On Sunday, 4 October 2020 at 17:01:44 UTC, Paul Backus wrote:

On Sunday, 4 October 2020 at 16:48:24 UTC, Michael wrote:

Dear all,

Sorry for the potentially stupid question, but I'm a 
complete newbie to D. Why does compiling the following 
trivial code fail?


import std.stdio;

void main()
{
writeln(3.14);
}


Works fine for me using DMD 2.094.0 on Linux. What OS and 
compiler version are you using, and how did you install DMD?


DMD64 D Compiler v2.094.0
on macOS 10.15.6


I cannot reproduce locally (macOS 10.15.6 too). How did you 
install DMD ?


I used the dmg file: dmd.2.094.0.dmg


I reinstalled using the installation script (install.sh) and now 
it works. Still don't know why the dmg-based intstallation did 
not work.

https://dlang.org/install.html

Anyway, thanks for your responses!


Re: cannot call impure function

2020-10-04 Thread Michael via Digitalmars-d-learn

On Sunday, 4 October 2020 at 17:37:52 UTC, Mathias LANG wrote:

On Sunday, 4 October 2020 at 17:05:33 UTC, Michael wrote:

On Sunday, 4 October 2020 at 17:01:44 UTC, Paul Backus wrote:

On Sunday, 4 October 2020 at 16:48:24 UTC, Michael wrote:

Dear all,

Sorry for the potentially stupid question, but I'm a 
complete newbie to D. Why does compiling the following 
trivial code fail?


import std.stdio;

void main()
{
writeln(3.14);
}


Works fine for me using DMD 2.094.0 on Linux. What OS and 
compiler version are you using, and how did you install DMD?


DMD64 D Compiler v2.094.0
on macOS 10.15.6


I cannot reproduce locally (macOS 10.15.6 too). How did you 
install DMD ?


I used the dmg file: dmd.2.094.0.dmg


Re: cannot call impure function

2020-10-04 Thread Michael via Digitalmars-d-learn

On Sunday, 4 October 2020 at 17:05:33 UTC, Michael wrote:

On Sunday, 4 October 2020 at 17:01:44 UTC, Paul Backus wrote:

On Sunday, 4 October 2020 at 16:48:24 UTC, Michael wrote:

Dear all,

Sorry for the potentially stupid question, but I'm a complete 
newbie to D. Why does compiling the following trivial code 
fail?


import std.stdio;

void main()
{
writeln(3.14);
}


Works fine for me using DMD 2.094.0 on Linux. What OS and 
compiler version are you using, and how did you install DMD?


DMD64 D Compiler v2.094.0
on macOS 10.15.6


I did not use any compiler flags, but just:
dmd test1.d



Re: cannot call impure function

2020-10-04 Thread Michael via Digitalmars-d-learn

On Sunday, 4 October 2020 at 17:01:44 UTC, Paul Backus wrote:

On Sunday, 4 October 2020 at 16:48:24 UTC, Michael wrote:

Dear all,

Sorry for the potentially stupid question, but I'm a complete 
newbie to D. Why does compiling the following trivial code 
fail?


import std.stdio;

void main()
{
writeln(3.14);
}


Works fine for me using DMD 2.094.0 on Linux. What OS and 
compiler version are you using, and how did you install DMD?


DMD64 D Compiler v2.094.0
on macOS 10.15.6



cannot call impure function

2020-10-04 Thread Michael via Digitalmars-d-learn

Dear all,

Sorry for the potentially stupid question, but I'm a complete 
newbie to D. Why does compiling the following trivial code fail?


import std.stdio;

void main()
{
writeln(3.14);
}

Error message:

/Library/D/dmd/src/phobos/std/format.d(1601): Error: pure 
function std.format.FormatSpec!char.FormatSpec.toString cannot 
call impure function 
std.format.FormatSpec!char.FormatSpec.toString!(Appender!string).toString
/Library/D/dmd/src/phobos/std/format.d(478): Error: template 
instance std.format.FormatSpec!char error instantiating
/Library/D/dmd/src/phobos/std/stdio.d(1590):instantiated 
from here: formattedWrite!(LockingTextWriter, char, double)
/Library/D/dmd/src/phobos/std/stdio.d(3927):instantiated 
from here: write!(double, char)

test1.d(5):instantiated from here: writeln!double

Cheers,
Michael


Unused imports

2020-01-30 Thread Michael via Digitalmars-d-learn

Is there a way to detect unused imports?

It happened to me that I used imports which I did not need in the 
end. So, I'd like to remove them easily.





Re: readline / Gnu readline

2020-01-30 Thread Michael via Digitalmars-d-learn

On Thursday, 30 January 2020 at 13:23:17 UTC, Adam D. Ruppe wrote:

On Thursday, 30 January 2020 at 06:12:32 UTC, Michael wrote:

I did exactly just what you proposed.


yeah i often just leave my random filenames in there, in this 
case rl was one of them. (if you don't put `.d` at the end of a 
filename, dmd will add it automatically). Generally a "module X 
is in file X which cannot be read" error means you should:


1) double-check any filenames on the command line. Make sure no 
typos etc and the files all exist. With my samples that 
frequently means changing the filename from whatever nonsense I 
left in my lazy copy/paste :P


2) If all the files are in place, this error can also be caused 
by a mismatch between the `module` name in an imported file and 
the `import` statement that is trying to use it. Those should 
always match.


I'm getting a bit more familiar with D. Really nice. Just wrote a 
program consisting of several files. Importing works fine now and 
also GNU readline.

Thanks for your help


auto keyword

2020-01-30 Thread Michael via Digitalmars-d-learn
auto is surely a nice feature. Nonetheless I'd prefer to use 
explicit types. So when reading a code and I see the auto keyword 
I also have to find out what kind of type is meant.


I have a line of code that looks like this:
auto elements = buf.to!string.strip.split(" ").filter!(a => a != 
"");


That line strips white space from buf, splits it, removes empty 
elements and returns an array of strings. At least I thought so.


Indeed elements can be treated as a string slice, but if i 
replace auto by string[] the compiler complains:
Error: cannot implicitly convert expression 
filter(split(strip(to(buf)), " ")) of type 
FilterResult!(__lambda1, string[]) to string[]


In order to use an explicit type I wonder what kind of type I 
might use instead of auto?


Re: readline / Gnu readline

2020-01-30 Thread Michael via Digitalmars-d-learn

On Thursday, 30 January 2020 at 08:13:16 UTC, Mike Parker wrote:
That means any arguments you pass on the command line after the 
source file name will be passed to your program. Compiler 
options need to go before the source file name.


rdmd -L-lreadline mysource.d


That works, thanks Mike


Re: readline / Gnu readline

2020-01-29 Thread Michael via Digitalmars-d-learn

On Thursday, 30 January 2020 at 06:15:54 UTC, Mike Parker wrote:

Is your source file named rl.d? And are you running dmd in the 
source file's directory?


No, I did not. Changed it now and it works with dmd. Great!
Tried the same with rdmd I'm getting a linker error.




Re: readline / Gnu readline

2020-01-29 Thread Michael via Digitalmars-d-learn
On Wednesday, 29 January 2020 at 21:15:08 UTC, Adam D. Ruppe 
wrote:

On Wednesday, 29 January 2020 at 20:01:32 UTC, Michael wrote:

I am new to D.
I would like to use the Gnu readline function in D. Is there a 
module that i can use?


just define it yourself

---

// this line right here is all you need to call the function
extern(C) char* readline(const char*);

import core.stdc.stdio;

void main() {
char* a = readline("prompt> ");
printf("%s\n", a);
}

---

# and also link it in at the command line with -L-lreadline
 dmd rl -L-lreadline






readline is so simple you don't need to do anything fancier. If 
you need history and such too you just define those functions 
as well.


Dear Adam,

I did exactly just what you proposed.

When 'dmd rl -L-lreadline' in the command line. I do get the 
following error:

Error: module rl is in file 'rl.d' which cannot be read.

So probably I'm missing something unfortunately I don't know what.


readline / Gnu readline

2020-01-29 Thread Michael via Digitalmars-d-learn

I am new to D.
I would like to use the Gnu readline function in D. Is there a 
module that i can use?


Re: Deprecation: foreach: loop index implicitly converted from size_t to int

2019-01-18 Thread Michael via Digitalmars-d-learn

On Friday, 18 January 2019 at 13:29:29 UTC, Adam D. Ruppe wrote:

On Friday, 18 January 2019 at 12:27:17 UTC, Michael wrote:

This, to be, looks like quite the explicit conversion, no?


Yeah, I agree. But the language is silly. I just leave the type 
out of foreach and explicitly cast it inside the body.


Thank you all for the concise explanations and suggestions, I 
think that's fairly straightforward. I thought perhaps I was 
doing the sensible thing of dealing with the conversion inside 
the foreach statement, but I guess not!


Deprecation: foreach: loop index implicitly converted from size_t to int

2019-01-18 Thread Michael via Digitalmars-d-learn

Hello all,

I am getting this deprecation warning when compiling using DMD64 
D Compiler v2.084.0 on Linux. I'm a little unsure what the 
problem is, however, because the code producing these warnings 
tends to be of the form:



foreach (int i, ref prop; props)


This, to be, looks like quite the explicit conversion, no? Does 
it mean I now have to use to!int(i) to convert the type of i in a 
foreach now?


Thanks,
Michael.


Re: Enum and CTFE function call

2018-08-14 Thread Michael via Digitalmars-d-learn
On Tuesday, 14 August 2018 at 11:25:06 UTC, Jonathan M Davis 
wrote:
On Tuesday, August 14, 2018 4:03:11 AM MDT Michael via 
Digitalmars-d-learn wrote:

[...]


CTFE is triggered when a value must be known at compile-time. 
So, if you have something like


[...]


That is much clearer now, thanks for clarifying.


Re: Enum and CTFE function call

2018-08-14 Thread Michael via Digitalmars-d-learn

On Tuesday, 14 August 2018 at 09:17:41 UTC, ixid wrote:

On Tuesday, 14 August 2018 at 09:12:30 UTC, ixid wrote:
This will not compile as it says n is not known at compile 
time...


This does work if 'value' is changed to immutable and fun to 
accept it. So it still seems like a missed opportunity as enum 
shouldn't be able to change either.


From the examples on this page:
https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe

It looks to me like it might be a slight issue with the function 
being void, in that you need to explicitly ensure that n is 
immutable. For the example on the page above, they assign the 
result of the function to a static variable, and so the function 
is evaluated at compile-time, but without assigning the value to 
a static variable, it is evaluated at runtime. I guess in this 
case the compiler may just be being cautious?


The page does state that enums should trigger CTFE though.


Re: UFCS confusion

2018-07-13 Thread Michael via Digitalmars-d-learn

On Friday, 13 July 2018 at 11:17:32 UTC, Radu wrote:

On Friday, 13 July 2018 at 11:12:47 UTC, Michael wrote:

On Friday, 13 July 2018 at 10:52:54 UTC, Radu wrote:

On Friday, 13 July 2018 at 10:21:54 UTC, Michael wrote:

[...]


Do you try to call member functions? UFCS only works with 
free functions, meaning declared at module level.


https://dlang.org/spec/function.html#pseudo-member


I'm not intentionally trying to call member functions, no. The 
functions are all static functions of a class, but the 
chaining of them using UFCS doesn't seem to work.


OK, static functions of a class are static *member* functions, 
they are not free functions.


Oh, really? Well that will explain it then. I was sure I had used 
this syntax before but perhaps not. Thanks for your help!


Re: UFCS confusion

2018-07-13 Thread Michael via Digitalmars-d-learn

On Friday, 13 July 2018 at 10:52:54 UTC, Radu wrote:

On Friday, 13 July 2018 at 10:21:54 UTC, Michael wrote:

Hello,

I am nesting some function calls, and I'm pretty used to 
making use of D's Uniform Function Call Syntax, but I'm 
getting an error if I try to convert this line:



createSet(createVector(langSize, index)).length;


which works, into this line:


createVector(langSize, index).createSet.length;


receiving the error "Error: no property createSet for type 
int[]", when the signature for createSet is:



static auto createSet(in int[] vector) pure


What am I missing here? I'm sure it's stupidly obvious but it 
expects one argument, so I'm just calling it without 
parentheses.


Do you try to call member functions? UFCS only works with free 
functions, meaning declared at module level.


https://dlang.org/spec/function.html#pseudo-member


I'm not intentionally trying to call member functions, no. The 
functions are all static functions of a class, but the chaining 
of them using UFCS doesn't seem to work.


UFCS confusion

2018-07-13 Thread Michael via Digitalmars-d-learn

Hello,

I am nesting some function calls, and I'm pretty used to making 
use of D's Uniform Function Call Syntax, but I'm getting an error 
if I try to convert this line:



createSet(createVector(langSize, index)).length;


which works, into this line:


createVector(langSize, index).createSet.length;


receiving the error "Error: no property createSet for type 
int[]", when the signature for createSet is:



static auto createSet(in int[] vector) pure


What am I missing here? I'm sure it's stupidly obvious but it 
expects one argument, so I'm just calling it without parentheses.


Re: Passing a reference to a returned reference

2018-07-06 Thread Michael via Digitalmars-d-learn

On Friday, 6 July 2018 at 16:24:03 UTC, Timoses wrote:

On Friday, 6 July 2018 at 15:51:34 UTC, Michael wrote:

[...]
While writing I realized that the following is even the case 
without the 'ref' parameter:
The caller of the setter will still be able to change the 
content of your private data after you checked it for validity. 
Take following example:


[...]


I understand your warning, but as the array is created purely to 
assign to the agent's beliefs, it only appears in the main() 
function as a rhs reference, which is passed to the agent, the 
reference is stored in the member variable, and then the main 
function continues, holding no reference to that array outside of 
agent's agent.beliefs getter function. Appreciate the heads up, 
though.


Re: Passing a reference to a returned reference

2018-07-06 Thread Michael via Digitalmars-d-learn

On Friday, 6 July 2018 at 15:57:27 UTC, Timoses wrote:

On Friday, 6 July 2018 at 15:33:18 UTC, Michael wrote:


This is definitely to do with my use of the setter syntax, 
which maybe I am misunderstanding? Because if I change it to a 
normal function call like so:


a.beliefs(Operator.create());

then it complains if I use ref, and doesn't complain if I 
don't.


You can try removing the "auto" from the Operator class method

// Error: returning dict escapes a reference to local variable 
dict

false
static ref create()
{
double[int] dict;
dict[2] = 1.0;
dict[1] = 0.0;
return dict;
}

That indicates that 'dict' is actually returned by value (at 
least the reference). This sounds a bit confusing.
AFAIK, in above code a reference 'dict' is created pointing 
towards memory that stores that dictionary entries. The 
reference itself is a "pointer"(?) which will "die" when 
running out of the method's scope. This pointer lives on the 
function's stack. So returning this "pointer" (essentially a 
value type, e.g. int) will return it by value.


You could return a reference like so:

class Operator
{
static double[int] hdict;
static this()
{
 hdict[2] = 1.0;
 hdict[1] = 0.0;
}
static ref create()
{
return hdict;
}
}

then


a.beliefs(Operator.create());


should work on the "ref" setter.


Oh of course, as it's a reference type, the "value" being 
returned by 'auto ref' is the value of the reference to the array 
anyway, so I am already passing the reference. Not sure how I 
managed to get really confused about that. Then, forcing the 
function to return a reference to a local reference to a heap 
alloc. array is obviously a problem. This makes way more sense 
now. Thank you again, Timoses, and I hope you enjoyed the result, 
Ali.


I guess I just need to be careful with the setter/getter 
approach, in case I accidentally invoke the getter to return a 
reference, which I then overwrite with another reference. I don't 
know if that's worthy of adding some kind of compiler warning, or 
if I'm just the stupid one to get caught by that, but I'm glad 
it's resolved.


At least it was, technically, working before by leaking access to 
the private member variable and overwriting it.


Re: Passing a reference to a returned reference

2018-07-06 Thread Michael via Digitalmars-d-learn

On Friday, 6 July 2018 at 15:37:25 UTC, Timoses wrote:

On Friday, 6 July 2018 at 15:14:01 UTC, Michael wrote:

class Agent
{
private
{
double[int] mDict;
}

// Setter: copy
void beliefs(ref double[int] dict)
{
import std.stdio : writeln;
writeln("Setter function.");
this.mDict = dict;
}

// Getter
auto ref beliefs()
{
return this.mDict;
}
}

class Operator
{
static auto ref create()
{
double[int] dict;
dict[2] = 1.0;
dict[1] = 0.0;
return dict;
}
}


Throw in a writeln statement in getter and be amazed : D

class Agent
{
private
{
double[int] mDict;
}

// Setter: copy
void beliefs(ref double[int] dict)
{
import std.stdio : writeln;
writeln("Setter function.");
this.mDict = dict;
}

// Getter
auto ref beliefs()
{
writeln("getter");
return this.mDict;
}
}

class Operator
{
static auto ref create()
{
double[int] dict;
dict[2] = 1.0;
dict[1] = 0.0;
return dict;
}
}

unittest
{
import std.stdio : writeln;

Agent a = new Agent();

writeln("Statement 1");
a.beliefs = Operator.create();
writeln("Statement 2");
assert(a.beliefs.keys.length == 2);

writeln("Statement 3");
writeln(a.beliefs);
}

If you remove the ref then "Statement 1" will use the setter 
method. Otherwise it calls the "auto ref" function to get a 
reference of 'mDict' and assign it a value. Essentially


// Getter
auto ref beliefs()
{
return this.mDict;
}

makes your "private" variable not private any more, since you 
are leaking a reference to it. So any program calling 
"a.beliefs" can get a reference to your private data and modify 
it.


My guess is that (if ref is removed from the setter) it serves 
as a @property function:

https://dlang.org/spec/function.html#property-functions
So this allows the function to work in a statement like:
a.beliefs = 

And apparently it is prioritized over the "ref beliefs()" 
function.



I guess it is nicer to actually use the "setter" function, as 
it allows you to inspect what the private data will be assigned 
to and take appropriate measures, whereas with the "ref 
beliefs()" method the private data is open for any manipulation 
out of your control.


How strange! Nice find though, thanks for your help with this, I 
really appreciate it as this had been bugging me for a while.


So essentially, what is happening is, if the setter method 
expects a reference, then because the create() function isn't 
returning a reference but a copy, it actually does the following:


a.beliefs = Operator.create();

receives an object from create(), while a.beliefs retrieves a 
reference to agent.mBeliefs, and then effectively sets this 
reference to point to the object copied by create()?


Also, yes, I am using the setter method to play around with the 
precision of the double values, and do some normalising. I always 
want it to access the setter method, but I had hoped that, given 
it's an associative array, that the creation of the object in 
create() would simply create it on the heap and I could pass back 
a reference. It seems that I was incorrect, as if I set create() 
to explicitly return a reference, it complains about returning a 
reference to a local variable. Any advice on the best way to pass 
it as a reference?


Re: Passing a reference to a returned reference

2018-07-06 Thread Michael via Digitalmars-d-learn

On Friday, 6 July 2018 at 15:14:01 UTC, Michael wrote:

On Friday, 6 July 2018 at 14:50:39 UTC, Ali Çehreli wrote:

[...]


I'm just trying to do that now.

Here is what I have in terms of code:

[...]


This is definitely to do with my use of the setter syntax, which 
maybe I am misunderstanding? Because if I change it to a normal 
function call like so:


a.beliefs(Operator.create());

then it complains if I use ref, and doesn't complain if I don't.


Re: Passing a reference to a returned reference

2018-07-06 Thread Michael via Digitalmars-d-learn

On Friday, 6 July 2018 at 14:50:39 UTC, Ali Çehreli wrote:

On 07/06/2018 07:36 AM, Michael wrote:
> but not in
> my case, if this is a weird edge-case with setter member
functions?

This is all very interesting but I'm dying to see the code. :) 
Can you change Timoses's code to demonstrate your case?


Ali


I'm just trying to do that now.

Here is what I have in terms of code:

class Agent
{
private
{
double[int] mDict;
}

// Setter: copy
void beliefs(ref double[int] dict)
{
import std.stdio : writeln;
writeln("Setter function.");
this.mDict = dict;
}

// Getter
auto ref beliefs()
{
return this.mDict;
}
}

class Operator
{
static auto ref create()
{
double[int] dict;
dict[2] = 1.0;
dict[1] = 0.0;
return dict;
}
}

unittest
{
import std.stdio : writeln;

Agent a = new Agent();

a.beliefs = Operator.create();
assert(a.beliefs.keys.length == 2);

writeln(a.beliefs);
}


and here is the output WITH "ref" in beliefs's signature before 
double[int] dict:


➜  dempshaf git:(master) ✗ rdmd -I../ -unittest -main bug_test.d
[2:1, 1:0]

and without "ref":

➜  dempshaf git:(master) ✗ rdmd -I../ -unittest -main bug_test.d
Setter function.
[2:1, 1:0]


I'm surprised it's showing the correct value of the associative 
array, but hopefully this shows my point that it's not printing 
for some reason, when the parameter is ref.


Re: Passing a reference to a returned reference

2018-07-06 Thread Michael via Digitalmars-d-learn

On Friday, 6 July 2018 at 14:11:42 UTC, Timoses wrote:


This works for me:

auto create()
{
string[int] dict;
dict[2] = "hello";
return dict;
}

void modifyNoRef(string[int] m)
{
 writeln("Address not ref: ", );
 m[0] = "modified";
}

void modifyRef(ref string[int] m)
{
 writeln("Address ref: ", );
 m[1] = "modified";
}

unittest
{
auto r = create();
writeln("Address r: ", );
assert(r.keys.length == 1);
modifyNoRef(r);
assert(r.keys.length == 2); 
modifyRef(r);
assert(r.keys.length == 3); 
}

So either with ref or not as parameter storage class the assoc. 
array is modified. Note the address in the "ref" one is the 
same as "r" in the unittest.


[1]: https://dlang.org/spec/function.html#auto-ref-functions



I'm wondering, seeing as it works in the example given above, but 
not in my case, if this is a weird edge-case with setter member 
functions?


What I wanted to do was have the "Agent" object set its internal 
variable to point to the newly created associative array but 
instead it just seems to go right past the setter function.


Re: Passing a reference to a returned reference

2018-07-06 Thread Michael via Digitalmars-d-learn

On Friday, 6 July 2018 at 14:11:42 UTC, Timoses wrote:

On Friday, 6 July 2018 at 13:13:43 UTC, Michael wrote:

static auto ref consensus( ... )


`auto ref` infers the return type from the return statement 
[1]. So it's not necessarily returning a ref type.


However, I don't think this matters if the only concern you 
have is that the setter function actually modifies the passed 
object.




and the agent's setter method looks like the following:

void beliefs(ref double[int] beliefs)

Now obviously if I remove ref in the above signature, 
everything works fine, but I am surprised I wasn't getting any 
kind of warning about the reference because the setter 
function just doesn't run at all. I tried checking if (beliefs 
is null) but perhaps this isn't the correct way to check if an 
associative array's reference is no longer reachable?


Some explanation would be great, thanks!


This works for me:

auto create()
{
string[int] dict;
dict[2] = "hello";
return dict;
}

void modifyNoRef(string[int] m)
{
 writeln("Address not ref: ", );
 m[0] = "modified";
}

void modifyRef(ref string[int] m)
{
 writeln("Address ref: ", );
 m[1] = "modified";
}

unittest
{
auto r = create();
writeln("Address r: ", );
assert(r.keys.length == 1);
modifyNoRef(r);
assert(r.keys.length == 2); 
modifyRef(r);
assert(r.keys.length == 3); 
}

So either with ref or not as parameter storage class the assoc. 
array is modified. Note the address in the "ref" one is the 
same as "r" in the unittest.


[1]: https://dlang.org/spec/function.html#auto-ref-functions


Aah yes I had forgotten that I had set it as auto ref. However, 
even when I set that function to always return a copy of the 
associative array, it didn't change anything -- the issue always 
seems to be with the setter method expecting a reference.


So then, supposing it's returning a copy of the object. Given 
that my setter method was still expecting a reference, is there a 
reason why it doesn't even invoke the setter unless I change the 
parameter from expecting a reference to expecting a copy of the 
object? It's not like the function failed in any way, it just 
wouldn't run the function at all. Not even while I was printing 
something right at the top.


Passing a reference to a returned reference

2018-07-06 Thread Michael via Digitalmars-d-learn

Hello,

I'm a little confused about what is actually happening when I try 
to pass a reference, returned by a method that produces the 
object (associative array), to a setter method which expects a 
reference. What seems to be happening is that it simply does 
nothing, as if the setter method is never called.


I'll try to provide some code to highlight the signatures.

The setter method being called on the reference returned by the 
method that produces the array:


agent.beliefs = combination( ... ); // should receive a 
reference, and pass this to the setter method.


Combination is a function point to one of two functions, whose 
signatures look like:


static auto ref consensus( ... )

and the agent's setter method looks like the following:

void beliefs(ref double[int] beliefs)

Now obviously if I remove ref in the above signature, everything 
works fine, but I am surprised I wasn't getting any kind of 
warning about the reference because the setter function just 
doesn't run at all. I tried checking if (beliefs is null) but 
perhaps this isn't the correct way to check if an associative 
array's reference is no longer reachable?


Some explanation would be great, thanks!


Re: ldc flags for beginner

2018-04-22 Thread Michael via Digitalmars-d-learn

On Sunday, 22 April 2018 at 02:08:40 UTC, fevasu wrote:
what flags to use so that the intermediate .o files are 
discared by ldc and only a.out is written to disk


You can also use rdmd with ldc, if that makes things easier.


Re: How to delete element from array container or dlist?

2018-03-18 Thread Michael via Digitalmars-d-learn

On Sunday, 18 March 2018 at 15:42:18 UTC, Andrey Kabylin wrote:

On Sunday, 18 March 2018 at 15:32:47 UTC, Michael wrote:

On Sunday, 18 March 2018 at 14:58:52 UTC, Andrey Kabylin wrote:
In DList we have method remove, but I can't understand how 
this method works, I want write somethink like this:

void unsubscribe(EventsSubscriber subscriber) {
subscribers.remove(subscriber);
}


So I guess you would want something like

subscribers.remove!(a => a == subscriber));

which is the different definition of remove available here:
https://dlang.org/phobos/std_algorithm_mutation.html#.remove.2


Yes this works, thanks!


No problem, glad to help!


Re: How to delete element from array container or dlist?

2018-03-18 Thread Michael via Digitalmars-d-learn

On Sunday, 18 March 2018 at 14:58:52 UTC, Andrey Kabylin wrote:
In DList we have method remove, but I can't understand how this 
method works, I want write somethink like this:

void unsubscribe(EventsSubscriber subscriber) {
subscribers.remove(subscriber);
}


The remove function seems to expect an index, not an element.


Re: How to delete element from array container or dlist?

2018-03-18 Thread Michael via Digitalmars-d-learn

On Sunday, 18 March 2018 at 14:58:52 UTC, Andrey Kabylin wrote:
In DList we have method remove, but I can't understand how this 
method works, I want write somethink like this:

void unsubscribe(EventsSubscriber subscriber) {
subscribers.remove(subscriber);
}


So I guess you would want something like

subscribers.remove!(a => a == subscriber));

which is the different definition of remove available here:
https://dlang.org/phobos/std_algorithm_mutation.html#.remove.2


Re: I want to transmit the class name and the member name in the method

2018-01-05 Thread Michael via Digitalmars-d-learn

On Friday, 5 January 2018 at 12:19:11 UTC, thedeemon wrote:

On Friday, 5 January 2018 at 07:40:14 UTC, Brian wrote:

I think code style like:
db.select(User).where(email.like("*@hotmail.com")).limit(10);


You need to read about templates in D, here's a good guide:
https://github.com/PhilippeSigaud/D-templates-tutorial

Basically you can write a function like
auto select(Class, string fieldName)(Class value) {
 ...

and call it later as
select!(User, "name")(u);

Here User and "name" are compile-time arguments, you can pass 
there types, values and more exotic stuff like other templates.


And inside the function you can use
__traits(getMember, u, fieldName)
to get field by its name from the passed value.
See: https://dlang.org/spec/traits.html


This was a really nice, short example btw. I'm not OP, but thanks 
for that.


Re: Alias example should supposedly be illegal, but runs fine

2017-12-19 Thread Michael via Digitalmars-d-learn

On Tuesday, 19 December 2017 at 02:12:29 UTC, Mike Franklin wrote:

On Tuesday, 19 December 2017 at 02:04:34 UTC, codephantom wrote:


 writeln(S.j);
 // Error: Instance symbols cannot be used through types.


I don't understand why you would say that is a bug.



I meant that the example is wrong, and a bug report should be 
filed to fix the example.


Mike


Hmm.. but the example is explicitly dealing with when it is valid 
to create an alias for a non-static struct member. Should it 
still not be int? Even if you cannot change it via that alias?


Re: Alias example should supposedly be illegal, but runs fine

2017-12-19 Thread Michael via Digitalmars-d-learn

On Tuesday, 19 December 2017 at 01:29:04 UTC, Meta wrote:

On Monday, 18 December 2017 at 23:44:46 UTC, Michael wrote:

[...]


I think the reason that this works is because i is static, 
meaning that you don't need the `this` reference of S to access 
it and thus it can be aliased. Declaring a static class or 
struct variable is pretty much the same as declaring a global 
variable, just with a tighter scope. If you look at it that 
way, then this makes a lot more sense. If you declare a global 
variable i at module scope, of course you can create an alias 
for it.


Yes I think you're right. I wasn't sure what was going on, just 
noticed that the example definitely isn't right. I'll file a bug 
support and try and fix it.


Alias example should supposedly be illegal, but runs fine

2017-12-18 Thread Michael via Digitalmars-d-learn

Hello,

I have been looking at the following example found right at the 
end of the section here: 
https://dlang.org/spec/declaration.html#alias


struct S { static int i; }
S s;

alias a = s.i; // illegal, s.i is an expression
alias b = S.i; // ok
b = 4; // sets S.i to 4

and it runs fine to me, including if I add:

a = 3;

So, to me I don't see why either can't be valid, but either way 
something needs to be fixed to reflect that this is no longer 
illegal in DMD v2.077.1.


Re: Is there a pragma or command line option to ask DMD to show a warning when it implicitly casts from long to ulong ?

2017-12-15 Thread Michael via Digitalmars-d-learn
On Friday, 15 December 2017 at 21:29:10 UTC, Jonathan M Davis 
wrote:
On Friday, December 15, 2017 20:40:10 Ecstatic Coder via 
Digitalmars-d-learn wrote:
It's taken me some time to find an implicit cast bug ("if 
(my_sometimes_negative_index >= this_array.length)"), while a 
simple C++-like implicit-cast warning would have allowed me to 
find this nasty bug WAY sooner...


No.

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

- Jonathan M Davis


How does something like this get left for so long...


Re: Shared and race conditions

2017-11-29 Thread Michael via Digitalmars-d-learn
On Wednesday, 29 November 2017 at 17:03:42 UTC, Steven 
Schveighoffer wrote:

On 11/29/17 11:13 AM, Wanderer wrote:
I'm trying to simulate a race condition in D with the 
following code:


(https://run.dlang.io/is/RfOX0I)


One word of caution, I think the running of your code on 
run.dlang.io is cached. Refreshing doesn't make it re-run.


https://run.dlang.io/is/k0LhQA

-Steve


Ah good catch.


Re: Shared and race conditions

2017-11-29 Thread Michael via Digitalmars-d-learn
On Wednesday, 29 November 2017 at 16:33:50 UTC, Steven 
Schveighoffer wrote:

On 11/29/17 11:13 AM, Wanderer wrote:

[...]


[snip]


[...]


Using the compiler switch -vcg-ast, I see no synchronization of 
these methods.


[...]


Any idea what has changed in DMD-nightly to retain the correct 
ordering of IDs?


Re: Shared and race conditions

2017-11-29 Thread Michael via Digitalmars-d-learn

On Wednesday, 29 November 2017 at 16:19:05 UTC, Michael wrote:

On Wednesday, 29 November 2017 at 16:13:13 UTC, Wanderer wrote:

[...]


I unfortunately cannot answer your question but I am noticing 
that running the code with DMD gives you an unordered sequence 
of IDs, but running with DMD-nightly gives you a sequence in 
the correct order. I wonder what has changed to do with threads 
that causes this difference between compiler versions.


Just to add to this, the page here:
https://tour.dlang.org/tour/en/multithreading/synchronization-sharing
does suggest that the compiler will automatically create critical 
sections when it can i.e. wrapping a section in


synchronized {
importStuff();
}


Re: Shared and race conditions

2017-11-29 Thread Michael via Digitalmars-d-learn

On Wednesday, 29 November 2017 at 16:13:13 UTC, Wanderer wrote:
I'm trying to simulate a race condition in D with the following 
code:


(https://run.dlang.io/is/RfOX0I)

```
import std.stdio;
import core.thread;
import std.concurrency;

shared struct IdGen(T)
{
T id;

this(T start)
{
id = start;
}

T next()
{
id = id.next();
return id;
}
}

struct MyId
{
uint num;

shared MyId next()
{
return MyId(num + 1);
}
}

static void getId(shared IdGen!(MyId)* g)
{
writeln("next: ", g.next());
writeln("next: ", g.next());
}

void main()
{
MyId start = {0};
auto g = IdGen!(MyId)(start);

auto num = 12;
for (auto i = 0; i < num; i++)
{
spawn(, );
}

thread_joinAll();
writeln("Done");
}
```

But all I get is correct IDs without any sign of a race (i.e. 
duplicate ids).

Does compiler add synchronization for `shared` data?


I unfortunately cannot answer your question but I am noticing 
that running the code with DMD gives you an unordered sequence of 
IDs, but running with DMD-nightly gives you a sequence in the 
correct order. I wonder what has changed to do with threads that 
causes this difference between compiler versions.


Re: reduce condition nesting

2017-11-23 Thread Michael via Digitalmars-d-learn
On Thursday, 23 November 2017 at 14:16:25 UTC, Andrea Fontana 
wrote:
On Thursday, 23 November 2017 at 13:47:37 UTC, Adam D. Ruppe 
wrote:

On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:

for instance in kotlin it can be replace with this:

when {
c1 -> foo(),
c2 -> bar(),
c3 -> ...
else -> someDefault()
}


The `switch` statement covers some of these cases too.


Anyway you can create something like this:
https://run.dlang.io/is/7pbVXT


That's pretty cool!


Re: html fetcher/parser

2017-08-12 Thread Michael via Digitalmars-d-learn

On Saturday, 12 August 2017 at 20:22:44 UTC, Adam D. Ruppe wrote:

On Saturday, 12 August 2017 at 19:53:22 UTC, Faux Amis wrote:

[...]


My dom.d and http2.d combine to make this easy:

https://github.com/adamdruppe/arsd/blob/master/dom.d
https://github.com/adamdruppe/arsd/blob/master/http2.d

[...]


Sometimes it feels like there's the standard D library, Phobos, 
and then for everything else you have already developed a 
suitable library to supplement it haha!


Re: Vectorflow noob

2017-08-11 Thread Michael via Digitalmars-d-learn

On Thursday, 10 August 2017 at 19:10:05 UTC, Jiyan wrote:

Hey,
wanted to the following simple thing with vectorflow:

[...]


I'm worried there might not be many on the forums who can help 
too much with vectorflow given how new it is. Maybe some in the 
community are more familiar with neural nets and have played wit 
vectorflow already, but I'm not sure. I hope somebody can drop in 
to give you a hand.


Re: Why free and realloc seem to include .

2017-08-03 Thread Michael via Digitalmars-d-learn

On Thursday, 3 August 2017 at 15:29:29 UTC, Adam D. Ruppe wrote:

On Thursday, 3 August 2017 at 15:18:17 UTC, Michael wrote:
I've not seen that either, though I'm not a C++ programmer. 
Does using free() on its own not assume access of a global 
namespace?


Consider the following:

class Foo {
   void free(void*);

   void other_method() {
  free(ptr); // calls the member function
   }
}


The leading dot in D just ensures it calls the global one 
instead of a member (if present).


So it could be used without, but you risk conflicts with other 
functions. I got it, thanks to both of you.


Re: Why free and realloc seem to include .

2017-08-03 Thread Michael via Digitalmars-d-learn

On Thursday, 3 August 2017 at 14:15:40 UTC, Temtaime wrote:

On Thursday, 3 August 2017 at 14:03:56 UTC, Michael wrote:
So this might be a bit of a stupid question, but looking at 
the DMD source code (dmodule.d in particular) I see the 
following code:



[...]


and I was just wondering why certain functions seem to be 
called using the dot operator on its own, unattached to some 
object. This is probably a naive question but I haven't seen 
this in my limited experience using D and I was just wondering 
why this is. I have only really seen this relating to D's 
manual memory management. But in the same file, I see examples 
like this:



[...]


so what is the case when you should use .free() and why not 
just free()? Thanks.


Dot is equal to C++'s :: operator to access a global namespace.
Aka ::free(ptr);


I've not seen that either, though I'm not a C++ programmer. Does 
using free() on its own not assume access of a global namespace?


Why free and realloc seem to include .

2017-08-03 Thread Michael via Digitalmars-d-learn
So this might be a bit of a stupid question, but looking at the 
DMD source code (dmodule.d in particular) I see the following 
code:



if (srcfile._ref == 0)
   .free(srcfile.buffer);
srcfile.buffer = null;
srcfile.len = 0;


and I was just wondering why certain functions seem to be called 
using the dot operator on its own, unattached to some object. 
This is probably a naive question but I haven't seen this in my 
limited experience using D and I was just wondering why this is. 
I have only really seen this relating to D's manual memory 
management. But in the same file, I see examples like this:



FileName.free(n);


so what is the case when you should use .free() and why not just 
free()? Thanks.


Re: bug in foreach continue

2017-03-17 Thread Michael via Digitalmars-d-learn

On Friday, 17 March 2017 at 11:30:48 UTC, Jonathan M Davis wrote:
On Friday, March 17, 2017 01:55:19 Hussien via 
Digitalmars-d-learn wrote:


I tend to agree with this. If the foreach is static, and 
continue and break are just going to be ignored, then they 
should just be illegal. Allowing them is just going to confuse 
people. Now, making it so that they actually work statically 
has some interesting possibilities, but that would fall apart 
as soon as you have any constructs that would use continue or 
break (e.g. a loop or switch statement) inside the static 
foreach, and it might break code in rare cases. So, we're 
probably better off just making them illegal. But having them 
be legal just seems disingenious, since they don't do anything.


- Jonathan M Davis


What exactly IS happening in the case of a continue in a 
static-if? I could sort of imagine that maybe if you were 
expecting the loop to be unrolled, that you then have a continue 
statement in the correct part of the unrolled loop. But I take it 
this isn't what's happening?


Re: D to C++

2016-08-31 Thread Michael via Digitalmars-d-learn

On Wednesday, 31 August 2016 at 11:43:12 UTC, Nick wrote:

On Tuesday, 30 August 2016 at 14:24:22 UTC, eugene wrote:

On Tuesday, 30 August 2016 at 13:33:44 UTC, Nick wrote:

Is it possible to compile from D to C++?

Explanation:
I do some competition programming and would like to write it 
in D instead of C++ :)


maybe will help https://wiki.dlang.org/Calypso


That's quite nice, but not what I'm looking for.
What Calypso does, as far as I can see, is to make it possible 
to compile C++ and D together. I'm looking for a compiler that 
takes in D code and spits out either C or C++ code.


I can't imagine that's been done, and I'm not sure it will be 
high on anybody's list of priorities I'm afraid. A lot of work 
was done on automating C++ -> D, but converting D, a language 
intended to replace C++, to C++ itself seems a little backwards. 
I get why you'd like to use it, but I don't think it's been done, 
sorry.


Re: Using -O with DMD seems to produce non-random values.

2016-05-19 Thread Michael via Digitalmars-d-learn

On Thursday, 19 May 2016 at 15:49:17 UTC, ag0aep6g wrote:

On 05/19/2016 05:09 PM, Michael wrote:
Any idea what causes this to occur when optimising? I wanted 
to try and

speed up a simulation I'm running but it just produces too many
unexpected consequences.


I suspect that you're seeing issue 16027 [1], a bad bug in 
2.071.0. The upcoming 2.071.1 point release should have it 
fixed.



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


Yeah that's what I was thinking but I don't know much about the 
optimisation process so I wanted to mention it anyway.


Re: Using -O with DMD seems to produce non-random values.

2016-05-19 Thread Michael via Digitalmars-d-learn
Could it be that the code is optimised to the same as that in the 
original issue and so the current compiler still produces the 
incorrect result? Obviously the original issue has since been 
fixed but I won't be able to test this until the next version of 
DMD is released.




Using -O with DMD seems to produce non-random values.

2016-05-19 Thread Michael via Digitalmars-d-learn
I'm not entirely sure what optimisations are made when supplying 
the -O flag to rdmd, but this may be related to an earlier issue 
I found for similar code here:

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

The code is:
void main()
{
auto seed = 128;
auto rand = Random(seed);
double[] values;

values = generateValues(rand, 10);

writeln(values);
}

double[] generateValues(ref Random rand, int l)
{
auto values = new double[](l);
foreach (ref val; values)
{
auto value = 1.0;
if (uniform(0, 2, rand))
{
value = value * -1;
}
val = value;
}

return values;
}

Which returns different values depending on whether -O is passed:

$rdmd testing_optimiser.d
[1, -1, -1, 1, -1, 1, 1, 1, -1, -1]
$rdmd -O testing_optimiser.d
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]


Any idea what causes this to occur when optimising? I wanted to 
try and speed up a simulation I'm running but it just produces 
too many unexpected consequences.


Re: Using shorthand *= leads to unexpected result?

2016-05-15 Thread Michael via Digitalmars-d-learn

On Sunday, 15 May 2016 at 14:12:47 UTC, Marc Schütz wrote:

On Sunday, 15 May 2016 at 13:01:45 UTC, Michael wrote:
It may be that I'm doing something wrong here, but after 
updating DMD to the latest version, my simulations started 
producing some very odd results and I think I've pinpointed it 
to a sign inversion that I was making. Here is some code from 
dpaste to demonstrate the behaviour I get vs. the behaviour I 
expected:

https://dpaste.dzfl.pl/9bd7aea75fb2

Any ideas? Am I getting something wrong or is this some sort 
of regression with the latest DMD?


It's a regression introduced here:
https://github.com/dlang/dmd/pull/5534

I've filed a bug report:
https://issues.dlang.org/show_bug.cgi?id=16027


Thanks for filing the bug report, and good find with the 
regression.


Re: Using shorthand *= leads to unexpected result?

2016-05-15 Thread Michael via Digitalmars-d-learn

On Sunday, 15 May 2016 at 13:12:44 UTC, Saurabh Das wrote:

On Sunday, 15 May 2016 at 13:01:45 UTC, Michael wrote:
It may be that I'm doing something wrong here, but after 
updating DMD to the latest version, my simulations started 
producing some very odd results and I think I've pinpointed it 
to a sign inversion that I was making. Here is some code from 
dpaste to demonstrate the behaviour I get vs. the behaviour I 
expected:

https://dpaste.dzfl.pl/9bd7aea75fb2

Any ideas? Am I getting something wrong or is this some sort 
of regression with the latest DMD?


Strangely, if I substitute the variable type 'auto' for 'float' 
or 'real', it gives the expected output of -1. Variable type 
'double' gives the incorrect value of 1.


This is strange behaviour. Perhaps someone more knowledgeable 
can shed light. If it is a regression, it is a very serious 
regression :(


~ sd


Well I'm pretty sure the code was working just fine earlier in 
the week at the office, but running the code at home with the 
newest version of DMD started producing these odd results.


Using shorthand *= leads to unexpected result?

2016-05-15 Thread Michael via Digitalmars-d-learn
It may be that I'm doing something wrong here, but after updating 
DMD to the latest version, my simulations started producing some 
very odd results and I think I've pinpointed it to a sign 
inversion that I was making. Here is some code from dpaste to 
demonstrate the behaviour I get vs. the behaviour I expected:

https://dpaste.dzfl.pl/9bd7aea75fb2

Any ideas? Am I getting something wrong or is this some sort of 
regression with the latest DMD?


Re: Using D in Android App

2016-04-18 Thread Michael via Digitalmars-d-learn

On Monday, 18 April 2016 at 09:38:48 UTC, Justice wrote:

On Saturday, 16 April 2016 at 04:04:24 UTC, Justice wrote:
Is it difficult to create a D business like app and connect it 
to android through java for the interface?


I'd rather create all the complex stuff in D and either use it 
natively through java(I need a UI).


If it is workable, can the same be said for IOS(just recompile 
the D source to the IOS architecture then use it in an IOS app 
for the ui)?


Anyone?


If you're treating the Android/iOS applications as purely 
"client-side", then there's no reason why you can't create a 
"server-side" application in D using something like vibe.d and 
then passing data between clients and servers using JSON or 
similar data interchange formats. That way,you only develop the 
main server-side application in D, and can communicate with it 
using any other language (Java/C#/Swift) on other operating 
systems and devices.


Re: scope block do not handle failure, but try-catch does

2014-12-11 Thread Michael via Digitalmars-d-learn

On Thursday, 11 December 2014 at 20:40:40 UTC, Suliman wrote:

string dbname = config.getKey(dbname1);
scope(failure) writeln(look like dbname is missing);

I am using dini and trying to throw exception if value can't be 
extract from config. If I am wrap it's in try-сефср block it's 
work or. But in this situation scope block do not execute and I 
see only stack tracing error on console.


Why? What's wrong. By idea if block failure scope should execute


I'm not 100% sure on this, but I suspect you have to declare the 
scope(failure) before you call the code that might execute it.


e.g.

scope(failure) writeln(Looks like the dbname is missing);
string dbname = config.getKey(dbname1);


how to access a ftp sever with socket

2014-12-08 Thread michael via Digitalmars-d-learn
Hello Anyone:
 i am trying make a ftp client with socket,i have tried std.net.curl,but i 
cont stand with so many try-catch structure in my code,i am not familiar with
socket,i write a pecie of code but it cont give me the welcome message which i 
want,and then i use wireshark to trace the comunication,i found that
ftp server:21 send the message to my client:1024,but why there is nothing in 
str buf?thanks for your help.
import std.socket,std.stdio;
void main(){
 string ip = **;//i hide the address
 int msecs = 1000;
 auto ftp = getAddressInfo(ip,ftp);
 Socket listener = new TcpSocket;
 listener.bind(new InternetAddress(1024));
 listener.connect(ftp[0].address);
 auto pair = socketPair();
 auto sock = pair[0];
 sock.setOption(SocketOptionLevel.SOCKET,SocketOption.RCVTIMEO, 
dur!msecs(msecs));
 char[1024] buf;
 sock.receive(buf);
 writeln(buf);
}

 ‍

Re: threading issues with D - C - Python

2014-12-08 Thread Michael via Digitalmars-d-learn

On Monday, 8 December 2014 at 01:17:16 UTC, Ellery Newcomer wrote:

On 12/07/2014 03:12 PM, Michael wrote:
On Saturday, 6 December 2014 at 00:40:49 UTC, Ellery Newcomer 
wrote:

On 12/04/2014 10:55 PM, Ellery Newcomer wrote:

I guess tomorrow I can try messing around with 
thread_attachThis, as the
fullcollect happening in #2 might be screwing with python 
data. But you
aren't really passing anything from python to d or vice 
versa, so I'm
not sure why the gc would need to know about the python 
threads.


by gum, thread_attachThis and thread_detachThis fix the issue!

now to figure out how to use them in the general case.


This is great.. Thank you. I'm looking forward to being able 
to try the

finished result.


It would be great if there were some convenient hook in python 
to stick these calls. Near as I can tell, there isn't. That 
leaves you with either explicitly calling attach and detach 
with an exposed api, or pyd obsessively checking whether the 
current thread is registered.


Actually, I suppose with a thread local flag the latter 
wouldn't be too bad.


Mind if I incorporate your example into pyd's test suite?


Not at all. Go for it.


Re: threading issues with D - C - Python

2014-12-07 Thread Michael via Digitalmars-d-learn
On Saturday, 6 December 2014 at 00:40:49 UTC, Ellery Newcomer 
wrote:

On 12/04/2014 10:55 PM, Ellery Newcomer wrote:

I guess tomorrow I can try messing around with 
thread_attachThis, as the
fullcollect happening in #2 might be screwing with python 
data. But you
aren't really passing anything from python to d or vice versa, 
so I'm
not sure why the gc would need to know about the python 
threads.


by gum, thread_attachThis and thread_detachThis fix the issue!

now to figure out how to use them in the general case.


This is great.. Thank you. I'm looking forward to being able to 
try the finished result.


Re: threading issues with D - C - Python

2014-12-04 Thread Michael via Digitalmars-d-learn
On Thursday, 4 December 2014 at 03:22:05 UTC, Ellery Newcomer 
wrote:


dustmite?


Not sure what went wrong with dustmite, but every time I tried it 
it just started deleting all the files in the directory and 
setup.py would give errors. I manually deleted a reasonable chunk 
of the code and I'm left with these files which still seem to 
cause segfaults:


Main code: http://pastebin.com/zqgNTk9w
PyD definitions: http://pastebin.com/6mRH3KZZ
setup.py: http://pastebin.com/i9Ph78UC
test code that causes segfaults: http://pastebin.com/1ukzShVh

Cheers,
Michael.


Re: threading issues with D - C - Python

2014-12-03 Thread Michael via Digitalmars-d-learn
On Wednesday, 3 December 2014 at 06:11:56 UTC, Ellery Newcomer 
wrote:
are you looking at this pyd: 
https://bitbucket.org/ariovistus/pyd


I'm looking at this one, which is what came up when googling 
python to D

http://pyd.dsource.org/


Re: threading issues with D - C - Python

2014-12-03 Thread Michael via Digitalmars-d-learn
On Wednesday, 3 December 2014 at 06:30:07 UTC, Russel Winder via 
Digitalmars-d-learn wrote:


As far as I can tell PyD is still active, but in a non-funded 
FOSS way,
i.e. work happens as and when volunteers put time and effort 
in. I
haven't tried PyD recently but it worked fine last time I did. 
If can
set out what you tried and what didn't work, maybe there is a 
PyD

solution, or a fix to PyD to give a solution?


Yeah apparently I might have used the wrong PyD. This might end 
up being all I need to do to fix my problem.



D's big problem is shared objects/dynamic link libraries. 
Without them
you cannot interwork with Python at all. I have tried 
experiments on
Linux creating shared libraries from D code with C linkage 
entry points
to create classic Python extensions, and it appears to work 
fine. Except
for having to start up the D heap and thread  management, 
should they be
needed. But that is what PyD is there for. If I took my 
experiments any

further I would end up recreating PyD or something like it.


This is what I'm doing. I'm using the rt_init() function to setup 
the heap/thread management. Am I missing anything else here? It 
seems the issue is definitely more complicated than that.


I'll also point out that: http://dlang.org/interfaceToC had no 
information on calling rt_init first, although it seems like it 
should.



It sounds like you are in a similar situation except that you 
appear to
have an extra layer of C code. I am not sure a layer of C is 
needed
between Python and D, it would be good to know more about why 
you seem

to need it.


Well I wanted to compile D code and directly call it in Python, 
and since PyD didn't work for me, I instead tried the python- C 
interface I already knew 
(https://docs.python.org/2/c-api/index.html) and worked my way 
through a C-D interface, which I understood to be relatively 
simple.


My guess would be not properly initializing the D 
infrastructure from

the incoming Python thread.

I would suggest that you want to avoid threads crossing the 
boundaries
and just pass data via a shared channel. Unix pipes seem to 
work well in
this context since they provide a language independent data 
channel.


Yeah I'm leaning in that direction myself, although I might try 
the other PyD library first. I wanted to be able to use the D 
message-passing libraries to do the thread-safety stuff at first, 
because it was much easier than the alternative, but I'm not sure 
that's true anymore.


Re: threading issues with D - C - Python

2014-12-03 Thread Michael via Digitalmars-d-learn
On Wednesday, 3 December 2014 at 21:35:48 UTC, ketmar via 
Digitalmars-d-learn wrote:
ah, dsource strikes back! that vile site keep biting us again 
and

again. let's hope that new admins will kill it for good.


Yeah. I've got the new PyD and it compiles and does everything I 
want much nicer, but it appears to have the exact same problems. 
When calling a python thread to my code, it can cause segfaults 
and hanging issues.


Cheers,
Michael.


Re: threading issues with D - C - Python

2014-12-03 Thread Michael via Digitalmars-d-learn
On Thursday, 4 December 2014 at 02:31:51 UTC, Ellery Newcomer 
wrote:


okay. that's not too surprising.

If you can get me a minimal example, I'd be happy to have a 
look since pyd should probably support this case.


Cool. Unfortunately most of the times I've attempted to reduce 
this down it always seems to work, but I think that's because I 
often did the example code in D. I'll play around with it and try 
to send you an example.


Cheers,
Michael.


threading issues with D - C - Python

2014-12-02 Thread Michael via Digitalmars-d-learn
Hi. I'm new here and this is my first post. I'm not sure this is 
the right subforum for it, but wasn't sure where else to put it 
either.


I've written a library to talk to some external hardware using a 
socket. It uses the std.concurrency threads to send messages 
between the main D-object for the hardware and the D-object for 
the sockets. I then wanted to be able to call these functions 
from Python. PyD appeared to be out of date, so I've been using a 
D - C interface, and a C - Python interface. The python code 
will often run from different python threads, so I then added yet 
another message-passing layer between the D-C interface and the 
D-hardware interface.


My problem is that this code routinely causes segmentation 
faults. I've spent a long time going through trying to figure out 
exactly what the causes are. I think there have been some related 
to D-exceptions not being handled gracefully by the C/Python 
code. Some more by stdout writing from multiple threads (which 
surprised me).


I'm fairly sure I have tackled both of these issues, but it still 
seems like Python threads and D threads don't mix well. When 
running the same functions from D, I am able to get no errors, 
but when run from Python/C it causes segfaults reliably.


Sorry for the large exposition. I am currently at the point of 
suspecting bugs in Phobos, but I am unskilled enough to tell for 
sure, and would appreciate any help.


The latest core dump gives a backtrace of almost entirely phobos 
commands:


#0  0x7fe789ad3b97 in gc.gc.Gcx.fullcollect() () from 
/lib/libphobos2.so.0.66
#1  0x7fe789ad3294 in gc.gc.Gcx.bigAlloc() () from 
/lib/libphobos2.so.0.66
#2  0x7fe789ad0df1 in gc.gc.GC.mallocNoSync() () from 
/lib/libphobos2.so.0.66
#3  0x7fe789ad0c15 in gc.gc.GC.malloc() () from 
/lib/libphobos2.so.0.66
#4  0x7fe789ad6470 in gc_malloc () from 
/lib/libphobos2.so.0.66
#5  0x7fe789ae6d36 in _d_newitemT () from 
/lib/libphobos2.so.0.66
#6  0x7fe789e57112 in 
std.array.__T8AppenderTAaZ.Appender.__T3putTAxaZ.put() () from 
/usr/lib/libv5camera.so
#7  0x7fe789e570b5 in 
std.array.__T8AppenderTAaZ.Appender.__T3putTAxaZ.put() () from 
/usr/lib/libv5camera.so
#8  0x7fe789e562dc in 
std.array.__T8AppenderTAaZ.Appender.__T3putTAaZ.put() () from 
/usr/lib/libv5camera.so
#9  0x7fe789e561ea in 
std.array.__T8AppenderTAaZ.Appender.__T3putTxwZ.put() () from 
/usr/lib/libv5camera.so
#10 0x7fe789e5617d in 
std.format.__T10formatCharTS3std5array16__T8AppenderTAaZ8AppenderZ.formatChar() 
() from /usr/lib/libv5camera.so
#11 0x7fe789e56132 in 
std.format.__T10formatCharTS3std5array16__T8AppenderTAaZ8AppenderZ.formatChar() 
() from /usr/lib/libv5camera.so
#12 0x7fe789e61f09 in 
std.concurrency.MessageBox.__T3getTS4core4time8DurationTDFNfAyaiZvZ.get() 
() from /usr/lib/libv5camera.so
#13 0x7fe789e5b4ac in 
std.concurrency.MessageBox.__T3getTS4core4time8DurationTDFNaNbNiNfAyaiZvZ.get() 
() from /usr/lib/libv5camera.so
#14 0x7fe789e57e8d in 
std.typecons.__T5TupleTAyaTiTG65536kZ.Tuple.__T6__ctorTS3std8typecons24__T5TupleTAyaTiTG65536kZ5TupleZ.__ctor() 
()

   from /usr/lib/libv5camera.so
#15 0x7fe789e581f1 in 
std.variant.__T8VariantNVmi32Z.VariantN.__T7handlerTS3std8typecons24__T5TupleTAyaTiTG65536kZ5TupleZ.handler() 
()

   from /usr/lib/libv5camera.so
#16 0x7fe789e57d0f in 
std.typecons.__T5TupleTAyaTiTG65536kZ.Tuple.__T8opEqualsTS3std8typecons24__T5TupleTAyaTiTG65536kZ5TupleZ.opEquals() 
()

   from /usr/lib/libv5camera.so
#17 0x7fe789e57ba8 in 
std.typecons.__T5TupleTAyaTiTG65536kZ.injectNamedFields() () from 
/usr/lib/libv5camera.so
#18 0x7fe789e62087 in 
std.concurrency.MessageBox.__T3getTS4core4time8DurationTDFNfAyaiZvZ.get() 
() from /usr/lib/libv5camera.so
#19 0x7fe789e621a3 in 
std.concurrency.MessageBox.__T3getTS4core4time8DurationTDFNfAyaiZvZ.get() 
() from /usr/lib/libv5camera.so
#20 0x7fe789e5b7f6 in 
std.concurrency.MessageBox.__T3getTS4core4time8DurationTDFNaNbNiNfAyaiZvZ.get() 
() from /usr/lib/libv5camera.so
#21 0x7fe789ac7d51 in core.thread.Thread.run() () from 
/lib/libphobos2.so.0.66
#22 0x7fe789ac6f95 in thread_entryPoint () from 
/lib/libphobos2.so.0.66
#23 0x7fe79cee5182 in start_thread (arg=0x7fe77aca5700) at 
pthread_create.c:312
#24 0x7fe79cc11fbd in clone () at 
../sysdeps/unix/sysv/linux/x86_64/clone.S:111


Cheers,
Michael.



Re: threading issues with D - C - Python

2014-12-02 Thread Michael via Digitalmars-d-learn
Thanks for this. Its definitely a step in the right direction. 
Would you mind explaining a bit more about the problem here, if 
you can? I don't fully understand why the garbage collector needs 
to know about the threads, and if so for how long does it need to 
know? If I put in 
thread_attachThis();scope(exit)thread_detachThis(); it doesn't 
appear to fix my problems, so I'm definitely curious as to what 
is going on under the hood.


Cheers,
Michael.

On Wednesday, 3 December 2014 at 01:17:43 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Wed, 03 Dec 2014 01:07:42 +
Michael via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:

I'm fairly sure I have tackled both of these issues, but it 
still seems like Python threads and D threads don't mix well. 
When running the same functions from D, I am able to get no 
errors, but when run from Python/C it causes segfaults 
reliably.
you are right, D threads and other language/library threads 
aren't mix

well. at least you have to use `thread_attachThis()` and
`thread_detachThis()` from core.threads module to make sure 
that GC is
aware of alien threads. and i assume that calling this 
functions

from python will not be very easy.

but it's better to not mix 'em at all if it is possible.




Re: threading issues with D - C - Python

2014-12-02 Thread Michael via Digitalmars-d-learn
On Wednesday, 3 December 2014 at 02:41:11 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Wed, 03 Dec 2014 02:21:45 +
Michael via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:

Thanks for this. Its definitely a step in the right direction. 
Would you mind explaining a bit more about the problem here, 
if you can? I don't fully understand why the garbage collector 
needs to know about the threads, and if so for how long does 
it need to know? If I put in 
thread_attachThis();scope(exit)thread_detachThis(); it 
doesn't appear to fix my problems, so I'm definitely curious 
as to what is going on under the hood.
you have to call `thread_attachThis();` in alien thread, not 
in D
thread. i.e. if you created thread from python code, you have 
to call
`thread_attachThis();` in that python thread (i don't know how 
you'll
do that, but you must ;-). and you must call 
`thread_detachThis();`

from the same python thread before exiting from it.

garbage collector must know about all running threads so it can 
scan
their stacks, variables and so on. as there is no portable way 
to set
application-wide hooks on thread creation and termination, you 
must

inform GC about that events manually.

the other thing you can do is to not use any D allocated data in
alien threads. i.e. don't pass anything that was allocated by 
D code
to python thread and vice versa. if you want to pass some data 
to
alien thread, `malloc()` the necessary space, copy data to it 
and
pass malloc'ed pointer. don't forget to free that data in 
alien
thread. but i think that this is not what you really want, as 
it means
alot of allocations and copying, and complicates the whole 
thing alot.



alien is the thread that was created outside of D code.


Okay. Well I am already not passing any D-allocated data. I'm 
specifically creating variables/arrays on the C-stack, and then 
passing the pointer of that to D and overwriting the data of the 
C-stack pointer for any return values. I was worried about that 
specific problem and I thought this would be a solution. I am 
then able to tell python to use the C-stack variable without 
having to worry about D trying to run any garbage collection on 
it.


Going the other way, I probably am passing some python strings 
etc.. into D, but I would assume they are valid for the lifetime 
of the function call, and that D would have no reason to try and 
perform any garbage collection on them.


help

2014-11-19 Thread michael via Digitalmars-d-learn
Hello All:
when i am using std.net.curl to download a file that dosent exist on ftp 
sever,my code
will get an execption like this
 ‍std.net.curl.CurlException@std\net\curl.d(3605): Remote file not found on 
handle
 1A8E038

0x0042EC05
0x004027F6
and then it exits,however what i want when it occers is that my code shows an 
error message  like
please check the url you typed blablabla not the one above,can anyone give a 
hand on me ?

thanks a lot.

Michael.