Re: How can I set Timeout of Socket?

2020-11-14 Thread FreeSlave via Digitalmars-d-learn

On Sunday, 15 November 2020 at 00:05:08 UTC, Marcone wrote:

Socket s = new Socket(AddressFamily.INET, SocketType.STREAM);
s.connect(new InternetAddress("domain.com", 80));

I want that program raise an error if reach for example 30 
seconds of timeout.


Perhaps using Socket.select and SocketSet?

import std.socket;
import std.stdio;
import core.time;

void main()
{
Socket s = new Socket(AddressFamily.INET, SocketType.STREAM);
s.blocking = false;
auto set = new SocketSet(1);
set.add(s);
s.connect(new InternetAddress("dlang.org", 80));
scope(exit) s.close();
Socket.select(null, set, null, dur!"seconds"(10));
if (set.isSet(s))
{
writeln("socket is ready");
}
else
{
writeln("could not connect");
}
}


Re: How format UnixTime to "%Hh:%Mm:%Ss" ?

2020-11-14 Thread Marcone via Digitalmars-d-learn

On Sunday, 15 November 2020 at 03:15:29 UTC, Anonymouse wrote:

On Sunday, 15 November 2020 at 03:08:48 UTC, Marcone wrote:

On Sunday, 15 November 2020 at 02:29:20 UTC, Anonymouse wrote:

On Sunday, 15 November 2020 at 01:04:41 UTC, Marcone wrote:

auto mytime = Clock.currTime().toUnixTime()

writeln(strftime("%Hh:%Mm:%Ss", mytime)); How can I make 
some like this in D?


auto mytime = Clock.currTime;
writefln("%02dh:%02dm:%02ds", mytime.hour, mytime.minute, 
mytime.second);


auto mytimeFromUnix = SysTime.fromUnixTime(1234567890);
writefln("%02dh:%02dm:%02ds", mytimeFromUnix.hour, 
mytimeFromUnix.minute, mytimeFromUnix.second);


https://run.dlang.io/is/TAO6Q9


Your code get wrong hour result: 21hours, only minuts and 
seconds correct.


If you're looking at the run.dlang.io output, the server is 
probably in a different timezone than yours.


Tested locally at 04:14 instead of sleeping:

$ dmd -run mytime.d
04h:14m:06s
00h:31m:30s


See my program for you understand better:
Here is the picture of my program, see red mark: 
https://i.imgur.com/D5nsNHM.png


Re: How format UnixTime to "%Hh:%Mm:%Ss" ?

2020-11-14 Thread Marcone via Digitalmars-d-learn

On Sunday, 15 November 2020 at 03:15:29 UTC, Anonymouse wrote:

On Sunday, 15 November 2020 at 03:08:48 UTC, Marcone wrote:

On Sunday, 15 November 2020 at 02:29:20 UTC, Anonymouse wrote:

On Sunday, 15 November 2020 at 01:04:41 UTC, Marcone wrote:

auto mytime = Clock.currTime().toUnixTime()

writeln(strftime("%Hh:%Mm:%Ss", mytime)); How can I make 
some like this in D?


auto mytime = Clock.currTime;
writefln("%02dh:%02dm:%02ds", mytime.hour, mytime.minute, 
mytime.second);


auto mytimeFromUnix = SysTime.fromUnixTime(1234567890);
writefln("%02dh:%02dm:%02ds", mytimeFromUnix.hour, 
mytimeFromUnix.minute, mytimeFromUnix.second);


https://run.dlang.io/is/TAO6Q9


Your code get wrong hour result: 21hours, only minuts and 
seconds correct.


If you're looking at the run.dlang.io output, the server is 
probably in a different timezone than yours.


Tested locally at 04:14 instead of sleeping:

$ dmd -run mytime.d
04h:14m:06s
00h:31m:30s


I do this:

// Função strftime()
string strftime(string text, int tempo){
int H = to!int(dur!"seconds"(tempo).total!"hours");
int M = to!int(dur!"seconds"(tempo % (60*60)).total!"minutes");
	int S = to!int(dur!"seconds"(tempo % (60*60) % 
60).total!"seconds");
	return text.replace("%H", H.to!string).replace("%M", 
M.to!string).replace("%S", S.to!string);

}


