passing static arrays to each! with a ref param [Re: Why can't static arrays be sorted?]

2016-10-10 Thread Jon Degenhardt via Digitalmars-d-learn
On Monday, 10 October 2016 at 16:46:55 UTC, Jonathan M Davis 
wrote:
On Monday, October 10, 2016 16:29:41 TheGag96 via 
Digitalmars-d-learn wrote:
On Saturday, 8 October 2016 at 21:14:43 UTC, Jon Degenhardt 
wrote:
> This distinction is a bit on the nuanced side. Is it 
> behaving as it should?

>
> --Jon

I think so? It's not being modified in the second case because 
the array is being passed by value... "x" there is a reference 
to an element of the copy created to be passed to each(). I 
assume there's a good reason why ranges in general are passed 
by value into these functions -- except in this one case, the 
stuff inside range types copied when passed by value won't be 
whole arrays, I'm guessing.


Whether it's by value depends entirely on the type of the 
range. They're passed around, and copying them has whatever 
semantics it has. In most cases, it copies the state of the 
range but doesn't copy all of the elements (e.g. that's what 
happens with a dynamic array, since it gets sliced). But if a 
range is a class, then it's definitely a reference type.  The 
only way to properly save the state of a range is to call save.


But passing by ref would make no sense at all with input 
ranges. It would completely kill chaining them. Almost all 
range-based functions return rvalues.


- Jonathan M Davis


The example I gave uses ref parameters. On the surface it would 
seem reasonable to that passing a static array by ref would allow 
it to be modified, without having to slice it first. The 
documentation says:


// If the range supports it, the value can be mutated in place
   arr.each!((ref n) => n++);
   assert(arr == [1, 2, 3, 4, 5]);

but, 'arr' is a dynamic array, so technically it's not describing 
a static array (the opApply case).


Expanding the example, using foreach with ref parameters will 
modify the static array in place, without slicing it. I would 
have expected each! with a ref parameter to behave the same.


At a minimum this could be better documented, but it may also be 
a bug.


Example:

T increment(T)(ref T x) { return x++; }

void main()
{
import std.algorithm : each;

int[] dynamicArray = [1, 2, 3, 4, 5];
int[5] staticArray = [1, 2, 3, 4, 5];

dynamicArray.each!(x => x++); // Dynamic array by 
value

assert(dynamicArray == [1, 2, 3, 4, 5]);  // ==> Not modified

dynamicArray.each!((ref x) => x++);   // Dynamic array by 
ref

assert(dynamicArray == [2, 3, 4, 5, 6]);  // ==> Modified

staticArray[].each!((ref x) => x++);  // Slice of static 
array, by ref

assert(staticArray == [2, 3, 4, 5, 6]);   // ==> Modified

staticArray.each!((ref x) => x++);// Static array by 
ref

assert(staticArray == [2, 3, 4, 5, 6]);   // ==> Not Modified

/* Similar to above, using foreach and ref params. */
foreach (ref x; dynamicArray) x.increment;
assert(dynamicArray == [3, 4, 5, 6, 7]);  // Dynamic array => 
Modified


foreach (ref x; staticArray[]) x.increment;
assert(staticArray == [3, 4, 5, 6, 7]);   // Static array 
slice => Modified


foreach (ref x; staticArray) x.increment;
assert(staticArray == [4, 5, 6, 7, 8]);   // Static array => 
Modified

}



Re: MemberDefaults trait

2016-10-10 Thread Ali Çehreli via Digitalmars-d-learn

On 10/10/2016 04:50 PM, Ali Çehreli wrote:

> static if (!is(T == struct)) {
> static assert(T.stringof ~ " is not a struct type");
> }

Wow! That's a nice brain fart on my part. Ok, I can fix that one... :)

Ali



Re: Current State of the GC?

2016-10-10 Thread rikki cattermole via Digitalmars-d-learn

On 11/10/2016 10:12 AM, Martin Lundgren wrote:

I've been reading up a bit on the D garbage collector. Seen mostly
negative things about it. I've also seen a lot of proposals and what
not, but not much about the current state of things.

