Re: Debugging D shared libraries

2015-09-22 Thread Johannes Pfau via Digitalmars-d-learn
Am Tue, 22 Sep 2015 14:40:43 +
schrieb John Colvin :

> On Tuesday, 22 September 2015 at 14:37:11 UTC, Russel Winder 
> wrote:
> > On Sun, 2015-09-20 at 17:47 +0200, Johannes Pfau via 
> > Digitalmars-d -learn wrote:
> >> [...]
> > […]
> >> [...]
> >
> > Debian Jessie is far too out of date to be useful. I'm on 
> > Debian Sid
> > (still quite old), and Fedora Rawhide (not quite so old).
> >
> > Sadly GDC on Debian Sid tells me 5.2.1 20150911 which may well 
> > tell Iain which DMD is being used, but I haven't a clue. :-)
> >
> > […]
> >
> > The real problem using GDC is:
> >
> > gdc -I. -O3 -fPIC -c -o processAll_library_d.o 
> > processAll_library_d.d
> > /usr/include/d/core/stdc/config.d:28:3: error: static if 
> > conditional
> > cannot be at global scope
> >static if( (void*).sizeof > int.sizeof )
> >^
> >
> > I haven't had chance to sit down and see if this is reasonable 
> > or not.
> 
> seeing as it's an error in core.stdc.config, I'd say it's 
> definitely not reasonable

It's indeed strange.
 
@Russel Winder if you can reproduce this with the latest GDC* please
file a bug report. Even the error message doesn't make sense: static if
works fine at global scope, AFAIK.

* you could use the latest binaries from http://gdcproject.org/downloads



Re: Template detection

2015-09-22 Thread Meta via Digitalmars-d-learn
On Tuesday, 22 September 2015 at 16:11:59 UTC, Alexandru Ermicioi 
wrote:

Thx. Didn't know that there is such trait available.

On page http://dlang.org/traits.html it's not present.


Yeah, it's undocumented for some reason. Somebody must've forgot 
to make a corresponding doc pull request I suppose.


Re: Template detection

2015-09-22 Thread Alexandru Ermicioi via Digitalmars-d-learn

On Tuesday, 22 September 2015 at 15:37:32 UTC, Meta wrote:
On Sunday, 13 September 2015 at 08:26:55 UTC, Alexandru 
Ermicioi wrote:

Hello,

Given:

class SomeClass {

public {
void someSimpleMethod() {}

template setOfTemplatedMethods(Type) {
void templatedMethodOne() {}
void templatedMethodTwo() {}
}
}
}

Is there a way to detect at compile time if a member of 
class/struct is a template? (in the example above, if 
setOfTemplatedMethods is a template).


You can use __traits(isTemplate, ).


Thx. Didn't know that there is such trait available.

On page http://dlang.org/traits.html it's not present.


BidirectionalRange switching direction

2015-09-22 Thread Tofu Ninja via Digitalmars-d-learn
Trying to implement a bi directional range and it is slightly 
unclear what the semantics are supposed to be and just wanted 
some clarification.


Are bidirectional ranges supposed to be able to support switching 
direction mid iteration? Like if I do popFront popFront popBack 
should that be equal to just a single popFront? Or once you start 
on a direction should switching be considered an error? Or is 
popFront and popBack supposed to consume from both ends of the 
range and the range is empty when they meet?


-tofu



How to setup mono-D for shared libraries?

2015-09-22 Thread Jacob via Digitalmars-d-learn
How do I setup mono-D for creating shared libraries and including 
them into other projects? When I drag the .d files to create the 
library from, which is not my own, I get undefined references. I 
have the lib files, which are a bunch of separate libs, that I 
want to include into one big lib. Once that's done I want to 
include that lib into another project.


I'd rather not modify si.ini. Are there any tutorials for getting 
started with Mono-D? (setup, not coding)




Re: Unexpected behavior when casting away immutable

