Re: How do I obtain the default hash of a user-defined struct

2014-04-02 Thread dnspies

On Wednesday, 2 April 2014 at 22:07:36 UTC, FreeSlave wrote:
Contents of struct are compared field by field using comparison 
for the type of each field. Dynamic arrays are compared by 
contents. If you want to compare them by pointer use .ptr 
property.


opEquals and opCmp are not about hashing, I believe. They are 
just operators to help when dealing with chaining when 
different objects have same hash (since hash may be not unique)


Thanks, I'll post the other questions in separate threads.


Cannot access static overloaded function?

2014-04-02 Thread Domain

I have 2 modules:

module A;
public class A {
private static void foo() {}
public  static void foo(int) {}
}

module B;
import A;
public class B {
public static void bar() { A.foo(0); }
}

Error: class A.A member foo is not accessible


Re: Why is SysTime.init invalid?

2014-04-02 Thread Jonathan M Davis
On Tuesday, April 01, 2014 03:54:07 Jonathan M Davis wrote:
> On Tuesday, April 01, 2014 05:35:28 ed wrote:
> > OK, lazy me just read the std.satetime article again. It appears
> > the design is for no invalid values and it is currently a known
> > limitation due to CTFE.
> > 
> > ---
> > d_time_nan  There is no equivalent. SysTime.init, which has a null
> > TimeZone object, would be the closest, but once CTFE advances to
> > the point that you can new up class objects with it,
> > SysTime.init's timezone will be LocalTime, so don't rely on
> > SysTime.init being invalid. std.datetime in general tries to
> > avoid having any invalid states for any of its types. It's
> > intended that creating such values be impossible.
> > ---
> > 
> > I would still like to know if there is a way around this so I can
> > have my struct default init.
> 
> You can certainly have a struct with a SysTime member, but there's no way
> for it to use that SysTime without first assigning a valid value to it - at
> least, not without segfaulting when it tries to use the SysTime in any
> context which it would use its timezone member.
> 
> If you really want to have the SysTime be useable in the init value of your
> struct, then you can make it so that each of its member functions which use
> the SysTime member check it for validity first (or have a separate bool
> which indicates whether the SysTime has been assigned to or not), in which
> case, it could assign something like SysTime(0) to it, which would be the
> valid equivalent of SysTime.init.
> 
> Unfortunately, that does add extra overhead, but there isn't any way around
> it if you want to have the SysTime be used without having the user of the
> struct assign to it first. It's pretty much the same boat that SysTime
> itself is in. I could make it so that it checks the timezone for null every
> time it's used and assign LocalTime to it if it's null, but that adds
> constant overhead. My decision was that it was better to just live with the
> fact that SysTime.init is invalid. It's debatable though, as it's a
> trade-off.

Actually, after thinking about this more after someone opened a bug on the 
fact that SysTime.init.toString() segfaults (since the timezone is null)

https://d.puremagic.com/issues/show_bug.cgi?id=12507

I think that I have a solution. It's now possible to default-intialize the 
timezonet to UTC so that SysTime.init is equivalent to SysTime(0, UTC()). It's 
not ideal, because it's not SysTime(0, LocalTime()), and SysTime normally 
defaults to LocalTime, but it will never be possible to default-initialize it 
to LocalTime thanks to the fact that it has to call tzset when it's 
initialized.

You can look my comments in the bug for more details. I've opened a pull 
request with those changes, so the next release will probably have a valid 
SysTime.init, even if it's not SysTime(0, LocalTime()) like would be ideal.

- Jonathan M Davis


Re: unicode console output

2014-04-02 Thread Denis Mezhov

Tnx, chcp 65001 and Lucida font out correct string.


Re: template interface and delegates

2014-04-02 Thread Kenji Hara
On Tuesday, 1 April 2014 at 19:55:05 UTC, Steven Schveighoffer 
wrote:
On Tue, 01 Apr 2014 15:47:42 -0400, anonymous 
 wrote:


Is this bug allready reported? or can somebody who has a 
deeper insight to this report it?


I don't know. I think you should report it. If it's already 
reported, someone will close it as a "duplicate"


-Steve