The latest page I can find about it is 2015H1. It mentions improving the
GC and making libraries less reliant on it. However, I can't find *any*
information about what GC improvements have been made. No up to date
performance comparisons, etc.

So what's been happening in memory management land lately? Bad GC seems
like one of the Dlangs weak points, so showing improvements here could
definitely bring more people in.


Well I can't say what has happened since that half way document.
Most of the work that goes on is minor tweaks and improvements that 
pretty much nobody outside of druntime knows about and that is quite all 
right.


If you want to actually see all these things going on check out Github[0].

Anyway, most of the time the GC isn't a problem, contrary to popular 
belief. As long as you do tricks like reusing memory it will never fire 
and more importantly you won't be hit with memory allocation costs. So 
basically a double win for you in terms of speed.


When dealing with multi threading you probably want to disable the GC 
collection and have predefined points so it can collect safely with no 
performance hits.


In reality, except for some cycle counting points, you won't need a 
million dollar GC and even then they tend to fail at those sort of jobs.


[0] 
https://github.com/dlang/druntime/tree/2db828bd4f21807254b770b3ec304f14596a9805/src/gc


MemberDefaults trait

2016-10-10 Thread Ali Çehreli via Digitalmars-d-learn
Could you please review the following template to see whether it makes 
sense. It produces an AliasSeq type consisting of the default values of 
the members of a struct. It should and does support members that are 
initialized with '= void'. I could not achieve this with std.traits or 
__traits.


However, I'm not that happy with the use of __traits(compiles) below. 
Can this be improved?


Thank you,
Ali


import std.meta;

/** Get as an expression tuple the default values of members of a struct. */
template MemberDefaults(T) {
static if (!is(T == struct)) {
static assert(T.stringof ~ " is not a struct type");
}

import std.traits : FieldNameTuple;
enum t = T.init;
alias fields = FieldNameTuple!T;

template get(size_t i) {
static if (__traits(compiles, { enum _ = t.tupleof[i]; })) {
enum get = t.tupleof[i];
}
else {
alias get = void;
}
}

template Impl(size_t i = 0) {
import std.meta : AliasSeq;
static if (i == fields.length) {
alias Impl = AliasSeq!();
} else {
alias Impl = AliasSeq!(get!i, Impl!(i+1));
}
}

alias MemberDefaults = Impl!();
}

unittest {
struct S {
int i = 42;
string s = "hello";
char c = 'c';
}

static assert(MemberDefaults!S == AliasSeq!(42, "hello", 'c'));
}

unittest {
struct S {
int i = 42;
int j = void;
}

static assert(MemberDefaults!S[0] == 42);
static assert(is(MemberDefaults!S[1] == void));
}

void main() {
}


Re: How to correctly display accented characters at the Windows prompt?

2016-10-10 Thread Martin Krejcirik via Digitalmars-d-learn

Windows. You can try command cp 65001 in the console window


chcp 65001



Re: How to correctly display accented characters at the Windows prompt?

2016-10-10 Thread Martin Krejcirik via Digitalmars-d-learn
On Monday, 10 October 2016 at 19:31:14 UTC, Cleverson Casarin 
Uliana wrote:
Hi Martin, indeed, here in my workplace Windows machine there 
is no "CP_UTF_8" codepage, nor there is 65001. I was waiting to


codepage 65001 (UTF8) should be available on all modern Windows. 
You can try command cp 65001 in the console window (and change 
your font accordingly).


Just for info, this is probably a d related bug, because the 
Racket language for example has a (reencode-output-port) 
function, and it Works.


I don't know know about Racket, but D's problems are related to 
the C runtime library. Here is what I found out:


-m32 (uses DM C library): output to stdout works, but stderr is 
broken (corrupt accented characters, or total output freeze).

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

-m32mscoff and -m64 (Microsoft C runtime): broken characters and 
some mysterious failures.

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

So this leaves transcoding as the only viable solution. Luckily, 
there is std.windows.charset.toMBSz function you can use.


Re: How to correctly display accented characters at the Windows prompt?

