Re: how to link self made lib using dub

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

On Friday, 6 July 2018 at 21:13:37 UTC, Timoses wrote:

Shouldn't this be 'import output'?
nah, because I didn't import source directly, I import experiment 
so in order to use it, I do source/output.d, which when importing 
module means, source.output
and this '...\\experiment\\source\\'? (I'm not accustomed to 
Windows..)


nope, because I want to do make some sort of a package thing


You could also add a dependency to your other dub project and 
dub should automatically add the import and linker flags.

Add this to your dub.json:

"dependencies":
{
"experiment": {"version": "*", "path": "../experiment"}
}

I didn't test this now, but it should work something like that 
(again, not sure about Windows path...).


See also: 
http://code.dlang.org/package-format?lang=json#version-specs


huh, didn't know I could do that


Re: How to use LLD linker?

2018-07-06 Thread SrMordred via Digitalmars-d-learn
Well, since its VS 2017 installer, eventually I hit all the 
components needed to install it properly. Now its working.

Thanks 0xEAB for the tip about the Windows SDK too :)



Re: How to use LLD linker?

2018-07-06 Thread SrMordred via Digitalmars-d-learn
Delete everything, installed everything again, the installation 
failed to set the proper PATH to MS link.exe, so i put it by hand 
and now get:

fatal error LNK1104: cannot open file 'libcmt.lib'

Frustrating.


Re: how to link self made lib using dub

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

On Friday, 6 July 2018 at 17:08:48 UTC, Flaze07 wrote:

[...]
then, I made a project, with this main in this path : 
Z:\programming\D\experimentLib\source\main.d


it contains this

module main;

import std.stdio;

import source.output;


Shouldn't this be 'import output'?



void main( string[] args ) {
Output.write( "lol" );
readln();
}

and then, I have a dub file in this path : 
Z:\programming\D\experimentLib\dub.json


it contains :

{
  "name" : "experimentlib",
  "targetType": "executable",
  "targetPath": "bin",
  "importPaths": [
"Z:\\programming\\D\\usefulFiles\\experiment\\",


and this '...\\experiment\\source\\'? (I'm not accustomed to 
Windows..)



  ],
  "lflags": [
"+Z:\\programming\\D\\usefulFiles\\experiment\\lib\\",
  ],
}

so, I have made lflags to include the lib in experiment, but 
still, it failed to linker ( it compiles fine, but the linker 
fails )


You could also add a dependency to your other dub project and dub 
should automatically add the import and linker flags.

Add this to your dub.json:

"dependencies":
{
"experiment": {"version": "*", "path": "../experiment"}
}

I didn't test this now, but it should work something like that 
(again, not sure about Windows path...).


See also: 
http://code.dlang.org/package-format?lang=json#version-specs


Re: How to use LLD linker?

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

On Friday, 6 July 2018 at 19:36:05 UTC, 0xEAB wrote:

On Friday, 6 July 2018 at 03:48:04 UTC, SrMordred wrote:
Well I just installed the VS 2017 to try the ldc and get 
(maybe) the same error.


You didn't forget to install the Windows SDK with it, did you?


Yep I forgot xD
It fixed the PATHs but still getting this:

libcmt.lib(chkstk.obj) : fatal error LNK1112: module machine type 
'X86' conflicts with target machine type 'x64'


which i´m trying to solve now.




Memory corruption with COM object

2018-07-06 Thread Rene Zwanenburg via Digitalmars-d-learn
I've been staring at this problem the past few hours without 
making any progress. But I feel like I'm overlooking something 
obvious..


Using Adam's comhelpers, I've made a JSON plugin for LogParser. 
However after running for a bit it'll crash with signs of memory 
corruption.


My guess was the GC was collecting things still in use, and 
disabling the GC does indeed 'fix' the problem. Looking through 
comhelpers, the code doesn't add a GC root for a created object 
before handing it off to the C side. I've added root adding and 
removing, expecting that to fix the problem. However it didn't 
help.


The DLL uses the helper functions like dll_process_attach in 
DllMain, so druntime is initialized.


What would be a good way to figure out why the GC decides to 
collect objects I'm still holding on to?


Re: How to use LLD linker?

2018-07-06 Thread 0xEAB via Digitalmars-d-learn

On Friday, 6 July 2018 at 03:48:04 UTC, SrMordred wrote:
Well I just installed the VS 2017 to try the ldc and get 
(maybe) the same error.


You didn't forget to install the Windows SDK with it, did you?



Re: How to use LLD linker?

