Re: How to test that the IP port is reachable?

2019-04-05 Thread FrankLike via Digitalmars-d-learn

On Saturday, 6 April 2019 at 03:24:04 UTC, FrankLike wrote:

Hi,everyone,...


Do you have some better code than this?

import std.stdio;
import std.socket;

void main()
{
testIPPort();
}
bool testIPPort()
{
try
{
		auto results = 
getAddressInfo("127.0.0.1",AddressInfoFlags.NUMERICHOST);

auto sock = new Socket(results[0]);
auto address = getAddress("127.0.0.1",8080);
sock.connect(address[0]);
writeln(address," connect ok");
return true;
}
catch(SocketException e)
{   return false;
}
}
---
Thanks.


Re: Block statements and memory management

2019-04-05 Thread Murilo via Digitalmars-d-learn

On Saturday, 16 March 2019 at 15:53:26 UTC, Johan Engelen wrote:

On Saturday, 16 March 2019 at 03:47:43 UTC, Murilo wrote:
Does anyone know if when I create a variable inside a scope as 
in

{int a = 10;}
it disappears complete from the memory when the scope 
finishes? Or does it remain in some part of the memory? I am 
thinking of using scopes to make optimized programs that 
consume less memory.


Others have made good points in this thread, but what is 
missing is that indeed scopes _can_ be used beneficially to 
reduce memory footprint.

-Johan


I would like to thank everyone for your help, those informations 
were very helpful.




How to test that the IP port is reachable?

2019-04-05 Thread FrankLike via Digitalmars-d-learn

Hi,everyone,

How to test that the IP port is reachable?

In C,you can do this like that, what should I do in D?

/* C Code*/
https://blog.csdn.net/zhangyingchuang/article/details/51957552

#include 
#include 
#include 
#include 
#include     /* inet(3) functions */

#define bool int
#define false 0
#define true 1

bool checkIPandPort(const char* ip, const char* port) {
    struct sockaddr_in addr;
    int fd = socket(AF_INET, SOCK_STREAM, 0);

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);

    if (connect(fd, (struct sockaddr *) &addr, sizeof(struct 
sockaddr)) < 0) {

        printf("connect error \n");
        return false;
    }
    return true;
}
--
But how to do it in D?
Thanks.


Re: Undescriptive linker error. (bug?)

2019-04-05 Thread Sjoerd Nijboer via Digitalmars-d-learn

On Friday, 5 April 2019 at 22:08:50 UTC, Adam D. Ruppe wrote:

Weird combination of cases that maybe should be illegal.


It errors with the highly descriptive errormessage:

app.obj(app)
 Error 42: Symbol Undefined __D8mymodule3BarFZ3fooMFZCQx3Foo
Error: linker exited with status 1


Re: Undescriptive linker error. (bug?)

2019-04-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 5 April 2019 at 21:59:35 UTC, Sjoerd Nijboer wrote:

Foo Bar()
{
Foo foo();


It looks like the compiler is treating that as a function 
prototype without a body (that just happens to be nested in 
another function).



return foo;


And then, this foo is given the optional parens treatment, as if 
it was a call to foo() of the given function, which is why the 
linker complains.


Weird combination of cases that maybe should be illegal.



Undescriptive linker error. (bug?)

2019-04-05 Thread Sjoerd Nijboer via Digitalmars-d-learn

module mymodule;

class Foo{}

Foo Bar()
{
Foo foo();

return foo;
}

int main()
{
auto foo = Bar();

return 0;
}

This code doesn't compile with a linker error that there's a 
missing symbol for `Foo Bar()` on windows.

After all, `Foo foo();` isn't legitimate D.
But why does it return a linker error?
shouldn't it give an error that is more descriptive about a class 
instance being wrong?

I feel like this would be a common thing people try to write.
Especially in templates this would become difficult to narrow 
down.


Re: Using opOpAssign, cannot assign sequence

2019-04-05 Thread Paul Backus via Digitalmars-d-learn

On Friday, 5 April 2019 at 13:59:27 UTC, Alex wrote:

class X(T)
void opOpAssign(string op)(T d)

If T has more than length of one then

x += 

We can work around this but it seems to me that we should be 
able to get it to work in some way


x += Alias!(a,b,c)

fails to package it up as do all other things I have tried.


Works for me if you make opOpAssign a variadic template:

void opOpAssign(string op, Args...)(Args args)

Full example: https://run.dlang.io/is/dPk3BN


Re: Using opOpAssign, cannot assign sequence

2019-04-05 Thread ag0aep6g via Digitalmars-d-learn

On 05.04.19 16:00, Alex wrote:
I was thinking using tuple would work(of course is longer than Add but 
would allow for a more general approach, it would require automatic 
unpacking though and so doesn't work.


`tuple` works for me:


import std.typecons: tuple;

class X(T ...)
{
void opOpAssign(string op)(T d) {}
}

void main()
{
auto x = new X!(int, float, string);
int a = 42;
float b = 4.2;
string c = "foo";
x += tuple(a, b, c);
}



== comparison of string literals, and their usage

2019-04-05 Thread diniz via Digitalmars-d-learn

Hello,

Since literal strings are interned (and immutable), can I count on the fact that 
they are compared (==) by pointer?


Context: The use case is a custom lexer for a custom language. I initially 
wanted to represent lexeme classes by a big enum 'LexClass'. However, this makes 
me write 3 times all constant lexemes (keywords and keysigns):

1- in the enum of lexeme classes
2- in an array of constants (for the contant-scanning func)
3- in an associative array mapping constants to their classes
However, if literal strings are compared by equality, then they are kinds of 
Scheme or Ruby symbols: read enum values representing *cases*, which is exactly 
what I need. I would thus use the constants' strings themselves as lexeme 
classes... the parser would not be slown down.


What do you think?
--
diniz {la vita e estranj}


Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-05 Thread Adam D. Ruppe via Digitalmars-d-learn
BTW `T.stringof` is usually a bug waiting to happen. You are 
using mixin in a lot of places where you shouldn't be and this is 
going to lead to name conflicts, import problems, and more. Just 
use `T`.


If you need a member, use __traits(getMember, T, "name").


Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 5 April 2019 at 14:38:21 UTC, Alex wrote:

No one has a clue about this?


Your code has a lot of layers to unfold, but instead let me just 
show you a working example and then maybe you can fix your own 
code:


---
class A {
void foo(int a) {}
void foo(int b, int c) {}
}

void main() {
import std.stdio;
foreach(overload; __traits(getOverloads, A, "foo")) {
writeln(typeof(overload).stringof);
static if(is(typeof(overload) Params == __parameters))
static foreach(idx, _; Params) {{
alias param = Params[idx .. idx + 1];
writeln("\t", __traits(identifier, param));
}}
}
}
---


I don't know if your bug is in your code or in std.traits or 
what, but this example works and prints out


void(int a)
a
void(int b, int c)
b
c


The one bizarre thing is the `param = Params[idx .. idx + 1]` 
bit. I wrote a little about this for parameter attributes too


http://dpldocs.info/this-week-in-d/Blog.Posted_2019_02_11.html#how-to-get-uda-on-a-function-param

but it also applies to getting the identifier out.


Re: gtkDcoding Blog: Post #0024 - Switch and LightSwitch

2019-04-05 Thread Ron Tarrant via Digitalmars-d-learn

On Friday, 5 April 2019 at 14:05:55 UTC, Russel Winder wrote:
I get the feeling this blog is going to become an important 
tutorial resource for people wanting to do GtkD stuff with D. 
Given D and GtkD is currently the best way of writing Gtk+ 
applications, this blog is a great resource.


Thanks for the kind words, Russell. I certainly won't mind if 
that's where this goes. It's nice to feel like I'm contributing 
something of value.





Re: template with enum parameter doesn't compile

2019-04-05 Thread Sjoerd Nijboer via Digitalmars-d-learn

On Friday, 5 April 2019 at 14:52:05 UTC, lithium iodate wrote:
You are just having a little issue with operator precedence 
there. Your code attempts to get the member `A` from 
`MyClass!MyEnum`, if you add braces around it, it'll work just 
fine `MyClass!(MyEnum.A)`.



That's really funny acutally.
It works. Thank you!



Re: template with enum parameter doesn't compile

2019-04-05 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 5 April 2019 at 14:47:42 UTC, Sjoerd Nijboer wrote:
So the following code doesn't compile for some reason, and I 
can't figure out why.


enum MyEnum { A, B, C }

class MyClass(MyEnum myEnum)
{
/*...*/
}

int main()
{
MyClass!MyEnum.A a;
}

The error:  Error: template instance `MyClass!(MyEnum)` does 
not match template declaration `MyClass(MyEnum myEnum)`

pops up, no matter what I do.


You wrote:


MyClass!MyEnum.A a;


Which is equivalent to


MyClass!(MyEnum).A a;


What you want is

MyClass!(MyEnum.A) a;



Re: template with enum parameter doesn't compile

2019-04-05 Thread lithium iodate via Digitalmars-d-learn

On Friday, 5 April 2019 at 14:47:42 UTC, Sjoerd Nijboer wrote:
So the following code doesn't compile for some reason, and I 
can't figure out why.


The error:  Error: template instance `MyClass!(MyEnum)` does 
not match template declaration `MyClass(MyEnum myEnum)`

pops up, no matter what I do.
I'm quite puzzled actually


You are just having a little issue with operator precedence 
there. Your code attempts to get the member `A` from 
`MyClass!MyEnum`, if you add braces around it, it'll work just 
fine `MyClass!(MyEnum.A)`.


template with enum parameter doesn't compile

2019-04-05 Thread Sjoerd Nijboer via Digitalmars-d-learn
So the following code doesn't compile for some reason, and I 
can't figure out why.


enum MyEnum { A, B, C }

class MyClass(MyEnum myEnum)
{
/*...*/
}

int main()
{
MyClass!MyEnum.A a;
}

The error:  Error: template instance `MyClass!(MyEnum)` does not 
match template declaration `MyClass(MyEnum myEnum)`

pops up, no matter what I do.
I'm quite puzzled actually


Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-05 Thread Alex via Digitalmars-d-learn

No one has a clue about this?


Re: gtkDcoding Blog: Post #0024 - Switch and LightSwitch

2019-04-05 Thread Russel Winder via Digitalmars-d-learn
I get the feeling this blog is going to become an important tutorial resource
for people wanting to do GtkD stuff with D. Given D and GtkD is currently the
best way of writing Gtk+ applications, this blog is a great resource.

On Fri, 2019-04-05 at 13:44 +, Ron Tarrant via Digitalmars-d-learn wrote:
> Since the forum seems to have trouble with replies to existing 
> posts, for now I'll be doing a separate post for each. Hope that 
> doesn't get anyone's nose out of joint.
> 
> Anyway...
> 
> Today we explore the GTK Switch widget with two examples, a 
> simple one and a complex one. Enjoy.
-- 
Russel.
===
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk



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


Re: Using opOpAssign, cannot assign sequence

2019-04-05 Thread Alex via Digitalmars-d-learn

On Friday, 5 April 2019 at 13:59:27 UTC, Alex wrote:

class X(T)
void opOpAssign(string op)(T d)

If T has more than length of one then

x += 

We can work around this but it seems to me that we should be 
able to get it to work in some way


x += Alias!(a,b,c)

fails to package it up as do all other things I have tried.

void Add(Ts d) { opOpAssign!("+")(d); }

Then x.Add(a,b,c) works fine.

But of course defeats the entire purpose of opOpAssigns short 
hand notation.






I was thinking using tuple would work(of course is longer than 
Add but would allow for a more general approach, it would require 
automatic unpacking though and so doesn't work.


Using opOpAssign, cannot assign sequence

2019-04-05 Thread Alex via Digitalmars-d-learn

class X(T)
void opOpAssign(string op)(T d)

If T has more than length of one then

x += 

We can work around this but it seems to me that we should be able 
to get it to work in some way


x += Alias!(a,b,c)

fails to package it up as do all other things I have tried.

void Add(Ts d) { opOpAssign!("+")(d); }

Then x.Add(a,b,c) works fine.

But of course defeats the entire purpose of opOpAssigns short 
hand notation.








Re: gtkDcoding Blog: Post #0024 - Switch and LightSwitch

2019-04-05 Thread Ron Tarrant via Digitalmars-d-learn

On Friday, 5 April 2019 at 13:44:35 UTC, Ron Tarrant wrote:
Since the forum seems to have trouble with replies to existing 
posts, for now I'll be doing a separate post for each. Hope 
that doesn't get anyone's nose out of joint.


Anyway...

Today we explore the GTK Switch widget with two examples, a 
simple one and a complex one. Enjoy.


I think I can get away with one repost so, here's the link:

http://gtkdcoding.com/2019/04/05/0024-switch-and-light-switch.html


gtkDcoding Blog: Post #0024 - Switch and LightSwitch

2019-04-05 Thread Ron Tarrant via Digitalmars-d-learn
Since the forum seems to have trouble with replies to existing 
posts, for now I'll be doing a separate post for each. Hope that 
doesn't get anyone's nose out of joint.


Anyway...

Today we explore the GTK Switch widget with two examples, a 
simple one and a complex one. Enjoy.