Re: Unexpected behavior when casting away immutable

2015-09-23 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 23 September 2015 at 11:38:38 UTC, Mafi wrote:
On Wednesday, 23 September 2015 at 05:24:05 UTC, John Colvin 
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.


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.


In essence, this code snippet is even better than the OP 
expected in showing why you shouldn't cast away immutable.


Yes, that's true. I think using both snippets drives the point 
home pretty well.


dis...@dlang.org

2015-09-23 Thread Aidan via Digitalmars-d-learn
I am just starting to look into D and i have to say I am loving 
it at the moment. But I have ran into an issue that i can't seem 
to find any libraries for Api hooking.


If anyone knows of a well documented source for this it would be 
much appreciated.


Re: Unexpected behavior when casting away immutable

2015-09-23 Thread Mafi via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 05:24:05 UTC, John Colvin 
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.


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.


In essence, this code snippet is even better than the OP expected 
in showing why you shouldn't cast away immutable.


Re: Unexpected behavior when casting away immutable

2015-09-23 Thread Dicebot via Digitalmars-d-learn

On Wednesday, 23 September 2015 at 14:34:07 UTC, bachmeier wrote:
I was not aware that you could "violate" immutable. In that 
case, it's not immutable.


You can violate absolutely everything in a system language with 
casts and pointers. That is exactly what makes it system 
language. But you do so only by intentionally and explicitly 
abandoning type system and domain of well-defined behaviour. 
Compiler could as well decide to put it in RO section of 
executable resulting in app crash at runtime (that happens with 
string literals on Linux). Any cast means "I know what I am doing 
and I am fully prepared to get burned".


Re: Unexpected behavior when casting away immutable

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

On Wednesday, 23 September 2015 at 14:34:07 UTC, bachmeier wrote:
On Wednesday, 23 September 2015 at 05:24:05 UTC, John Colvin 
wrote:


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.


I was not aware that you could "violate" immutable. In that 
case, it's not immutable.


immutable is guaranteed to be enforced at the type-system level. 
If you deliberately break the type system and tell the compiler 
to modify data that in actual fact is immutable, then that is 
undefined behaviour. If you're lucky, the data could have some 
protection such that writing to it will trigger a fault at the 
hardware level, but that's definitely *not* guaranteed.


Re: scope in function argument

2015-09-23 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 23 September 2015 at 17:09:40 UTC, Freddy wrote:

What does it mean when there is a scope in a function argument.


That you are not supposed to let that reference escape the 
function scope. The compiler does little verification of this 
right now, but may optimize on that assumption (notably, scope 
delegates will not be copied to the heap right now).


In the future, it may become an error to allow them to escape.

int* b;

void func(scope int* a) {
   b = a; // you made a escape the function scope, undefined 
behavior results

}



scope in function argument

2015-09-23 Thread Freddy via Digitalmars-d-learn

What does it mean when there is a scope in a function argument.
---
void func(scope int* a){}
---


Re: scope in function argument

2015-09-23 Thread Ali Çehreli via Digitalmars-d-learn

On 09/23/2015 10:11 AM, Adam D. Ruppe wrote:
> On Wednesday, 23 September 2015 at 17:09:40 UTC, Freddy wrote:
>> What does it mean when there is a scope in a function argument.
>
> That you are not supposed to let that reference escape the function
> scope.

Just to complete with a related feature, here are also 'return' 
parameters, which make the compiler ensure that the returned reference 
lives longer than the 'ref' argument. Unfortunately, I can't see that 
feature here anymore:


  http://dlang.org/function.html#parameters

Has it been pulled back? It still works in 2.068. I had written about it:


http://ddili.org/ders/d.en/function_parameters.html#ix_function_parameters.return,%20parameter

Then there is the -dip25 compiler switch that forces the programmer to 
put 'return' on every returned 'ref' parameter, effectively enforcing 
that check:


  http://wiki.dlang.org/DIP25

Ali



Re: Dub package with C code

2015-09-23 Thread Sebastiaan Koppe via Digitalmars-d-learn
On Thursday, 24 September 2015 at 04:17:14 UTC, Rikki Cattermole 
wrote:

Is libxlsxwriter available in the systems package manager?