2018-07-06 Thread 0xEAB via Digitalmars-d-learn

On Friday, 6 July 2018 at 17:32:09 UTC, SrMordred wrote:

On Friday, 6 July 2018 at 10:55:47 UTC, Rainer Schuetze wrote:



On 06/07/2018 05:48, SrMordred wrote:

[...]


The problem is that the Digital Mars linker is called but the 
Microsoft linker is run, because they share the same name 
link.exe. For dmd/x64/32mscoff or LDC in general the latter is 
expected to be used. Usually dmd and ldc know how to find the 
appropriate one, but dub might just expect it to be found 
through the PATH environment variable.


So, try to set your PATH so that the MS linker is found first.


Yep, this is the way, but i could make it work yet. The PATH 
works, but i get errors relate to libs, even running 
vcvarsall.bat. Thats bizarre since its a fresh intallation of 
VS. It used to work, not sure what happened. Anyway i'll give 
up by now and try another time.


I thought that LDC removed this VS dependecy.


The latest stable release didn't, at least.

Which means, if it cannot locate the Windows SDK, it will call 
`link.exe` and hope to execute something useful, which - as 
already stated before - isn't the case and usually leads to 
OptLink.


Re: How to use LLD linker?

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

On Friday, 6 July 2018 at 10:55:47 UTC, Rainer Schuetze wrote:



On 06/07/2018 05:48, SrMordred wrote:

[...]


The problem is that the Digital Mars linker is called but the 
Microsoft linker is run, because they share the same name 
link.exe. For dmd/x64/32mscoff or LDC in general the latter is 
expected to be used. Usually dmd and ldc know how to find the 
appropriate one, but dub might just expect it to be found 
through the PATH environment variable.


So, try to set your PATH so that the MS linker is found first.


Yep, this is the way, but i could make it work yet. The PATH 
works, but i get errors relate to libs, even running 
vcvarsall.bat. Thats bizarre since its a fresh intallation of VS. 
It used to work, not sure what happened. Anyway i'll give up by 
now and try another time.


I thought that LDC removed this VS dependecy.


how to link self made lib using dub

2018-07-06 Thread Flaze07 via Digitalmars-d-learn
I have been trying to link self made .lib, and have tried to use 
it several times, I failed..
so, here I have a file in this path : 
Z:\programming\D\usefulFiles\experiment\source\output.d


it has

module output;

class Output {
public:
static void write( string msg ) {
import std.stdio;
writeln( msg );
}
}

and then I compiled it into library using dub project with this 
file in a path : Z:\programming\D\usefulFiles\experiment\dub.json


{
  "name" : "experiment",
  "targetType": "library",
  "targetPath": "lib",
  "sourcePaths": [
"source",
  ],
}

then, I made a project, with this main in this path : 
Z:\programming\D\experimentLib\source\main.d


it contains this

module main;

import std.stdio;

import source.output;

void main( string[] args ) {
Output.write( "lol" );
readln();
}

and then, I have a dub file in this path : 
Z:\programming\D\experimentLib\dub.json


it contains :

{
  "name" : "experimentlib",
  "targetType": "executable",
  "targetPath": "bin",
  "importPaths": [
"Z:\\programming\\D\\usefulFiles\\experiment\\",
  ],
  "lflags": [
"+Z:\\programming\\D\\usefulFiles\\experiment\\lib\\",
  ],
}

so, I have made lflags to include the lib in experiment, but 
still, it failed to linker ( it compiles fine, but the linker 
fails )





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: immutable / inout / pure headaches

2018-07-06 Thread Timoses via Digitalmars-d-learn
On Friday, 6 July 2018 at 15:44:28 UTC, Steven Schveighoffer 
wrote:


I'm long overdue for an inout article...

I can point you at my talk from 2016: 
https://www.youtube.com/watch?v=UTz55Lv9FwQ

Thanks, will definitely take a look when I get home.




I never really used 'pure' and just now found a use case with 
immutable [1], i.e. to return unique objects from functions 
which can be assigned to a mutable or immutable reference.
What other "use cases" or reasons to use 'pure' are there 
(aside from compiler optimizations)?


The reason pure functions allow mutability changes is due to 
the nature of what pure means semantically -- you have 
guarantees that nothing else goes in or out, so it's possible 
to deduce what is unique and what is not.


This is powerful to a human reader of a function as well! 
Without seeing the insides, it tells you exactly what it can 
and cannot affect, giving you more understanding of when it can 
be used and when it can't. It helps write safer more tractable 
code, IMO.


In the end, all these attributes are to help reason about large 
code bases without having to read ALL the code.