I filed it.

https://d.puremagic.com/issues/show_bug.cgi?id=12508

Kenji Hara


execute commands in the terminal OSX

2014-04-02 Thread Joel
It says in the Language Reference not to use system and gives 
other options. But the other options don't seem to work. I just 
have code system("clear");


Re: How do I obtain the default hash of a user-defined struct

2014-04-02 Thread FreeSlave
Contents of struct are compared field by field using comparison 
for the type of each field. Dynamic arrays are compared by 
contents. If you want to compare them by pointer use .ptr 
property.


opEquals and opCmp are not about hashing, I believe. They are 
just operators to help when dealing with chaining when different 
objects have same hash (since hash may be not unique)


Re: Sockets between D and C(++) app

2014-04-02 Thread FreeSlave

It's only server. Maybe problem is on client side.

Try this if you are on Linux:

//Linux C client
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main()
{
int sock, res;
struct sockaddr_in addr;
const char* hello;
size_t len;

sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
{
printf("Can't create socket\n");
return -1;
}

addr.sin_family = AF_INET;
addr.sin_port = htons(5432);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");

hello = "Hello from C";
len = strlen(hello);
while (1)
{
res = sendto(sock, hello, len, 0, (struct 
sockaddr*)&addr, sizeof(addr));

if (res <= 0)
{
printf("Can't send\n");
return -1;
}
}
close(sock);
return 0;
}


//D server
import std.socket;
import std.stdio;

int main(string[] args)
{
auto s = new UdpSocket(AddressFamily.INET);

auto addr = new InternetAddress("127.0.0.1", 5432);
s.setOption(SocketOptionLevel.IP, SocketOption.REUSEADDR, 
true);

s.bind(addr);

while (true)
{
ubyte[2048] recv_buf;
int count = s.receiveFrom(recv_buf[]);
char[] test = cast(char[])recv_buf;

writefln("Received: %s\n", test);
}
return 0;
}


Note that you don't need to remove zero-symbol if you don't pass 
it from client.


Re: How to repeat a function call?

2014-04-02 Thread Simen Kjærås

On 2014-04-02 20:48, monarch_dodra wrote:

On Wednesday, 2 April 2014 at 19:54:38 UTC, monarch_dodra wrote:

On Wednesday, 2 April 2014 at 19:02:29 UTC, Simen Kjærås wrote:

Better ideas, please?

--
 Simen


I only skimmed through your post, but have you tried taking a look at
adjoin? Preferably, the version in head? Something like
"adjoin!(repeat!(fun, N))" seems to natively do what you are asking for?

I'll re-read your post later tonight, and if I'm misunderstood, I'll
try to provide a better solution.


Indeed, "assert(adjoin!(Repeat!(3, ()=>a++))() == tuple(0,1,2));" seems
to work for me.

That said, while I think the behavior is *defined*, I'm not sure it's
*specified*.


True, in the case of a++, it's probably a bad idea. In my case, the 
function is uniform(0.0, 1.0, rng), and I'm happy with it simply being 
random.


Thanks a lot!

--
  Simen


Re: How do I obtain the default hash of a user-defined struct

2014-04-02 Thread dnspies

Thanks,
Actually I'm realizing there's a lot I'm unclear about when it 
comes to default comparison, equality, hashing, etc.


If my struct contains a dynamic array, are the contents of the 
array compared by default, or just the pointers/lengths?


Also, when two arrays are compared for content, are their 
pointers compared first in case they happen to be the same so the 
deep comparison can be shortcut?
Also, how do I obtain the default hash of a dynamic array ('s 
contents)?  Is there one?  I assume there must be since 
associative arrays can take string as a key-type.