Re: How format UnixTime to "%Hh:%Mm:%Ss" ?

2020-11-14 Thread Anonymouse via Digitalmars-d-learn

On Sunday, 15 November 2020 at 03:08:48 UTC, Marcone wrote:

On Sunday, 15 November 2020 at 02:29:20 UTC, Anonymouse wrote:

On Sunday, 15 November 2020 at 01:04:41 UTC, Marcone wrote:

auto mytime = Clock.currTime().toUnixTime()

writeln(strftime("%Hh:%Mm:%Ss", mytime)); How can I make some 
like this in D?


auto mytime = Clock.currTime;
writefln("%02dh:%02dm:%02ds", mytime.hour, mytime.minute, 
mytime.second);


auto mytimeFromUnix = SysTime.fromUnixTime(1234567890);
writefln("%02dh:%02dm:%02ds", mytimeFromUnix.hour, 
mytimeFromUnix.minute, mytimeFromUnix.second);


https://run.dlang.io/is/TAO6Q9


Your code get wrong hour result: 21hours, only minuts and 
seconds correct.


If you're looking at the run.dlang.io output, the server is 
probably in a different timezone than yours.


Tested locally at 04:14 instead of sleeping:

$ dmd -run mytime.d
04h:14m:06s
00h:31m:30s


Re: How format UnixTime to "%Hh:%Mm:%Ss" ?

2020-11-14 Thread Marcone via Digitalmars-d-learn

On Sunday, 15 November 2020 at 02:29:20 UTC, Anonymouse wrote:

On Sunday, 15 November 2020 at 01:04:41 UTC, Marcone wrote:

auto mytime = Clock.currTime().toUnixTime()

writeln(strftime("%Hh:%Mm:%Ss", mytime)); How can I make some 
like this in D?


auto mytime = Clock.currTime;
writefln("%02dh:%02dm:%02ds", mytime.hour, mytime.minute, 
mytime.second);


auto mytimeFromUnix = SysTime.fromUnixTime(1234567890);
writefln("%02dh:%02dm:%02ds", mytimeFromUnix.hour, 
mytimeFromUnix.minute, mytimeFromUnix.second);


https://run.dlang.io/is/TAO6Q9


I want to convert seconds to hour, minut and second.


Re: How format UnixTime to "%Hh:%Mm:%Ss" ?

2020-11-14 Thread Marcone via Digitalmars-d-learn

On Sunday, 15 November 2020 at 02:29:20 UTC, Anonymouse wrote:

On Sunday, 15 November 2020 at 01:04:41 UTC, Marcone wrote:

auto mytime = Clock.currTime().toUnixTime()

writeln(strftime("%Hh:%Mm:%Ss", mytime)); How can I make some 
like this in D?


auto mytime = Clock.currTime;
writefln("%02dh:%02dm:%02ds", mytime.hour, mytime.minute, 
mytime.second);


auto mytimeFromUnix = SysTime.fromUnixTime(1234567890);
writefln("%02dh:%02dm:%02ds", mytimeFromUnix.hour, 
mytimeFromUnix.minute, mytimeFromUnix.second);


https://run.dlang.io/is/TAO6Q9


Your code get wrong hour result: 21hours, only minuts and seconds 
correct.

I'm using this example:

import std;
import core.thread;

// Functiono strftime()
string strftime(string text, int tempo){
int H = to!int(tempo / (60*60));
int M = to!int(tempo % (60*60) / 60);
int S = to!int(tempo % (60*60) % 60);
	return text.replace("%H", H.to!string).replace("%M", 
M.to!string).replace("%S", S.to!string);

}

void main(){
int starttime = Clock.currTime().toUnixTime();
Thread.sleep(5.seconds);
int endtime = Clock.currTime().toUnixTime() - starttime;
writeln(strftime("%Hh:%Mm:%Ss", endtime));
}


Re: How format UnixTime to "%Hh:%Mm:%Ss" ?