Pacman says no.


Let e.g. Windows users figure theirs out.


libxlsxwriter is not supported on windows. Which is kind-of funny.


Re: dis...@dlang.org

2015-09-23 Thread Ali Çehreli via Digitalmars-d-learn

On 09/23/2015 06:01 AM, Aidan wrote:

I am just starting to look into D and i have to say I am loving it at
the moment. But I have ran into an issue that i can't seem to find any
libraries for Api hooking.

If anyone knows of a well documented source for this it would be much
appreciated.


I don't understand the question but it may be related to lack of fiber 
in one's diet: :)


  http://vibed.org/features#fibers

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

Ali



Re: dis...@dlang.org

2015-09-23 Thread novice2 via Digitalmars-d-learn

http://forum.dlang.org/thread/hrzfcjrltftgzansd...@forum.dlang.org
https://github.com/Trass3r/hooksample


What's wrong with this BinaryHeap declaration line? Assertion failure in std.container.array.d

2015-09-23 Thread Enjoys Math via Digitalmars-d-learn

What I HAD TO do to get it to compile:

programResultsQ = heapify!(compareResults, 
Array!(Results!(O,I)))(Array!(Results!(O,I))([Results!(O,I)()]), 
1);

programResultsQ.popFront();

What running it says:

AssertionFailure at line 381 of std.container.array.d, which 
looks like:


/**
Constructor taking a number of items
 */
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T))
{
import std.conv : emplace;
auto p = cast(T*) malloc(T.sizeof * values.length);
static if (hasIndirections!T)
{
if (p)
GC.addRange(p, T.sizeof * values.length);
}

foreach (i, e; values)
{
emplace(p + i, e);
assert(p[i] == e); /* THIS IS LINE 381 */
}
_data = Data(p[0 .. values.length]);
}

Any ideas.  How can I improve this declaration?  Using Phobos 
sometimes is such a mystery.





Re: What's wrong with this BinaryHeap declaration line? Assertion failure in std.container.array.d

2015-09-23 Thread Enjoys Math via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 05:56:08 UTC, Enjoys Math 
wrote:

What I HAD TO do to get it to compile:

programResultsQ = heapify!(compareResults, 
Array!(Results!(O,I)))(Array!(Results!(O,I))([Results!(O,I)()]), 1);

programResultsQ.popFront();

What running it says:

AssertionFailure at line 381 of std.container.array.d, which 
looks like:


/**
Constructor taking a number of items
 */
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T))
{
import std.conv : emplace;
auto p = cast(T*) malloc(T.sizeof * values.length);
static if (hasIndirections!T)
{
if (p)
GC.addRange(p, T.sizeof * values.length);
}

foreach (i, e; values)
{
emplace(p + i, e);
assert(p[i] == e); /* THIS IS LINE 381 */
}
_data = Data(p[0 .. values.length]);
}

Any ideas.  How can I improve this declaration?  Using Phobos 
sometimes is such a mystery.


I mean initialization...

Here's the corresponding declaration:

alias ProgramResultsQueue(O,I) = 
BinaryHeap!(Array!(Results!(O,I)), compareResults); /* module 
scope */


ProgramResultsQueue!(O,I) programResultsQ;   /* class member */

The intialization line occurs in the class's ctor.



Re: Why is the constructor of B called?

2015-09-23 Thread tcak via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 21:14:17 UTC, Adam D. Ruppe 
wrote:

On Wednesday, 23 September 2015 at 21:08:37 UTC, tcak wrote:
I wouldn't expect B's constructor to be called at all unless 
"super" is used there.


"If no call to constructors via this or super appear in a 
constructor, and the base class has a constructor, a call to 
super() is inserted at the beginning of the constructor. "



from http://dlang.org/class.html#constructors

the idea is to make sure the base class construction work is 
done too.


Is there any way to prevent this behaviour?

Quickly checked whether Java acts in the same way. Answer is yes.


Re: Why is the constructor of B called?

2015-09-23 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 23 September 2015 at 21:08:37 UTC, tcak wrote:
I wouldn't expect B's constructor to be called at all unless 
"super" is used there.


"If no call to constructors via this or super appear in a 
constructor, and the base class has a constructor, a call to 
super() is inserted at the beginning of the constructor. "