2016-10-10 Thread Cleverson Casarin Uliana via Digitalmars-d-learn
Hi Martin, indeed, here in my workplace Windows machine there is no
"CP_UTF_8" codepage, nor there is 65001. I was waiting to try it later
on my home machine, but since you say it's broken, then I'll need to
look for a way to convert the actual string to the 850 codepage...

Just for info, this is probably a d related bug, because the Racket
language for example has a (reencode-output-port) function, and it
Works.

Greetings,
Cleverson


Re: opIndexDispatch?

2016-10-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, October 10, 2016 19:01:19 Yuxuan Shui via Digitalmars-d-learn 
wrote:
> Hi,
>
> Why is there no opIndexDispatch for overloading a[x].func() ?

There's opIndex for overloading a[x], and then you can call a function on
the return value. If you want some kind of opDispatch on the return value,
then the return type will need to implement opDispatch.

- Jonathan M Davis



opIndexDispatch?

2016-10-10 Thread Yuxuan Shui via Digitalmars-d-learn

Hi,

Why is there no opIndexDispatch for overloading a[x].func() ?


Re: ptrdiff_t of class.tupleof entry

2016-10-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, October 10, 2016 17:57:15 Satoshi via Digitalmars-d-learn wrote:
> Hello,
> How can I take an ptrdiff_t of any tupleoff entry from class
> during compilation?
>
>
> I need something like this:
>
> template Test(T, int i) {
>   enum Test = [i] - [0];
> }
>
>
> and then use it like:
>
> class AnyClass {
> int a;
> int b;
> int c;
> string d;
>
> }
>
> enum Addr = Test!(AnyClass, 3);
>
> void assignInt!(T)(AnyClass cls, T value) {
> *(cast(T *)cast(void *)cls + Addr) = value;
> }
>
>
>
> This is just an example how I need to use it.
> Is something like this possible to do?

You can use the offsetof property of a member variable to find out the
offset between its address and the address of the beginning of the class or
struct that it's a member of.

- Jonathan M Davis



ptrdiff_t of class.tupleof entry

2016-10-10 Thread Satoshi via Digitalmars-d-learn

Hello,
How can I take an ptrdiff_t of any tupleoff entry from class 
during compilation?



I need something like this:

template Test(T, int i) {
 enum Test = [i] - [0];
}


and then use it like:

class AnyClass {
int a;
int b;
int c;
string d;

}

enum Addr = Test!(AnyClass, 3);

void assignInt!(T)(AnyClass cls, T value) {
*(cast(T *)cast(void *)cls + Addr) = value;
}



This is just an example how I need to use it.
Is something like this possible to do?


Re: How to correctly display accented characters at the Windows prompt?

2016-10-10 Thread Martin Krejcirik via Digitalmars-d-learn

Thanks,
Cleverson


Call SetConsoleOutputCP(CP_UTF8).


No, this may appear to to work, but in reality, it's broken. The 
only reliable way is to convert to the native windows codepage.




Re: isRvalue trait

2016-10-10 Thread Nordlöw via Digitalmars-d-learn

On Monday, 10 October 2016 at 12:22:54 UTC, Marc Schütz wrote:

I would like to overload to an identity op.


If the compiler is smart enough to understand what 
`moveEmplace()` does, it could already do this automatically.


Doh! My mistake.

I'll use `moveEmplace`, then.

Thx!


Re: Easy sockets - don't exist yet?

2016-10-10 Thread Konstantin Kutsevalov via Digitalmars-d-learn

On Monday, 10 October 2016 at 07:37:48 UTC, Bauss wrote:
Wrote some pretty simple sockets that you could use (Based on 
vibe.d though.)


https://github.com/bausshf/cheetah


Hi,

Yes I saw it, but not sure. Does it make sense to use vibe.d only 
for sockets. I mean, it like a web framework with many 
dependencies etc...


Re: Easy sockets - don't exist yet?

2016-10-10 Thread Konstantin Kutsevalov via Digitalmars-d-learn
On Monday, 10 October 2016 at 02:54:09 UTC, Jonathan M Davis 
wrote:
On Monday, October 10, 2016 01:43:54 Konstantin Kutsevalov via 
So, it's simply gone. But if someone wants to propose a 
replacement, they're certainly still free to do so.