2020-11-14 Thread Anonymouse via Digitalmars-d-learn

On Sunday, 15 November 2020 at 01:04:41 UTC, Marcone wrote:

auto mytime = Clock.currTime().toUnixTime()

writeln(strftime("%Hh:%Mm:%Ss", mytime)); How can I make some 
like this in D?


auto mytime = Clock.currTime;
writefln("%02dh:%02dm:%02ds", mytime.hour, mytime.minute, 
mytime.second);


auto mytimeFromUnix = SysTime.fromUnixTime(1234567890);
writefln("%02dh:%02dm:%02ds", mytimeFromUnix.hour, 
mytimeFromUnix.minute, mytimeFromUnix.second);


https://run.dlang.io/is/TAO6Q9


How format UnixTime to "%Hh:%Mm:%Ss" ?

2020-11-14 Thread Marcone via Digitalmars-d-learn

auto mytime = Clock.currTime().toUnixTime()

writeln(strftime("%Hh:%Mm:%Ss", mytime)); How can I make some 
like this in D?


Re: magically a static member on init?

2020-11-14 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Nov 14, 2020 at 11:20:55PM +, Martin via Digitalmars-d-learn wrote:
> Hi, i do no know if this is intended - but imo this is weird:
> https://run.dlang.io/is/eBje3A
> 
> I expected that `c.a.str == ""` (just like `c.str` is). But instead
> `c.a.str` keeps the value of `b.a.str`.
> 
> Is this intentional? IMO this feels not consistent and its weird when
> a reference leaks into another instance without having declared it
> static member.
[...]

This is a known "feature".  Using a `new` expression as a field
initializer will initialize it once at program startup, and the
reference is copied thereafter into all instances of the class.

If you want separate instances per class instantiation, move the `new`
into the constructor instead.

Yes, it does feel weird, and IMNSHO it's a misfeature. But it is what it
is; if you don't like the semantics, don't use it; always allocate the
field in the class ctor instead.


T

-- 
The fact that anyone still uses AOL shows that even the presence of options 
doesn't stop some people from picking the pessimal one. - Mike Ellis


How can I set Timeout of Socket?

2020-11-14 Thread Marcone via Digitalmars-d-learn

Socket s = new Socket(AddressFamily.INET, SocketType.STREAM);
s.connect(new InternetAddress("domain.com", 80));

I want that program raise an error if reach for example 30 
seconds of timeout.






Re: magically a static member on init?

2020-11-14 Thread Martin via Digitalmars-d-learn
On Saturday, 14 November 2020 at 23:30:58 UTC, Adam D. Ruppe 
wrote:

On Saturday, 14 November 2020 at 23:20:55 UTC, Martin wrote:

Is this intentional?

[...]


alright, thank you! :)


Re: magically a static member on init?

2020-11-14 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 14 November 2020 at 23:20:55 UTC, Martin wrote:

Is this intentional?


In the current language design, yes. For the many users who ask 
this, no.


All static initializers are run at compile time and refer to the 
static data segment - this is consistent across the language.


static x = y; // y is run at compile time

struct A {
int[] a = [1,2,3]; // array built t compile time
}

so.

void main() {
   A a;
   a.a[0] = 5;
   A b;
   b.a[0] == 5; // true!!!
}


Because both actually share the same initial array reference.


And with classes again, same deal. *Constructors* are run for 
each instance. But the rest of it is part of the static 
initializer that is shared


IMO this feels not consistent and its weird when a reference 
leaks into another instance without having declared it static 
member.



Yeah, it is weird and a frequent mistake people make, I almost 
wish it had to be more explicit (I'd be sad if it was removed 
though, I actually use this in places!).


But once you understand it it kinda makes sense.

Note btw that the reference itself is NOT static... just the 
object it refers to. So if you do


obj.a  = new Thing

then it doesn't affect other objects, just this one. But they all 
start off pointing to the same thing.


magically a static member on init?

2020-11-14 Thread Martin via Digitalmars-d-learn

Hi, i do no know if this is intended - but imo this is weird:
https://run.dlang.io/is/eBje3A