Sounds like a good idea to always use it whenever possible. For 
me as a kind of novice it takes time to understand the purpose 
and meaning of each of those attributes. I guess I got one step 
closer to understanding "Why pure?".


That leaves @nogc, @safe and @trusted :D. I feel the best way to 
understand these idioms is to experience the "trouble" oneself. I 
knew in some way what pure functions were from the spec, but I 
didn't have an example at hand that made "non-usage" of pure 
painful.


Re: Passing a reference to a returned reference

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

On Friday, 6 July 2018 at 15:51:34 UTC, Michael wrote:
Also, yes, I am using the setter method to play around with the 
precision of the double values, and do some normalising.
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:


class A
{
private static string[] _disallowed = ["damn"];
private string[int] _dict;

// called before and after every member method call
invariant
{
import std.algorithm : any, canFind;
// don't allow _dict to contain any from _disallowed
 assert(!this._dict.byValue()
.any!((entry) => _disallowed.canFind(entry)));
}

@property dict(string[int] dict)
{
 // checks ...
this._dict = dict;
}

void check() {
import std.stdio : writeln;
writeln(this._dict);
}
}

unittest
{
string[int] loc;
auto a = new A();
loc[1] = "hello john";
a.dict = loc; // using the @property setter
loc[1] = "damn";
a.check;
}

What might be safer is using 'opIndexAssign'

void opIndexAssign(string value, int key)
{
import std.algorithm.searching : canFind;
if (!_disallowed.canFind(value))
this._dict[key] = value;
}

and also duping the dictionary that is handed to the Agent

@property dict(string[int] dict)
{
 // checks ...
this._dict = dict.dup;
}

So you can call

string[int] loc;
auto a = new A();
loc[1] = "hello john";
a.dict = loc;
a[1] = "damn";
loc[1] = "damn";
a.check;

without assigning bad values.



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?


I suppose this might already answer your question:
https://forum.dlang.org/post/edrejkakhaylivlqj...@forum.dlang.org



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 Timoses via Digitalmars-d-learn

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.


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: immutable / inout / pure headaches

2018-07-06 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/6/18 11:22 AM, Timoses wrote:

On Friday, 6 July 2018 at 14:28:39 UTC, Steven Schveighoffer wrote:
inout is not a compile-time wildcard, it's a runtime one. So it 
doesn't know how to convert an immutable to an inout. Essentially, 
inside this function, the compiler has no idea whether the real thing 
is an immutable, const, mutable, etc.


The fix is simple, replace both your constructors with one inout 
constructor:


this(inout(S) t) inout { this.s = t; }


Slowly getting acquainted to inout... Feels like magic.


I'm long overdue for an inout article...

I can point you at my talk from 2016: 
https://www.youtube.com/watch?v=UTz55Lv9FwQ


"viral" is very fitting. Throw in pure and I quickly reach the bottom of 
my program hitting a library function I used which is not pure.


I never really used 'pure' and just now found a use case with immutable 
[1], i.e. to return unique objects from functions which can be assigned 
to a mutable or immutable reference.
What other "use cases" or reasons to use 'pure' are there (aside from 
compiler optimizations)?


The reason pure functions allow mutability changes is due to the nature 
of what pure means semantically -- you have guarantees that nothing else 
goes in or out, so it's possible to deduce what is unique and what is not.


This is powerful to a human reader of a function as well! Without seeing 
the insides, it tells you exactly what it can and cannot affect, giving 
you more understanding of when it can be used and when it can't. It 
helps write safer more tractable code, IMO.


In the end, all these attributes are to help reason about large code 
bases without having to read ALL the code.


-Steve


Re: Passing a reference to a returned reference

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

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.


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: immutable / inout / pure headaches

2018-07-06 Thread Timoses via Digitalmars-d-learn
On Friday, 6 July 2018 at 14:28:39 UTC, Steven Schveighoffer 
wrote:
inout is not a compile-time wildcard, it's a runtime one. So it 
doesn't know how to convert an immutable to an inout. 
Essentially, inside this function, the compiler has no idea 
whether the real thing is an immutable, const, mutable, etc.


The fix is simple, replace both your constructors with one 
inout constructor:


this(inout(S) t) inout { this.s = t; }


Slowly getting acquainted to inout... Feels like magic.


And it will work for everything.

One word of caution though -- inout is viral (just like 
immutable). Everything you use has to support it, or it breaks 
down.


"viral" is very fitting. Throw in pure and I quickly reach the 
bottom of my program hitting a library function I used which is 
not pure.