2015-09-22 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 03:50:44 UTC, Vladimir 
Panteleev wrote:
On Wednesday, 23 September 2015 at 03:39:02 UTC, Mike Parker 
wrote:

```
immutable int x = 10;
int* px = cast(int*)
*px = 9;
writeln(x);
```

It prints 10, where I expected 9. This is on Windows. I'm 
curious if anyone knows why it happens.


Essentially, because x is immutable, the compiler optimizes 
writeln(x) to writeln(10). This seems to happen even without -O.


I see. Thanks.


Re: Unexpected behavior when casting away immutable

2015-09-22 Thread John Colvin via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 03:39:02 UTC, Mike Parker 
wrote:
I have a situation where I would like to demonstrate violating 
the contract of immutable (as an example of what not to do), 
but do so without using structs or classes, just basic types 
and pointers. The following snippet works as I would expect:


```
immutable int i = 10;
immutable(int*) pi = 
int** ppi = cast(int**)
writeln(*ppi);
int j = 9;
*ppi = 
writeln(*ppi);
```

Two different addresses are printed, so I've successfully 
violated the contract of immutable and changed the value of pi, 
an immutable pointer. However, this does not work as I expect.


```
immutable int x = 10;
int* px = cast(int*)
*px = 9;
writeln(x);
```

It prints 10, where I expected 9. This is on Windows. I'm 
curious if anyone knows why it happens.


violating immutable is undefined behaviour, so the compiler is 
technically speaking free to assume it never happens. At the very 
least, neither snippet's result is guaranteed to show a change or 
not. At the most, literally anything can happen.


Re: BidirectionalRange switching direction

2015-09-22 Thread John Colvin via Digitalmars-d-learn

On Wednesday, 23 September 2015 at 02:10:22 UTC, Tofu Ninja wrote:
Trying to implement a bi directional range and it is slightly 
unclear what the semantics are supposed to be and just wanted 
some clarification.


Are bidirectional ranges supposed to be able to support 
switching direction mid iteration? Like if I do popFront 
popFront popBack should that be equal to just a single 
popFront? Or once you start on a direction should switching be 
considered an error? Or is popFront and popBack supposed to 
consume from both ends of the range and the range is empty when 
they meet?


-tofu


The last one. E.g. for arrays (except narrow strings... ugh)

auto popFront(T)(ref T[] a)
{
a = a[1 .. $];
}

auto popBack(T)(ref T[] a)
{
a = a[0 .. $-1];
}


Re: Unexpected behavior when casting away immutable

2015-09-22 Thread Vladimir Panteleev via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 03:39:02 UTC, Mike Parker 
wrote:

```
immutable int x = 10;
int* px = cast(int*)
*px = 9;
writeln(x);
```

It prints 10, where I expected 9. This is on Windows. I'm 
curious if anyone knows why it happens.


Essentially, because x is immutable, the compiler optimizes 
writeln(x) to writeln(10). This seems to happen even without -O.




Unexpected behavior when casting away immutable

2015-09-22 Thread Mike Parker via Digitalmars-d-learn
I have a situation where I would like to demonstrate violating 
the contract of immutable (as an example of what not to do), but 
do so without using structs or classes, just basic types and 
pointers. The following snippet works as I would expect:


```
immutable int i = 10;
immutable(int*) pi = 
int** ppi = cast(int**)
writeln(*ppi);
int j = 9;
*ppi = 
writeln(*ppi);
```

Two different addresses are printed, so I've successfully 
violated the contract of immutable and changed the value of pi, 
an immutable pointer. However, this does not work as I expect.


```
immutable int x = 10;
int* px = cast(int*)
*px = 9;
writeln(x);
```

It prints 10, where I expected 9. This is on Windows. I'm curious 
if anyone knows why it happens.


Re: How to setup mono-D for shared libraries?

2015-09-22 Thread Rikki Cattermole via Digitalmars-d-learn

On 23/09/15 8:20 AM, Jacob wrote:

How do I setup mono-D for creating shared libraries and including them
into other projects? When I drag the .d files to create the library
from, which is not my own, I get undefined references. I have the lib
files, which are a bunch of separate libs, that I want to include into
one big lib. Once that's done I want to include that lib into another
project.

I'd rather not modify si.ini. Are there any tutorials for getting
started with Mono-D? (setup, not coding)


Well you could go the route of dub, which configuration files can be 
loaded directly into it as a project.




Re: Cameleon: Stricter Alternative Implementation of VariantN

2015-09-22 Thread Kagamin via Digitalmars-d-learn

On Monday, 21 September 2015 at 13:42:14 UTC, Nordlöw wrote:

Questions:

- Is the logic of opAssign and get ok for string?
- How does the inner workings of the GC harmonize with my calls 
to `memcpy` in `opAssign()` here


https://github.com/nordlow/justd/blob/master/cameleon.d#L80


That line won't compile, so don't be afraid of its behavior.

- Do I have to call some GC-logic in order to make the GC aware 
of the new string-reference in `opAssign`?


Make sure your struct is always sizeof(void*)-aligned.


Has anyone created a D wrapper for wbemuuid.lib

2015-09-22 Thread Thomas Mader via Digitalmars-d-learn
I looked at some of the windows API wrapper projects for D on 
github [1][2], but none of them seems to have wrapped 
wbemuuid.lib right now.

Do I miss something? Does anyone have wrapped this lib?

Thomas

[1] http://code.dlang.org/packages/windows-headers
[2] https://github.com/smjgordon/bindings/tree/master/win32


Re: Has anyone created a D wrapper for wbemuuid.lib

2015-09-22 Thread Thomas Mader via Digitalmars-d-learn

On Tuesday, 22 September 2015 at 10:03:52 UTC, Thomas Mader wrote:

Do I miss something? Does anyone have wrapped this lib?


This was also asked on stackoverflow some time ago. [1]
Wonder if something happend since then.

[1] 
http://stackoverflow.com/questions/24051606/can-i-use-routines-from-comdef-h-wbemidl-h-etc-in-d




Re: Do users need to install VS runtime redistributable if linking with Microsoft linker?

2015-09-22 Thread thedeemon via Digitalmars-d-learn

On Monday, 21 September 2015 at 15:00:24 UTC, ponce wrote:

All in the title.

DMD 64-bit links with the VS linker.
Do users need to install the VS redistributable libraries?


I think they don't.
Generated .exe seems to depend only on kernel32.dll and 
shell32.dll, i.e. things users already have.


Re: Has anyone created a D wrapper for wbemuuid.lib

2015-09-22 Thread Kagamin via Digitalmars-d-learn
The bindings are translated from mingw headers, and mingw doesn't 
supply libraries with precompiled GUIDs, only functions. But 
bindings for GUIDs are pretty simple:


extern extern(System) CLSID CLSID_SWbemLocator;

Just copy the names you want.


Re: Template detection

2015-09-22 Thread Alexandru Ermicioi via Digitalmars-d-learn

On Sunday, 13 September 2015 at 09:29:13 UTC, Enamex wrote:
On Sunday, 13 September 2015 at 08:26:55 UTC, Alexandru 
Ermicioi wrote:

Hello,

Given:

class SomeClass {

public {
void someSimpleMethod() {}

template setOfTemplatedMethods(Type) {
void templatedMethodOne() {}
void templatedMethodTwo() {}
}
}
}

Is there a way to detect at compile time if a member of 
class/struct is a template? (in the example above, if 
setOfTemplatedMethods is a template).


There's this: 
http://stackoverflow.com/questions/5483381/test-if-an-alias-is-a-template-in-d-2-0


and it should work with 'member templates' OK.


Thx for the provided link.

I've tried to implement version from the stackoverflow answer, 
and run against tests provided in another answer to the question 
in link, and failed.