I expected that `c.a.str == ""` (just like `c.str` is). But 
instead `c.a.str` keeps the value of `b.a.str`.


Is this intentional? IMO this feels not consistent and its weird 
when a reference leaks into another instance without having 
declared it static member.


Greetigs


presence of function template prevents diagnostic

2020-11-14 Thread kdevel via Digitalmars-d-learn

~~~A.d
module A;
import std.stdio;

void bar (int s) { __PRETTY_FUNCTION__.writeln; }
~~~

~~~foo.d
import std.stdio;
import A;
alias bar = A.bar;

version (X) {
   void bar (T) (T t) { __PRETTY_FUNCTION__.writeln; }
}

void bar (int s) { __PRETTY_FUNCTION__.writeln; }

void main ()
{
   bar (1);
}
~~~

$ dmd -i foo
foo.d(13): Error: foo.bar called with argument types (int) 
matches both:

A.d(4): A.bar(int s)
and:
foo.d(9): foo.bar(int s)


$ dmd -version=X -i foo
$ ./foo
void A.bar(int s)

Is the latter behavior intended or a bug?


Re: .d files without a module statement? Required to be absent?

2020-11-14 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Nov 14, 2020 at 05:55:13PM +, WhatMeWorry via Digitalmars-d-learn 
wrote:
> 
> I was poking around the dmd code just to "learn from the best"

IMNSHO, Phobos is more representative of typical D code than dmd; dmd
code was automatically translated from C++, so a lot of it may still
have a lot of C++-isms that wouldn't be in "native" D code.


> and I came across some files that ended with the .d extension which
> did not have the module statement. (I was under the naive impression
> that all .d files must have a module statement)

No, if there is no module declaration, the module name will be inferred
from the filename.


T

-- 
"You know, maybe we don't *need* enemies." "Yeah, best friends are about all I 
can take." -- Calvin & Hobbes


Re: .d files without a module statement? Required to be absent?

2020-11-14 Thread Paul Backus via Digitalmars-d-learn

On Saturday, 14 November 2020 at 17:55:13 UTC, WhatMeWorry wrote:


I was poking around the dmd code just to "learn from the best" 
and I came across some files that ended with the .d extension 
which did not have the module statement. (I was under the naive 
impression that all .d files must have a module statement)


If a .d file does not have a module statement, the compiler will 
infer the name of the module from the path of the file. So, the 
file `foo/bar.d` will have its module name inferred as `foo.bar`.


There is one exception to this: the file `foo/package.d` will 
have its module name inferred as `foo`, not `foo.package`.


.d files without a module statement? Required to be absent?

2020-11-14 Thread WhatMeWorry via Digitalmars-d-learn



I was poking around the dmd code just to "learn from the best" 
and I came across some files that ended with the .d extension 
which did not have the module statement. (I was under the naive 
impression that all .d files must have a module statement)


However, in the directory:

https://github.com/dlang/dmd/blob/master/samples

I can see many examples where this is not the case. Most of them 
have things like Windows or C structures or calls, etc.


In stark difference, there is

https://github.com/dlang/dmd/tree/master/src/dmd/backend

where all its files seem to have file name = module name strictly 
enforced.


So I guess my question is when is the module statement required?  
Are they recommended but not essential?  Maybe some "Best 
Practices" notation?


the spec sasy "Modules automatically provide a namespace scope 
for their contents..." so maybe my question becomes, when are 
namespace scopes required to be present or required to be absent?





How can execute method in new Thread?

2020-11-14 Thread Marcone via Digitalmars-d-learn

My simple example code:

import std;

struct Fruit {
string name;
this(string name){
this.name = name;
}

void printmyname(){
writeln(this.name);
}

void showname(){
		task!this.printmyname().executeInNewThread(); // Error here!! 
Can not send this.printmyname() tonew thread.

}
}


void main(){
Fruit f = Fruit("Banana");
f.showname();
}


Error: 
D:\dmd2\windows\bin\..\..\src\phobos\std\parallelism.d(516): 
Error: struct `Fruit` does not overload ()