- Jonathan M Davis


I see, thank you for answer


Re: Why can't static arrays be sorted?

2016-10-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, October 10, 2016 16:29:41 TheGag96 via Digitalmars-d-learn wrote:
> On Saturday, 8 October 2016 at 21:14:43 UTC, Jon Degenhardt wrote:
> > This distinction is a bit on the nuanced side. Is it behaving
> > as it should?
> >
> > --Jon
>
> I think so? It's not being modified in the second case because
> the array is being passed by value... "x" there is a reference to
> an element of the copy created to be passed to each(). I assume
> there's a good reason why ranges in general are passed by value
> into these functions -- except in this one case, the stuff inside
> range types copied when passed by value won't be whole arrays,
> I'm guessing.

Whether it's by value depends entirely on the type of the range. They're
passed around, and copying them has whatever semantics it has. In most
cases, it copies the state of the range but doesn't copy all of the elements
(e.g. that's what happens with a dynamic array, since it gets sliced). But
if a range is a class, then it's definitely a reference type.  The only way
to properly save the state of a range is to call save.

But passing by ref would make no sense at all with input ranges. It would
completely kill chaining them. Almost all range-based functions return
rvalues.

- Jonathan M Davis



Re: Why can't static arrays be sorted?

2016-10-10 Thread TheGag96 via Digitalmars-d-learn

On Saturday, 8 October 2016 at 21:14:43 UTC, Jon Degenhardt wrote:
This distinction is a bit on the nuanced side. Is it behaving 
as it should?


--Jon


I think so? It's not being modified in the second case because 
the array is being passed by value... "x" there is a reference to 
an element of the copy created to be passed to each(). I assume 
there's a good reason why ranges in general are passed by value 
into these functions -- except in this one case, the stuff inside 
range types copied when passed by value won't be whole arrays, 
I'm guessing.


Re: How to correctly display accented characters at the Windows prompt?

2016-10-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, October 10, 2016 13:24:09 Cleverson Casarin Uliana via Digitalmars-
d-learn wrote:
> Hello John and all, how do you search for a given function to see
> where it is defined/declared? I tried to find SetConsoleOutputCP by
> myself, but the search embeded in the documentation cannot find
> anything in the phobos library refference, and searching the entire
> site returns forum posts only.

It looks like it's in

core.sys.windows.wincon;

- Jonathan M Davis



Re: How to correctly display accented characters at the Windows prompt?

2016-10-10 Thread Cleverson Casarin Uliana via Digitalmars-d-learn
Hello John and all, how do you search for a given function to see
where it is defined/declared? I tried to find SetConsoleOutputCP by
myself, but the search embeded in the documentation cannot find
anything in the phobos library refference, and searching the entire
site returns forum posts only.

Thanks,
Cleverson


Re: How to correctly display accented characters at the Windows prompt?

2016-10-10 Thread John C via Digitalmars-d-learn
On Monday, 10 October 2016 at 14:14:08 UTC, Cleverson Casarin 
Uliana wrote:
Hello, when I run a compiled Windows executable at the console, 
letters outside the ascii range such as ç and ã do not display 
propperly. Is there any d function to change the console code 
page on the fly? My Windows console code page, which is for 
Brazilian Portuguese, is at 850. Perhaps an alternative would 
be to convert d strings from Unicode to the 850 code page on 
the fly.


Thanks,
Cleverson


Call SetConsoleOutputCP(CP_UTF8).


How to correctly display accented characters at the Windows prompt?

2016-10-10 Thread Cleverson Casarin Uliana via Digitalmars-d-learn
Hello, when I run a compiled Windows executable at the console,
letters outside the ascii range such as ç and ã do not display
propperly. Is there any d function to change the console code page on
the fly? My Windows console code page, which is for Brazilian
Portuguese, is at 850. Perhaps an alternative would be to convert d
strings from Unicode to the 850 code page on the fly.

Thanks,
Cleverson



Re: isRvalue trait

2016-10-10 Thread Marc Schütz via Digitalmars-d-learn

On Monday, 10 October 2016 at 11:46:01 UTC, Nordlöw wrote:

At

https://github.com/nordlow/phobos-next/blob/master/src/moval.d

I've implemented a helper function for creating r-value out of 
l-values defined as


E movedToRvalue(E)(ref E e)
{
import std.algorithm.mutation : move;
E value;
move(e, value); // this can be optimized
return value;
}



Doesn't the second overload of `move()` already do what you want?

https://dlang.org/phobos/std_algorithm_mutation.html#.move

Note that in your implementation, you needlessly initialize 
`value`, which then needs to be properly destroyed by `move()`, 
which the Phobos implementation avoids:


https://github.com/dlang/phobos/blob/master/std/algorithm/mutation.d#L1083-L1088

For the case when movedToRvalue is called with an r-value, such 
as in,


static assert(__traits(compiles, { consume(S(14)); }));

I would like to overload to an identity op.


If the compiler is smart enough to understand what 
`moveEmplace()` does, it could already do this automatically.


Re: isRvalue trait

2016-10-10 Thread Nordlöw via Digitalmars-d-learn

On Monday, 10 October 2016 at 11:51:09 UTC, Nordlöw wrote:

Found it:

http://forum.dlang.org/post/n8m8bh$2vgc$1...@digitalmars.com
https://issues.dlang.org/show_bug.cgi?id=15634


Ok, so I added `isLvalue` and `isRvalue` to

https://github.com/nordlow/phobos-next/blob/master/src/moval.d

Now the next follow-up question becomes how to use it to 
restrict/overload my definition of `movedToRvalue()` since 
`isLvalue` takes an alias as argument and `movedToRvalue` takes a 
reference to an instance. How to solve this?


Re: isRvalue trait

2016-10-10 Thread Nordlöw via Digitalmars-d-learn

On Monday, 10 October 2016 at 11:46:01 UTC, Nordlöw wrote:

Is there one?


Found it:

http://forum.dlang.org/post/n8m8bh$2vgc$1...@digitalmars.com
https://issues.dlang.org/show_bug.cgi?id=15634


isRvalue trait

2016-10-10 Thread Nordlöw via Digitalmars-d-learn

At

https://github.com/nordlow/phobos-next/blob/master/src/moval.d

I've implemented a helper function for creating r-value out of 
l-values defined as


E movedToRvalue(E)(ref E e)
{
import std.algorithm.mutation : move;
E value;
move(e, value); // this can be optimized
return value;
}

For the case when movedToRvalue is called with an r-value, such 
as in,


static assert(__traits(compiles, { consume(S(14)); }));

I would like to overload to an identity op.

Is this currently possible somehow? I cannot find any trait 
`isRvalue` that fulfills


static assert(isRvalue!(S(14)));

Is there one?


Re: When to call GC.{add,remove}Range in containers?

2016-10-10 Thread Nordlöw via Digitalmars-d-learn

On Monday, 10 October 2016 at 08:26:45 UTC, Kagamin wrote:

On Monday, 10 October 2016 at 07:12:10 UTC, Nordlöw wrote:

should not be scanned by the GC.


Shouldn't be a problem.


What do you mean?

I'm talking about an optimization; don't call addRange when we 
don't need to, because the internal stored `E* _ptr` is 
maintained manually by calls to `*alloc` and `free`.


Re: How do I load a shared library?

2016-10-10 Thread Nafees via Digitalmars-d-learn
Fixed this issue, stackoverflow helped 
http://stackoverflow.com/questions/39929495


just compiled the library with -fPIC -m32 -shared


Re: How do I load a shared library?

2016-10-10 Thread Nafees via Digitalmars-d-learn
anyone? so there is no way to get my 50,000 Line of code to work 
again? All that code to waste?


P.S: I've tried doing these:
Tried to use GDC, no luck
used -defaultlib=libphobos2.so, no luck
removed all functions from library, compiled it empty, yet, 
dlopen gives segFault.


AND:
This same code used to work on ubuntu 14.04 (i386), now I'm on 
xubuntu 16.04 (amd64).


Re: weighted round robin

2016-10-10 Thread Marc Schütz via Digitalmars-d-learn

On Saturday, 8 October 2016 at 22:48:53 UTC, vino wrote:

Hi,

 Can some one guide me on how to implement the weighted round 
robin, below is what i tried or any other better ways to do it


Main Requirement : Incoming socket connection has to be sent to 
3 servers in the weighted round robin fashion.


Prog:1
import std.stdio;
import std.range;
import std.range.primitives;

void main()
{
auto a = [1,2,3]; // E.g :Server Array
auto b = [1,2,3,4,5]; // E.g: Socket Array
auto r = roundRobin(a, b);
writeln(r);
}
OUTPUT : [1, 1, 2, 2, 3, 3, 4, 5]
Requirement : [1, 1, 2, 2, 3, 3,1,4,2,5]


auto r = roundRobin(a.cycle, b.cycle);

Beware though that this yields an infinite range. If you just 
need one round, you can use:


import std.algorithm.comparison : max;
writeln(r.take(max(a.length, b.length)));



Re: When to call GC.{add,remove}Range in containers?

2016-10-10 Thread Kagamin via Digitalmars-d-learn

On Monday, 10 October 2016 at 07:12:10 UTC, Nordlöw wrote:

should not be scanned by the GC.


Shouldn't be a problem.


Re: dub command line in config?

2016-10-10 Thread Seb via Digitalmars-d-learn

On Sunday, 9 October 2016 at 20:03:58 UTC, WhatMeWorry wrote:

On Sunday, 9 October 2016 at 19:11:52 UTC, Jinx wrote:
On Sunday, 9 October 2016 at 08:52:55 UTC, rikki cattermole 
wrote:

On 09/10/2016 9:17 PM, Jinx wrote:

[...]


That is enough.
Mike Parker has presented a workaround that you can implement.
He has also shown how you can contact those that have the 
power to make this happen as you desire.


But as shown by other related issues[0], you may not get what 
you want.


[0] https://github.com/dlang/dub/issues/940


Yessim boss!  plez dunt hurt me! he shoed ma how too puck da 
cotton n giv me dis hur bag. I alredy no hew to puck da cotton 
n hud me own bag.


Hey Jinx.  Why don't you rename yourself to Jerk.


This is not a company - this is open source. I understand that 
one can be frustrated sometimes, but it should be kept in mind 
that all contributions happen voluntarily in people's free time. 
If you want something to be changed (and can't wait that someone 
does it in their free time), please submit a PR.


Re: Easy sockets - don't exist yet?

2016-10-10 Thread Bauss via Digitalmars-d-learn
Wrote some pretty simple sockets that you could use (Based on 
vibe.d though.)


https://github.com/bausshf/cheetah


When to call GC.{add,remove}Range in containers?

2016-10-10 Thread Nordlöw via Digitalmars-d-learn
Which std.trait should be used to statically check whether I 
should call GC.{add,remove}Range on the elements of D containers?


`std.container.array.Array` currently uses `hasIndirections` but 
a comment on the same line says it should use `hasPointers` 
instead.


containers-em uses

template shouldAddGCRange(T)
{
import std.traits;
	enum shouldAddGCRange = isPointer!T || hasIndirections!T || is 
(T == class);

}

Quite some inconsistency here.

And what the case `Array!(Array!int))`? The wrapper Array!int 
contains contains a non-GC allocate pointer to ints and should 
not be scanned by the GC. Do we need another Container-trait for 
this? Or do we need to tag pointers with a special attribute that 
tells whether it has been allocated by GC or not.




Re: Problems with "shared"

2016-10-10 Thread Satoshi via Digitalmars-d-learn

On Sunday, 9 October 2016 at 21:32:23 UTC, ag0aep6g wrote:

On 10/09/2016 10:57 PM, jython234 wrote:

1. Where do I use the "shared" keyword?


What doesn't work is creating an unshared object of a class 
that only has a shared constructor. To create both shared and 
unshared objects, you need either two constructors or a `pure` 
one.


class C
{
this() pure {}
}
void main()
{
auto u = new C; /* works */
auto s = new shared C; /* too */
}




Why this ins't in doc? :O I didn't know about using pure in this 
way.