With dmd version 2.067.1 this implementation fails at
static assert(! isTemplate!(FooS!int.func!float) );
static assert(! isTemplate!(FooS!int.bar) );
for .func it requires "this" (an value of type FooS, or 
isTemplate fails to instantiate).

and for .bar it just fails (isTemplate detects it as a template).

I've found another way to detect if a symbol is a template.
The idea is that for a not instantiated template you can get with 
allMembers trait the list of members in template, but you can't 
access them using getMembers trait since the template is not 
instantiated yet.
So if there is a symbol that has members which cannot be accessed 
using getMember (generates an error) then this is a template!


template allMembers(alias Type) {
alias allMembers = TypeTuple!(__traits(allMembers, Type));
}

template anyAccessible(alias Container, T...) {
static if (T.length > 1) {
enum bool anyAccessible = anyAccessible!(Container, T[0 
.. $ / 2]) || anyAccessible!(Container, T[$ / 2 .. $]);

} else static if (T.length == 1) {
enum bool anyAccessible = __traits(compiles, 
getMember!(Container, T[0]));

} else {
enum bool anyAccessible = false;
}
}

template isTemplate(alias T) {
static if ( // check if symbol has members
__traits(compiles, TypeTuple!(__traits(allMembers, T)))
&& !T.stringof.startsWith("module ", "package ")
) {
enum bool isTemplate =
(allMembers!(T).length > 0)
&& !anyAccessible!(T, allMembers!T);
} else {
enum bool isTemplate = false;
}
}

It passes almost all tests except for:
static assert(! isTemplate!((int x){return x;}) );
static assert(! isTemplate!(std) );
static assert(! isTemplate!(core) );

I don't know how but in this cases, compiler attempts to parse 
true branch of static if, even if the expression in static if is 
false.
For lamda function allMembers trait fails to get the members and 
an error is created by compiler.
For std, and core packages isTemplate evaluates to true even if 
expression from static if is false.


Is such behavior of static if correct (tested also with ldc 
compiler, same results)?


If there are other test cases, would be glad if they are posted 
here (to improve the accuracy of template detection).


Tests are:

struct FooS(T) {
struct Inner {}
struct Inner2(string U="!(") {}
int func(U)() { return 0; }
int bar;
}
FooS!int foo;

class FooC { int x; }
union FooU { int x;}
enum FooE { x }
interface FooI { int x(); }

template FooT(T) {
struct Inner {}
struct Inner2(string U="!(") {}
int func(U)() { return 0; }
int bar;
}