I never really used 'pure' and just now found a use case with 
immutable [1], i.e. to return unique objects from functions which 
can be assigned to a mutable or immutable reference.
What other "use cases" or reasons to use 'pure' are there (aside 
from compiler optimizations)?


[1]: 
https://forum.dlang.org/post/nmcnuenazaghjlxod...@forum.dlang.org


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 Ali Çehreli via Digitalmars-d-learn

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



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.


Re: immutable / inout / pure headaches

2018-07-06 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/6/18 7:10 AM, Timoses wrote:
I dared once again getting into immutable by adding an "immutable" 
keyword which causes a chain of actions to be taken.
I feel like I'm lost in a jungle of immutable, inout and pure (perhaps 
more will join the party...).


To start off, why does this not work?


 class Test
 {
     private S s;
     this(S t) { this.s = t; }
     this(immutable S t) immutable { this.s = t; }

     inout(Test) get() inout
     {
     // Error: none of the overloads of __ctor are callable 
using a inout object, candidates are:

//onlineapp.d(10):    onlineapp.Test.this(S t)
//onlineapp.d(11):    onlineapp.Test.this(immutable(S) t)
  return new inout Test(this.s);
     }


inout is not a compile-time wildcard, it's a runtime one. So it doesn't 
know how to convert an immutable to an inout. Essentially, inside this 
function, the compiler has no idea whether the real thing is an 
immutable, const, mutable, etc.


The fix is simple, replace both your constructors with one inout 
constructor:


this(inout(S) t) inout { this.s = t; }

And it will work for everything.

One word of caution though -- inout is viral (just like immutable). 
Everything you use has to support it, or it breaks down.


-Steve


Re: Passing a reference to a returned reference

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

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


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: Static member function returning immutable slice; compiler error: without this cannot be immutable

2018-07-06 Thread Ivo Maffei via Digitalmars-d-learn

Ok thanks to everyone for their help.

So I tried what you suggested and so the problem was bracketing.

For future reference, the above code can be fixed into this:

class Foo {
private static Foo[] fooSlice; //private static slice

	static const(Foo[]) getFooList() { //static method returning a 
constant slice

return fooSlice;
}   
}




Re: immutable / inout / pure headaches

2018-07-06 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, July 06, 2018 11:10:27 Timoses via Digitalmars-d-learn wrote:
> I dared once again getting into immutable by adding an
> "immutable" keyword which causes a chain of actions to be taken.
> I feel like I'm lost in a jungle of immutable, inout and pure
> (perhaps more will join the party...).
>
> To start off, why does this not work?
>
>
>   class Test
>   {
>   private S s;
>   this(S t) { this.s = t; }
>   this(immutable S t) immutable { this.s = t; }
>
>  inout(Test) get() inout
>  {
>  // Error: none of the overloads of __ctor are
> callable using a inout object, candidates are:
> //onlineapp.d(10):onlineapp.Test.this(S t)
> //onlineapp.d(11):onlineapp.Test.this(immutable(S) t)
>   return new inout Test(this.s);
>  }
>   }
>
>   struct S
>   {
>   int[] a;
>   }
>   void main()
>   {
>   immutable S s = immutable S([1,2,3]);
>   auto t = new immutable Test(s);
>   }

You have no constructor that will work with inout - only mutable amd
immutable. inout is only going to work when the object is always treated as
either inout or const, because it could be an object that's mutable, const,
or immutable. It can't ever treat it as mutable or immutable within the
function that marks it as inout.

- Jonathan M Davis



immutable / inout / pure headaches

2018-07-06 Thread Timoses via Digitalmars-d-learn
I dared once again getting into immutable by adding an 
"immutable" keyword which causes a chain of actions to be taken.
I feel like I'm lost in a jungle of immutable, inout and pure 
(perhaps more will join the party...).


To start off, why does this not work?


class Test
{
private S s;
this(S t) { this.s = t; }
this(immutable S t) immutable { this.s = t; }

inout(Test) get() inout
{
// Error: none of the overloads of __ctor are 
callable using a inout object, candidates are:

//onlineapp.d(10):onlineapp.Test.this(S t)
//onlineapp.d(11):onlineapp.Test.this(immutable(S) t)
return new inout Test(this.s);
}
}

struct S
{
int[] a;
}
void main()
{
immutable S s = immutable S([1,2,3]);
auto t = new immutable Test(s);
}



Re: How to use LLD linker?

2018-07-06 Thread Rainer Schuetze via Digitalmars-d-learn




On 06/07/2018 05:48, SrMordred wrote:

On Saturday, 30 June 2018 at 10:48:49 UTC, Suliman wrote:
Correct me if I am wrong, but I have read news that dmd now can be 
used without C++ Build Tools.


I trying to build simple project. And getting Error:

Warning: no Visual C++ installation detected
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
OPTLINK : Error 8: Illegal Filename
...

Error: C:\D\dmd2\windows\bin\link.exe failed with status: 1
ldc2 failed with exit code 1.

Same with dmd.

How to use LLD linker?


Well I just installed the VS 2017 to try the ldc and get (maybe) the 
same error.


dub run --config=application --arch=x86_64 --build=debug --compiler=ldc2
Performing "debug" build using ldc2 for x86_64.
lib ~master: building configuration "application"...
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
OPTLINK : Error 8: Illegal Filename
/NOLOGO /DEBUG /OPT:REF /OPT:ICF /DEFAULTLIB:libcmt 
/DEFAULTLIB:libvcruntime 
"/OUT:.dub\build\application-debug-windows-x86_64-ldc_2081-FC0CCB721F0C7E0D58B93FB1E50E3401\lib.exe" 
".dub\obj\lib.obj" "d:\ldc2\lib\ldc_rt.builtins.lib" 
/LIBPATH:d:/ldc2/bin/../lib phobos2-ldc.lib druntime-ldc.lib 
kernel32.lib user32.lib gdi32.lib
winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib 
advapi32.lib


     ^
Error: d:\D\dmd2\windows\bin\link.exe failed with status: 1
ldc2 failed with exit code 1.



The problem is that the Digital Mars linker is called but the Microsoft 
linker is run, because they share the same name link.exe. For 
dmd/x64/32mscoff or LDC in general the latter is expected to be used. 
Usually dmd and ldc know how to find the appropriate one, but dub might 
just expect it to be found through the PATH environment variable.


So, try to set your PATH so that the MS linker is found first.


Re: Function Template for Dynamic Parameter

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

On Thursday, 5 July 2018 at 16:23:36 UTC, vino.B wrote:

Hi All,

  Request your help on the below code

auto coCleanFiles(T ...) (T FFs) {
auto dFiles = Array!(Tuple!(string, 
SysTime))(dirEntries(FFs, SpanMode.depth).map!(a => 
tuple(a.name, a.timeCreated)));

return dFiles;
}

void process(T)(T pfunction, Array!string Dirlst) {
alias wlsType = typeof(pfunction(T));
auto Result = taskPool.workerLocalStorage!wlsType();
foreach (FFs; parallel(Dirlst[],1)) { Result.get ~= 
pfunction(FFs); }

foreach(i; Result.toRange) { writeln(i[][]); }
}

void main() {
Array!string Cleanlst;
Cleanlst.insert("C:\\Temp\\BACKUP1");
process(, Cleanlst);
}

Error : Error: coCleanFiles(T...)(T FFs) is not an lvalue and 
cannot be modified


I guess since in above code `coCleanFiles` is a template, you can 
not simply take the address of that function template. You'd have 
to instantiate the template first


process(!string, ...)

If you want to pass the template to process you could define 
process as something like


void process(alias func)(Array!string Dirlst)
{
func!string(Dirlst[0]);
}

and call

process!coCleanFiles(Cleanlst);



Re: How to use LLD linker?

2018-07-06 Thread Suliman via Digitalmars-d-learn
Well I just installed the VS 2017 to try the ldc and get 
(maybe) the same error.


dub run --config=application --arch=x86_64 --build=debug 
--compiler=ldc2

Performing "debug" build using ldc2 for x86_64.
lib ~master: building configuration "application"...
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
OPTLINK : Error 8: Illegal Filename
/NOLOGO /DEBUG /OPT:REF /OPT:ICF /DEFAULTLIB:libcmt 
/DEFAULTLIB:libvcruntime 
"/OUT:.dub\build\application-debug-windows-x86_64-ldc_2081-FC0CCB721F0C7E0D58B93FB1E50E3401\lib.exe" ".dub\obj\lib.obj" "d:\ldc2\lib\ldc_rt.builtins.lib" /LIBPATH:d:/ldc2/bin/../lib phobos2-ldc.lib druntime-ldc.lib kernel32.lib user32.lib gdi32.lib
winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib 
comdlg32.lib advapi32.lib



^
Error: d:\D\dmd2\windows\bin\link.exe failed with status: 1
ldc2 failed with exit code 1.



Could you please try:
dub build --force

And write here if it's help