To make a struct a valid key type, do I need to implement both 
opCmp and opEquals or just one or the other.  It says on the page 
about Associative Arrays: "The implementation may use either 
opEquals or opCmp or both."  Does that mean it uses whichever one 
is user-defined (or both if they're both user-defined)?  Or does 
it mean the user is responsible for defining both?


Also, it says "Care should be taken so that the results of 
opEquals and opCmp are consistent with each other when the 
struct/union objects are the same or not.", certainly this means 
that if a.opEquals(b), then a.opCmp(b) should be 0, but does the 
converse have to be true?


Is there somewhere I can find information about default operator 
implementations and consistency?


Re: How to repeat a function call?

2014-04-02 Thread monarch_dodra

On Wednesday, 2 April 2014 at 19:54:38 UTC, monarch_dodra wrote:

On Wednesday, 2 April 2014 at 19:02:29 UTC, Simen Kjærås wrote:

Better ideas, please?

--
 Simen


I only skimmed through your post, but have you tried taking a 
look at adjoin? Preferably, the version in head? Something like 
"adjoin!(repeat!(fun, N))" seems to natively do what you are 
asking for?


I'll re-read your post later tonight, and if I'm misunderstood, 
I'll try to provide a better solution.


Indeed, "assert(adjoin!(Repeat!(3, ()=>a++))() == tuple(0,1,2));" 
seems to work for me.


That said, while I think the behavior is *defined*, I'm not sure 
it's *specified*.


Re: How to repeat a function call?

2014-04-02 Thread monarch_dodra

On Wednesday, 2 April 2014 at 19:33:28 UTC, w0rp wrote:

auto initTuple(size_t N, alias func)() {
string magic() {
string result = "return tuple(";

foreach(i; 0..N) {
result ~= "func(),";
}

result ~= ");";

return result;
}

mixin(magic());
}


auto initTuple(size_t N, alias func)()
{
return mixin(q{tuple(%-(%s, %))}.format("func".repeat(N)));
}


Re: How do I obtain the default hash of a user-defined struct

2014-04-02 Thread FreeSlave

On Wednesday, 2 April 2014 at 20:14:31 UTC, dnspies wrote:
How can I get the default-hash of a struct I've defined (to be 
used as part of the hash for some containing type)?


UserDefined userDefined;
writeln(typeid(UserDefined).getHash(&userDefined));

Probably there is a better way. I don't like to call typeid for 
this purpose.


How do I obtain the default hash of a user-defined struct

2014-04-02 Thread dnspies
How can I get the default-hash of a struct I've defined (to be 
used as part of the hash for some containing type)?


Re: How to repeat a function call?

2014-04-02 Thread monarch_dodra

On Wednesday, 2 April 2014 at 19:02:29 UTC, Simen Kjærås wrote:

Better ideas, please?

--
  Simen


I only skimmed through your post, but have you tried taking a 
look at adjoin? Preferably, the version in head? Something like 
"adjoin!(repeat!(fun, N))" seems to natively do what you are 
asking for?


I'll re-read your post later tonight, and if I'm misunderstood, 
I'll try to provide a better solution.


Re: How to repeat a function call?

2014-04-02 Thread w0rp
tuples are definitely a compile-time job. You could do something 
like this to build an N tuple by calling a function N many times.


---
import std.typecons;

int foo() {
return 3;
}

auto initTuple(size_t N, alias func)() {
string magic() {
string result = "return tuple(";

foreach(i; 0..N) {
result ~= "func(),";
}

result ~= ");";

return result;
}

mixin(magic());
}

void main(string[] argv) {
enum Tuple!(int, int, int) tup = initTuple!(3, foo);
}

---

This just builds the tuple by building up a mixin string which 
calls the function so many times, with no guarantees about the 
order of arguments. If you have side-effects in your function 
though, you probably want to move it runtime anyway and use a 
range with the 'repeat' function, etc.


How to repeat a function call?

2014-04-02 Thread Simen Kjærås
I'm trying to create a function that repeats a function call N times. 
The exact use case is generating an N-dimensional tuple:


import std.typetuple;
import std.typecons;

template Repeat(size_t n, T...) {
static if (n == 1) {
alias Repeat = T;
} else static if (n) {
alias Repeat = TypeTuple!(Repeat!(n /2, T), Repeat!((n +1)/2, T));
} else {
alias Repeat = TypeTuple!();
}
}

auto fun1(size_t dim, T)(lazy T fn) {
return tuple(Repeat!(dim, fn));
}

auto fun2(size_t dim, alias fn)() {
return tuple(Repeat!(dim, fn));
}