static assert(! isTemplate!0 );
static assert(! isTemplate!"0" );
static assert(! isTemplate!0.0f );
static assert(! isTemplate!'0' );
static assert(! isTemplate!'!' );
static assert(! isTemplate!"module std.stdio" );
static assert(! isTemplate!null );
static assert(! isTemplate!true );
static assert(! isTemplate!__FILE__ );
static assert(! isTemplate!__LINE__ );
static assert(! isTemplate!([]) );
static assert(  isTemplate!FooS );
static assert(! isTemplate!(FooS!int) );
static assert(  isTemplate!(FooS!int.func) );
static assert(! isTemplate!(FooS!int.func!float) );
static assert(! isTemplate!(FooS!int.bar) );
static assert(! isTemplate!(FooS!int.Inner) );
static assert(  isTemplate!(FooS!int.Inner2) );
static assert(! isTemplate!(FooS!int.Inner2!"?") );
static assert(  isTemplate!FooT );
static assert(! isTemplate!(FooT!int) );
static assert(  isTemplate!(FooT!int.func) );
static assert(! isTemplate!(FooT!int.func!float) );
static assert(! isTemplate!(FooT!int.bar) );
static assert(! isTemplate!(FooT!int.Inner) );
static assert(  isTemplate!(FooT!int.Inner2) );
static assert(! isTemplate!(FooT!int.Inner2!"?") );
static assert(! isTemplate!foo );
static assert(  isTemplate!(foo.func) );
static assert(  isTemplate!isTemplate );
static assert(! isTemplate!(isTemplate!isTemplate) );
static assert(! isTemplate!FooC );
static assert(! isTemplate!FooU );
static assert(! isTemplate!FooE );
static assert(! isTemplate!FooI );
static assert(! isTemplate!((int 

Re: Debugging D shared libraries

2015-09-22 Thread Russel Winder via Digitalmars-d-learn
On Sun, 2015-09-20 at 17:49 +0200, Johannes Pfau via Digitalmars-d
-learn wrote:
> […]
> 
> Just realized this thread is titled "Debugging D shared libraries" ;
> -)
> GDC does not yet support shared libraries.

Conversely I thought it did due to the GCC toolchain thingy.

I'm using DMD and LDC just now so I have no actual data.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part


Re: Debugging D shared libraries

2015-09-22 Thread Russel Winder via Digitalmars-d-learn
On Sun, 2015-09-20 at 07:49 +, rom via Digitalmars-d-learn wrote:
> 
[…]
> > works entirely fine. However the "parallel" code:
> > 
> > extern(C)
> > double parallel(const int n, const double delta) {
> >   Runtime.initialize();
> >   const pi = 4.0 * delta * taskPool.reduce!"a + b"(
> >   map!((int i){ immutable x = (i - 0.5) * delta; return 
> > 1.0 / (1.0 + x * x); })(iota(1, n + 1)));
> >   Runtime.terminate();
> >   return pi;
> > }
> > 
[…]
> 
> Isn't it simply that when doing some work asynchronously, as with 
> task pool, work is being done in other threads than the main. All 
> the while, you're killing the Runtime. The fact that when 
> removing  Runtime.terminate makes the code work might support 
> that explanation.

I am not sure that can be the case per se as reduce only uses threads
internally, the task pool operations should be complete by the time pi
is initialized. On the other hand there might be a lock contention
between a quiescent default task pool and the terminate code.

The code is now moved on a bit from the above, and it works sufficient
for the task at hand. This isn't production code so it doesn't have to
be quite as good and correct as maybe it should be.
 
-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part


Re: Debugging D shared libraries

2015-09-22 Thread John Colvin via Digitalmars-d-learn
On Tuesday, 22 September 2015 at 14:37:11 UTC, Russel Winder 
wrote:
On Sun, 2015-09-20 at 17:47 +0200, Johannes Pfau via 
Digitalmars-d -learn wrote:

[...]

[…]

[...]


Debian Jessie is far too out of date to be useful. I'm on 
Debian Sid

(still quite old), and Fedora Rawhide (not quite so old).

Sadly GDC on Debian Sid tells me 5.2.1 20150911 which may well 
tell Iain which DMD is being used, but I haven't a clue. :-)


[…]

The real problem using GDC is:

gdc -I. -O3 -fPIC -c -o processAll_library_d.o 
processAll_library_d.d
/usr/include/d/core/stdc/config.d:28:3: error: static if 
conditional

cannot be at global scope
   static if( (void*).sizeof > int.sizeof )
   ^

I haven't had chance to sit down and see if this is reasonable 
or not.


seeing as it's an error in core.stdc.config, I'd say it's 
definitely not reasonable


Re: foreach automoatic counter?

2015-09-22 Thread Steven Schveighoffer via Digitalmars-d-learn

On 9/21/15 6:49 PM, Jonathan M Davis via Digitalmars-d-learn wrote:

In general though, if you want a counter for the
range that you're indexing, then you can use lockstep to wrap the range, and
then when you use it in foreach, you get the count and the element:

http://dlang.org/phobos/std_range.html#.lockstep


As Justin Whear pointed out, enumerate is better when you have one range.

-Steve


Re: Cameleon: Stricter Alternative Implementation of VariantN

2015-09-22 Thread Kagamin via Digitalmars-d-learn
You can generate a union from allowed types, it will make copies 
type safe too, sort of set!(staticIndexOf(T, 
AllowedTypes))(rhs)... hmm... can it be an overload?


Re: Do users need to install VS runtime redistributable if linking with Microsoft linker?

2015-09-22 Thread ponce via Digitalmars-d-learn

On Tuesday, 22 September 2015 at 09:38:12 UTC, thedeemon wrote:

I think they don't.
Generated .exe seems to depend only on kernel32.dll and 
shell32.dll, i.e. things users already have.


Great then.


Re: Cameleon: Stricter Alternative Implementation of VariantN

2015-09-22 Thread Per Nordlöw via Digitalmars-d-learn

On Tuesday, 22 September 2015 at 12:47:31 UTC, Per Nordlöw wrote:
How does the GC know if the data block contains a reference 
(pointer) or value type? Does it try to follow that pointer 
either way!?


If so when does this memory scanning occurr?


Re: Cameleon: Stricter Alternative Implementation of VariantN

2015-09-22 Thread Per Nordlöw via Digitalmars-d-learn

On Tuesday, 22 September 2015 at 09:57:58 UTC, Kagamin wrote:

Make sure your struct is always sizeof(void*)-aligned.


Could that be automatically enforced somehow based on the 
contents of AllowedTypes?


Re: Cameleon: Stricter Alternative Implementation of VariantN

2015-09-22 Thread Per Nordlöw via Digitalmars-d-learn

On Tuesday, 22 September 2015 at 09:57:58 UTC, Kagamin wrote:
- Do I have to call some GC-logic in order to make the GC 
aware of the new string-reference in `opAssign`?


Make sure your struct is always sizeof(void*)-aligned.


How does the GC know if the data block contains a reference 
(pointer) or value type? Does it try to follow that pointer 
either way!?


Re: Debugging D shared libraries

2015-09-22 Thread Russel Winder via Digitalmars-d-learn
On Sun, 2015-09-20 at 17:47 +0200, Johannes Pfau via Digitalmars-d
-learn wrote:
> Am Sat, 19 Sep 2015 17:41:41 +0100
> 
[…]
> 
> Have you tried using a newer GDC version? The debian jessie version
> probably uses the 2.064.2 frontend.

Debian Jessie is far too out of date to be useful. I'm on Debian Sid
(still quite old), and Fedora Rawhide (not quite so old).

Sadly GDC on Debian Sid tells me 5.2.1 20150911 which may well tell
Iain which DMD is being used, but I haven't a clue. :-)

[…]

The real problem using GDC is:

gdc -I. -O3 -fPIC -c -o processAll_library_d.o processAll_library_d.d
/usr/include/d/core/stdc/config.d:28:3: error: static if conditional
cannot be at global scope
   static if( (void*).sizeof > int.sizeof )
   ^

I haven't had chance to sit down and see if this is reasonable or not.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part


Re: Cameleon: Stricter Alternative Implementation of VariantN

2015-09-22 Thread Nordlöw via Digitalmars-d-learn

On Tuesday, 22 September 2015 at 12:54:47 UTC, Kagamin wrote:
You can generate a union from allowed types, it will make 
copies type safe too, sort of set!(staticIndexOf(T, 
AllowedTypes))(rhs)... hmm... can it be an overload?


I believe there is a trait somewhere that figures out the maximum 
alignment needed for a set/union of types. I think Walter spoke 
about in a DConf lecture some time ago.


Re: Template detection

2015-09-22 Thread Meta via Digitalmars-d-learn
On Sunday, 13 September 2015 at 08:26:55 UTC, Alexandru Ermicioi 
wrote:

Hello,

Given:

class SomeClass {

public {
void someSimpleMethod() {}

template setOfTemplatedMethods(Type) {
void templatedMethodOne() {}
void templatedMethodTwo() {}
}
}
}

Is there a way to detect at compile time if a member of 
class/struct is a template? (in the example above, if 
setOfTemplatedMethods is a template).


You can use __traits(isTemplate, ).