from http://dlang.org/class.html#constructors

the idea is to make sure the base class construction work is done 
too.


Re: Is there a smart way to process a range of range by front ?

2015-09-23 Thread BBasile via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 21:04:44 UTC, Justin Whear 
wrote:

On Wed, 23 Sep 2015 20:48:03 +, BBasile wrote:

I was thinking to a general *interleave()* algorithm for any 
compatible Range of Range but I can't find any smart way to 
process each sub range by front


Can you show a sample input and output to clarify what you mean 
by interleave?  It's possible that what you want is 
std.range.frontTransversal, std.range.transversal, or 
std.range.transposed.


---
auto r0 = [[0,2],[1,3]];
auto r1 = interleave(r0);
assert(r1 = [0,1,2,3]);
auto r2 = [[0,3],[1,4],[2,5]];
auto r3 = interleave(r2);
assert(r3 = [0,1,2,3,4,5]);
---

the fact that the numbers are ordered is just an helper.




Re: Is there a smart way to process a range of range by front ?

2015-09-23 Thread Justin Whear via Digitalmars-d-learn
On Wed, 23 Sep 2015 21:17:27 +, BBasile wrote:

> On Wednesday, 23 September 2015 at 21:04:44 UTC, Justin Whear wrote:
>> On Wed, 23 Sep 2015 20:48:03 +, BBasile wrote:
>>
>>> I was thinking to a general *interleave()* algorithm for any
>>> compatible Range of Range but I can't find any smart way to process
>>> each sub range by front
>>
>> Can you show a sample input and output to clarify what you mean by
>> interleave?  It's possible that what you want is
>> std.range.frontTransversal, std.range.transversal, or
>> std.range.transposed.
> 
> ---
> auto r0 = [[0,2],[1,3]];
> auto r1 = interleave(r0);
> assert(r1 = [0,1,2,3]);
> auto r2 = [[0,3],[1,4],[2,5]];
> auto r3 = interleave(r2);
> assert(r3 = [0,1,2,3,4,5]);
> ---
> 
> the fact that the numbers are ordered is just an helper.

OK, I think what you're after is std.range.roundRobin.


Re: Is there a smart way to process a range of range by front ?

2015-09-23 Thread BBasile via Digitalmars-d-learn

On Wednesday, 23 September 2015 at 21:17:29 UTC, BBasile wrote:
On Wednesday, 23 September 2015 at 21:04:44 UTC, Justin Whear 
wrote:

On Wed, 23 Sep 2015 20:48:03 +, BBasile wrote:

I was thinking to a general *interleave()* algorithm for any 
compatible Range of Range but I can't find any smart way to 
process each sub range by front


Can you show a sample input and output to clarify what you 
mean by interleave?  It's possible that what you want is 
std.range.frontTransversal, std.range.transversal, or 
std.range.transposed.


---
auto r0 = [[0,2],[1,3]];
auto r1 = interleave(r0);
assert(r1 = [0,1,2,3]);
auto r2 = [[0,3],[1,4],[2,5]];
auto r3 = interleave(r2);
assert(r3 = [0,1,2,3,4,5]);
---

the fact that the numbers are ordered is just an helper.


just imagine that there are double equal symbols in the 
assertions...


Re: Is there a smart way to process a range of range by front ?

2015-09-23 Thread BBasile via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 21:24:22 UTC, Justin Whear 
wrote:

On Wed, 23 Sep 2015 21:17:27 +, BBasile wrote:

On Wednesday, 23 September 2015 at 21:04:44 UTC, Justin Whear 
wrote:

On Wed, 23 Sep 2015 20:48:03 +, BBasile wrote:

I was thinking to a general *interleave()* algorithm for any 
compatible Range of Range but I can't find any smart way to 
process each sub range by front


Can you show a sample input and output to clarify what you 
mean by interleave?  It's possible that what you want is 
std.range.frontTransversal, std.range.transversal, or 
std.range.transposed.


---
auto r0 = [[0,2],[1,3]];
auto r1 = interleave(r0);
assert(r1 = [0,1,2,3]);
auto r2 = [[0,3],[1,4],[2,5]];
auto r3 = interleave(r2);
assert(r3 = [0,1,2,3,4,5]);
---