void main() {
int a = 0;
assert(fun1!3(a++) == tuple(0,1,2));
//assert(fun2!(3, ()=>a++) == tuple(0,1,2));
}

Now, the call to fun1 works great. But I'd like to specify fn at 
compile-time, thus doing something more like fun2. fun2 of course, does 
not work (else why would I ask?).


I tried staticMap'ing a template that calls its parameter over the 
result of Repeat in Fun2, but that did not work:


template call(alias fn) {
alias call = TypeTuple!(fn());
}

auto fun3(size_t dim, alias fn)() {
return tuple(staticMap!(call, Repeat!(dim, fn)));
}

fun3 ends up trying to evaluate the function call at compile-time, and 
fails because a++ can't be executed until run-time.


Better ideas, please?

--
  Simen


Re: When this will be freed?

2014-04-02 Thread Benjamin Thaut

Am 02.04.2014 17:57, schrieb Andrea Fontana:

On Wednesday, 2 April 2014 at 15:53:52 UTC, Jacob Carlborg wrote:

On 2014-04-02 17:45, Andrea Fontana wrote:


auto example(char* test) { return toStringz(to!string(test) ~ "
world!"); }

When that return string will be freed?


When there is no reference left to the string, the garbage collector
is free to collect it when it chooses to.



That's expected.


What about:

extern(C) auto example()


Same as above, extern(C) does not change how memory is collected. If
it is a C function, then it depends entirely on that particular function.


I mean: if it is an exported function (of a shared library) what
happens? There's no reference kept anywhere (in D). So if I'm right it
could be freed immediatly by GC. Right?



If you pass that string to a C function, there is a reference on the 
stack. So this string will not be freed until that C-function returns. 
If that C-Function returns, it is very likely however that this was the 
only reference and the string will be freed the next time the garbage 
collector runs.


Kind Regards
Benjamin Thaut


Re: When this will be freed?

2014-04-02 Thread Andrea Fontana

On Wednesday, 2 April 2014 at 15:53:52 UTC, Jacob Carlborg wrote:

On 2014-04-02 17:45, Andrea Fontana wrote:

auto example(char* test) { return toStringz(to!string(test) ~ 
" world!"); }


When that return string will be freed?


When there is no reference left to the string, the garbage 
collector is free to collect it when it chooses to.




That's expected.


What about:

extern(C) auto example()


Same as above, extern(C) does not change how memory is 
collected. If it is a C function, then it depends entirely on 
that particular function.


I mean: if it is an exported function (of a shared library) what 
happens? There's no reference kept anywhere (in D). So if I'm 
right it could be freed immediatly by GC. Right?




Re: When this will be freed?

2014-04-02 Thread John Colvin

On Wednesday, 2 April 2014 at 15:45:06 UTC, Andrea Fontana wrote:


auto example(char* test) { return toStringz(to!string(test) ~ " 
world!"); }


When that return string will be freed?

What about:

extern(C) auto example()

?


to!string allocates on the GC heap when given a char* (it has to, 
in order to safely produce immutable data in the string).


It will be freed by the first garbage collection ocurring after 
all references to that memory are dead.


Re: When this will be freed?

2014-04-02 Thread Jacob Carlborg

On 2014-04-02 17:45, Andrea Fontana wrote:


auto example(char* test) { return toStringz(to!string(test) ~ " world!"); }

When that return string will be freed?


When there is no reference left to the string, the garbage collector is 
free to collect it when it chooses to.



What about:

extern(C) auto example()


Same as above, extern(C) does not change how memory is collected. If it 
is a C function, then it depends entirely on that particular function.


--
/Jacob Carlborg


When this will be freed?

2014-04-02 Thread Andrea Fontana


auto example(char* test) { return toStringz(to!string(test) ~ " 
world!"); }


When that return string will be freed?

What about:

extern(C) auto example()

?





Re: unicode console output

2014-04-02 Thread Jos van Uden

On 2-4-2014 15:38, Denis Mezhov wrote:

On Wednesday, 2 April 2014 at 12:51:57 UTC, bearophile wrote:

Denis Mezhov:


How to out unicode cyrillic string to console?


Try this command on the command line:

chcp 65001

Bye,
bearophile


chcp 65001 dont'work


start.bat

mode con cols=150 lines=50
chcp 65001
%Path%\Minesweeper\Debug\Mi.exe

don't out correct string


Do you have a Unicode font selected in the dosbox properties,
like Lucida Console ?


Re: unicode console output

2014-04-02 Thread Denis Mezhov

On Wednesday, 2 April 2014 at 12:51:57 UTC, bearophile wrote:

Denis Mezhov:


How to out unicode cyrillic string to console?


Try this command on the command line:

chcp 65001

Bye,
bearophile


chcp 65001 dont'work


start.bat

mode con cols=150 lines=50
chcp 65001
%Path%\Minesweeper\Debug\Mi.exe

don't out correct string


Re: core.atomic and -profile switch

2014-04-02 Thread bearophile

Saurabh Das:


I see. I wasn't sure - hence I asked.


https://d.puremagic.com/issues/show_bug.cgi?id=11471



In general, how do I check if this is a known issue?


Search in Bugzilla and/or ask to people.

Bye,
bearophile


Re: core.atomic and -profile switch

2014-04-02 Thread Saurabh Das


I see. I wasn't sure - hence I asked.

In general, how do I check if this is a known issue?

Thanks,
Saurabh

On Wednesday, 2 April 2014 at 11:19:16 UTC, bearophile wrote:

Saurabh Das:


With error:
/usr/include/dmd/druntime/import/core/atomic.d(910): Error: 
asm statements are assumed to throw


Is there a workaround for this? I have a decent sized codebase 
which I wish to profile for hotspots - but it won't compile 
with '-profile'.


Isn't this a recent regression?

Bye,
bearophile




Re: unicode console output

2014-04-02 Thread MGW

On Wednesday, 2 April 2014 at 12:47:06 UTC, Denis Mezhov wrote:

I'm trying out to windows console unicode latin string

writeln("aaabbb");
console out: aaabbb

trying out to console unicode cyrillic string
writeln("бббггг");
console out: ╨│╨│╨│╨▒╨▒╨▒

How to out unicode cyrillic string to console?

-

On the website http://dlang.ru all the details are written about 
re-encode the video into Cyrillic.


Re: unicode console output

2014-04-02 Thread bearophile

Denis Mezhov:


How to out unicode cyrillic string to console?


Try this command on the command line:

chcp 65001

Bye,
bearophile


unicode console output

2014-04-02 Thread Denis Mezhov

I'm trying out to windows console unicode latin string

writeln("aaabbb");
console out: aaabbb

trying out to console unicode cyrillic string
writeln("бббггг");
console out: ╨│╨│╨│╨▒╨▒╨▒

How to out unicode cyrillic string to console?


Re: Signature of main [was Sockets between D and C(++) app]

2014-04-02 Thread Ola Fosheim Grøstad

On Wednesday, 2 April 2014 at 08:55:23 UTC, Russel Winder wrote:

The real signature of main in C/C++ is, I believe:

int main(int, char**, char**)


I believe in C it is:

int main(void)
int main(int,char**)

or implementation defined (e.g. the third env pointer in the main 
signature from unix)


But the actual entry point is system dependent and can be 
specified as a link option? On linux "_start" or something like 
that.


Re: Signature of main [was Sockets between D and C(++) app]

2014-04-02 Thread Andrej Mitrovic
On 4/2/14, Dicebot  wrote:
> D main != C main, latter is implemented in D runtime to call the
> former. 0 will be also returned by latter, not the former.

Actually, the compiler injects a return statement in D's main.

It generates the actual C main function (unless WinMain/DllMain is
provided), which calls another special D runtime init function, which
itself calls the D main function (which itself has the injected return
statement unless it's already an int return).

It's quite complicated, but it's all open-source so you can inspect it.


Re: Signature of main [was Sockets between D and C(++) app]

2014-04-02 Thread Dicebot

On Wednesday, 2 April 2014 at 08:55:23 UTC, Russel Winder wrote:

On Wed, 2014-04-02 at 00:34 +, bearophile wrote:

Alexandre L.:



> int main(string[] args)
> {

If you don't need args, then I suggest to not put it as main 
argument. So probably this is better (note no int nor return, 
in D they are not needed):


void main() {
...
}


I am not convinced by this argument. The return value (aka exit 
code) is
always present, if you ignore this by having a void main, it 
will return
0, i.e. main is not a void function even if you claim it is. As 
for
ignoring the parameters, this is making use of the fact that 
main is the
only function where you do not have to present the correct 
signature.

Perhaps this exception should be removed.

The real signature of main in C/C++ is, I believe:

int main(int, char**, char**)

what is the real signature in D?


D main != C main, latter is implemented in D runtime to call the 
former. 0 will be also returned by latter, not the former. Also 
exception will result in >0 status code even if return type is 
void. It effectively just says that you won't manage status code 
manually and allows runtime to take care of it.


Re: Signature of main [was Sockets between D and C(++) app]

2014-04-02 Thread bearophile

Russel Winder:


what is the real signature in D?


The _real_ signature of main() is flexible, you can use:

void main()
int main(in string[] args) pure nothrow

D allows you to omit the args if you don't need them, returns 0 
if you don't it, and it can be pure/nothrow/@safe as desired.




Perhaps this exception should be removed.


This special cases cause zero bugs and zero problems, so there is 
no chance to change this, even if we want (and I don't want).


Bye,
bearophile


Re: core.atomic and -profile switch

2014-04-02 Thread bearophile

Saurabh Das:


With error:
/usr/include/dmd/druntime/import/core/atomic.d(910): Error: asm 
statements are assumed to throw


Is there a workaround for this? I have a decent sized codebase 
which I wish to profile for hotspots - but it won't compile 
with '-profile'.


Isn't this a recent regression?

Bye,
bearophile


core.atomic and -profile switch

2014-04-02 Thread Saurabh Das

Hello

For this test program ('test.d'):

import core.atomic;
int func1(shared int a)
{
return atomicLoad(a);
}

These invocations of dmd succeed:
1. dmd -main test.d
2. dmd -main -debug test.d
3. dmd -main -release test.d

But this one fails:
dmd -main -profile test.d

With error:
/usr/include/dmd/druntime/import/core/atomic.d(910): Error: asm 
statements are assumed to throw


Is there a workaround for this? I have a decent sized codebase 
which I wish to profile for hotspots - but it won't compile with 
'-profile'.


Warm Regards,
Saurabh



Re: Trying Multiple Aggregate reduce

2014-04-02 Thread monarch_dodra

On Wednesday, 2 April 2014 at 09:25:53 UTC, Nordlöw wrote:
so happens to support this. So I added your code to the test 
cases:


Great!

BTW: Why is static qualifier needed on definition of 
minmaxElement() in the unittest?


Whenever you declare something in a nested context, it may or may 
not have a hidden context pointer, depending on the type, and the 
implementation.


"static", in this context, ensures this does not happen. It's not 
actually *needed* in this context though. It's more of a matter 
of style.


In your example, the function was declared in global context, so 
the "static" would have been gratuitous.


Re: Trying Multiple Aggregate reduce

2014-04-02 Thread Nordlöw
so happens to support this. So I added your code to the test 
cases:


Great!

BTW: Why is static qualifier needed on definition of 
minmaxElement() in the unittest?


Re: Signature of main [was Sockets between D and C(++) app]

2014-04-02 Thread Russel Winder
On Wed, 2014-04-02 at 00:34 +, bearophile wrote:
> Alexandre L.:

> > int main(string[] args)
> > {
> 
> If you don't need args, then I suggest to not put it as main 
> argument. So probably this is better (note no int nor return, in 
> D they are not needed):
> 
> void main() {
> ...
> }

I am not convinced by this argument. The return value (aka exit code) is
always present, if you ignore this by having a void main, it will return
0, i.e. main is not a void function even if you claim it is. As for
ignoring the parameters, this is making use of the fact that main is the
only function where you do not have to present the correct signature.
Perhaps this exception should be removed.

The real signature of main in C/C++ is, I believe:

int main(int, char**, char**)

what is the real signature in D? 

-- 
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