the fact that the numbers are ordered is just an helper.


OK, I think what you're after is std.range.roundRobin.


---
import std.range;

auto interleave(RoR)(RoR r)
{
return r.transposed.join;
}

void main()
{
auto r0 = [[0,2],[1,3]];
auto r1 = interleave(r0);
assert(r1 == [0,1,2,3]);
auto r2 = [[0,3],[1,4],[2,5]];
auto r3 = interleave(r2);
assert(r3 == [0,1,2,3,4,5]);
}
--

thx, but as you was suposing initially 'transposed' works.
didn't know this function before. works fine.


Re: Why is the constructor of B called?

2015-09-23 Thread Ali Çehreli via Digitalmars-d-learn

On 09/23/2015 02:25 PM, tcak wrote:

On Wednesday, 23 September 2015 at 21:14:17 UTC, Adam D. Ruppe wrote:

On Wednesday, 23 September 2015 at 21:08:37 UTC, tcak wrote:

I wouldn't expect B's constructor to be called at all unless "super"
is used there.


"If no call to constructors via this or super appear in a constructor,
and the base class has a constructor, a call to super() is inserted at
the beginning of the constructor. "


from http://dlang.org/class.html#constructors

the idea is to make sure the base class construction work is done too.


Is there any way to prevent this behaviour?


No and I don't think it will ever be implemented. The derived class is 
supposed to be used as the super class, which involves proper 
construction of the super parts.



Quickly checked whether Java acts in the same way. Answer is yes.


Same with C++. As discussed in the other thread, at least D allows 
changing the order in which the super constructor is executed.


Ali



Is there a smart way to process a range of range by front ?

2015-09-23 Thread BBasile via Digitalmars-d-learn
I was thinking to a general *interleave()* algorithm for any 
compatible Range of Range but I can't find any smart way to 
process each sub range by front, eg:


---
void interleave(RoR)(RoR r)
{
   r.each!(a => a.writeln);
}

void main()
{
auto r = [[0,2],[1,3]];
interleave(r);
}
---

will print:
[0,2]
[1,3]

while to interleave i need to take the front of each sub range 
before poping each ror element.


Currently I'm here (don't run this ;)) :

---
auto interleave(RoR)(RoR r)
{
alias T = ElementType!r[0];
T[] result;
while (!empty(r[0]))
r.each!(a => (result ~= a.front, a.popFront));
return result;
}

void main()
{
auto r = [[0,2],[1,3]];
interleave(r);
}
---

but it doesn't work because 'a' is not consumed. It looks like 
it's saved from the input parameter at each iteration of the 
while loop hence it never returns.


Is it possible ?


Why is the constructor of B called?

2015-09-23 Thread tcak via Digitalmars-d-learn

[code]
import std.stdio;

class B {
this() {
writeln("B.constructor");
foo();
}

void foo() {
writeln("B.foo");
}
}

class D : B {
this() {
writeln("D.constructor");
}

override void foo() {
writeln("D.foo overrides B.foo");
}
}

void main() {
auto b = new D();
}
[/code]

Result:

B.constructor
D.foo overrides B.foo
D.constructor



There is no use of "super()" in the constructor of D, yet B's 
constructor is called when D is created. Why is that so?


I changed the constructor of D as follows:

[code]
this() {
super();
writeln("D.constructor");
}
[/code]

Results haven't changed at all. "super()" doesn't make any 
difference. What's going on?


I wouldn't expect B's constructor to be called at all unless 
"super" is used there.


Re: Is there a smart way to process a range of range by front ?

2015-09-23 Thread Justin Whear via Digitalmars-d-learn
On Wed, 23 Sep 2015 20:48:03 +, BBasile wrote:

> I was thinking to a general *interleave()* algorithm for any compatible
> Range of Range but I can't find any smart way to process each sub range
> by front

Can you show a sample input and output to clarify what you mean by 
interleave?  It's possible that what you want is 
std.range.frontTransversal, std.range.transversal, or 
std.range.transposed.


Re: Dub package with C code

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

On 24/09/15 2:43 PM, Sebastiaan Koppe wrote:

I have just created bindings for libxlsxwriter, an c library for
creating excel files.

Used the htod tool to do most of the work, and only had to adjust some
things - mainly because libxlsxwriter uses data structures written in
macro's.

Right now I am making a dub package and I would like to aim for
convenience for end-users (read: me).

Therefor I decided to include the compiled static library inside the
package. I only use Linux 64-bit myself, but this is obviously limiting
for other people.

The other option I had was to include the whole c code, depend on gcc or
clang, and have dub  (somehow) first build libxlsxwriter. But that
seemed a bit too much...

Another option would be to require end-users to build libxlsxwriter
themselves.

What do you guys recommend?


Is libxlsxwriter available in the systems package manager?
If so, I would just link against that. Preferably the shared library.
Let e.g. Windows users figure theirs out.


Dub package with C code

2015-09-23 Thread Sebastiaan Koppe via Digitalmars-d-learn
I have just created bindings for libxlsxwriter, an c library for 
creating excel files.


Used the htod tool to do most of the work, and only had to adjust 
some things - mainly because libxlsxwriter uses data structures 
written in macro's.


Right now I am making a dub package and I would like to aim for 
convenience for end-users (read: me).


Therefor I decided to include the compiled static library inside 
the package. I only use Linux 64-bit myself, but this is 
obviously limiting for other people.


The other option I had was to include the whole c code, depend on 
gcc or clang, and have dub  (somehow) first build libxlsxwriter. 
But that seemed a bit too much...


Another option would be to require end-users to build 
libxlsxwriter themselves.


What do you guys recommend?


Re: Is there a smart way to process a range of range by front ?

2015-09-23 Thread Martin Nowak via Digitalmars-d-learn

On Wednesday, 23 September 2015 at 21:30:37 UTC, BBasile wrote:

auto interleave(RoR)(RoR r)
{
return r.transposed.join;


If you use joiner it will even be lazy and avoid the allocation.


Re: Why is the constructor of B called?

2015-09-23 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Sep 23, 2015 at 03:25:04PM -0700, Ali Çehreli via Digitalmars-d-learn 
wrote:
> On 09/23/2015 02:25 PM, tcak wrote:
> >On Wednesday, 23 September 2015 at 21:14:17 UTC, Adam D. Ruppe wrote:
> >>On Wednesday, 23 September 2015 at 21:08:37 UTC, tcak wrote:
> >>>I wouldn't expect B's constructor to be called at all unless
> >>>"super" is used there.
> >>
> >>"If no call to constructors via this or super appear in a
> >>constructor, and the base class has a constructor, a call to super()
> >>is inserted at the beginning of the constructor. "
> >>
> >>
> >>from http://dlang.org/class.html#constructors
> >>
> >>the idea is to make sure the base class construction work is done
> >>too.
> >
> >Is there any way to prevent this behaviour?
> 
> No and I don't think it will ever be implemented. The derived class is
> supposed to be used as the super class, which involves proper
> construction of the super parts.
[...]

I can't think of any valid use case for not running the base class ctor.
Are you sure you aren't violating the Liskov Substitution Principle in
some way?  It may be that what you need is a has-a relationship rather
than an is-a relationship in your class.


T

-- 
Once the bikeshed is up for painting, the rainbow won't suffice. -- Andrei 
Alexandrescu


Re: Why is the constructor of B called?

2015-09-23 Thread Nicholas Wilson via Digitalmars-d-learn

On Wednesday, 23 September 2015 at 21:25:15 UTC, tcak wrote:
On Wednesday, 23 September 2015 at 21:14:17 UTC, Adam D. Ruppe 
wrote:

On Wednesday, 23 September 2015 at 21:08:37 UTC, tcak wrote:
I wouldn't expect B's constructor to be called at all unless 
"super" is used there.


"If no call to constructors via this or super appear in a 
constructor, and the base class has a constructor, a call to 
super() is inserted at the beginning of the constructor. "



from http://dlang.org/class.html#constructors

the idea is to make sure the base class construction work is 
done too.


Is there any way to prevent this behaviour?

Quickly checked whether Java acts in the same way. Answer is 
yes.


You might be able to swap out the vtbl entry  for a stub call it 
and trick the compiler and swap it back, but...