Difference between is and ==

2014-02-04 Thread Suliman

What difference between
if ((x = stdin.readln().chomp) is q)
and
if ((x = stdin.readln().chomp) == q)

?


Re: Difference between is and ==

2014-02-04 Thread Martijn Pot

On Tuesday, 4 February 2014 at 08:08:30 UTC, Suliman wrote:

What difference between
if ((x = stdin.readln().chomp) is q)
and
if ((x = stdin.readln().chomp) == q)

?


My interpretation of tdpl p57:

'is' compares for alias equality for arrays and classes.
Otherwise they are the same.


Re: Difference between is and ==

2014-02-04 Thread Suliman

My interpretation of tdpl p57:

'is' compares for alias equality for arrays and classes.
Otherwise they are the same.


So should next code have same behavior if I will use is instead 
of ==


import std.stdio;
import std.string;

void main()
{
getchar();
}

void getchar()
{
string x;
if ((x = stdin.readln().chomp) == q)
writeln(it's is q);
else
writeln(Not q);
}

In case I am using is, I have never get first if expression is 
true.


Re: Difference between is and ==

2014-02-04 Thread Martijn Pot

On Tuesday, 4 February 2014 at 08:25:18 UTC, Suliman wrote:

My interpretation of tdpl p57:

'is' compares for alias equality for arrays and classes.
Otherwise they are the same.


So should next code have same behavior if I will use is instead 
of ==


import std.stdio;
import std.string;

void main()
{
getchar();
}

void getchar()
{
string x;
if ((x = stdin.readln().chomp) == q)
writeln(it's is q);
else
writeln(Not q);
}

In case I am using is, I have never get first if expression is 
true.


My guess is the following:
string is an immutable(char)[]. As string is an array, 'is' 
checks for alias equality. x is not an alias for the (unnamed?) 
string literal q.


Re: Difference between is and ==

2014-02-04 Thread bearophile

Suliman:


What difference between
if ((x = stdin.readln().chomp) is q)
and
if ((x = stdin.readln().chomp) == q)

?


is performs a raw comparison of just the values, and the value 
of a string is its ptr and length field. While == compares 
their contents. So you want to use == here because you are 
interested to see if x contains the char 'q', because while their 
lengths could be equal, their ptr is surely different.


Bye,
bearophile


Get struct template types

2014-02-04 Thread ed

Hi,

given a struct like so:

struct S(alias N, T) {...}

is there a way to get the template parameters of S? Something 
like:


S.typetuple[0] == N,
S.typetuple[1] == T

I've had a look at std.typecons and std.typetuple but I don't see 
what I'm missing something and cannot see a way to do the above.


Thanks,
ed


Re: Get struct template types

2014-02-04 Thread evilrat

On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:

Hi,

given a struct like so:

struct S(alias N, T) {...}

is there a way to get the template parameters of S? Something 
like:


S.typetuple[0] == N,
S.typetuple[1] == T

I've had a look at std.typecons and std.typetuple but I don't 
see what I'm missing something and cannot see a way to do the 
above.


Thanks,
ed


typeof(N).stringof will return type of N as string
for details read http://dlang.org/expression.html


Re: 3d vector struct

2014-02-04 Thread Francesco Cattoglio

On Monday, 3 February 2014 at 20:10:59 UTC, Brenton wrote:

6) Any other comments or suggestions?


I know that the I'm learning the language factor plays a huge 
role, but after you are done studying your vector implementation, 
I think you could forget about it and use the ones provided by 
other libraries :P


If you didn't knew about it, DUB is a marvelous software that 
gives you quick access to lots of nice libraries. EG: one you 
might be interested in is http://code.dlang.org/packages/gl3n

Another one *might* be gfm: http://code.dlang.org/packages/gfm

I'm also wondering where the hell did I put my raytracer code I 
did ages ago...


Re: Get struct template types

2014-02-04 Thread evilrat

On Tuesday, 4 February 2014 at 09:39:48 UTC, evilrat wrote:

On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:

Hi,

given a struct like so:

struct S(alias N, T) {...}

is there a way to get the template parameters of S? Something 
like:


S.typetuple[0] == N,
S.typetuple[1] == T

I've had a look at std.typecons and std.typetuple but I don't 
see what I'm missing something and cannot see a way to do the 
above.


Thanks,
ed


typeof(N).stringof will return type of N as string
for details read http://dlang.org/expression.html


oops sorry, it's for another people.

what are you trying to achieve? when passing types as template 
args it is still types, so again typeof should give you type for 
variables(in such cases there is typeof), otherwise type is type. 
also, afaik it is not possible to store types in tuple, but i 
think you can store variables in tuple and retrieve their types 
at runtime


Decorators, Annotations, Macros, AST transforms…

2014-02-04 Thread Russel Winder
…choose you favourite term.

Following on from trying to use D to write CPython extensions without
using PyD: entry points will be functions:

extern(C) type name() {
  initializeIfNoAlreadyDone();
  …
}

This immediately looks like a job for a Python decorator.

@pythonentry type name() {
  …
}

From searching the Web there is DIP6 and DIP50, but what is the current
official way of doing this source to source transform in D just now.

Thanks.

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



Re: Python calling D

2014-02-04 Thread Russel Winder
On Tue, 2014-02-04 at 07:13 +, Artem Tarasov wrote:
 On Sunday, 2 February 2014 at 15:31:30 UTC, Russel Winder wrote:
  result is:
 
  | LD_LIBRARY_PATH=. python execute.py
  Segmentation fault
 
 You should call Runtime.initialize() prior to calling any other D 
 functions.

Hummm… obvious once pointed out :-) Thanks for doing exactly that.

The question is how to get this run. I tried a module initializer:

static this() {
  Runtime.initialize();
}

but this module never actually gets loaded per se, so that is never
going to work. So this means calling a C linkage function to do the
initialization in every entry point:

import core.runtime: Runtime;
import std.stdio: writeln;

auto initialized = false;

extern (C) void initializeIfNotAlreadyDone() {
  if (!initialized) {
Runtime.initialize();
initialized = true;
  }
}

extern(C) {
  void sayHello() {
initializeIfNotAlreadyDone();
writeln(Hello World.);
  }
}

works a treat:

scons: Building targets ...
dmd -I. -fPIC -c -fPIC -ofhelloWorld.o helloWorld.d
dmd -shared -defaultlib=libphobos2.so -oflib_helloWorld.so helloWorld.o 
-fPIC -L-L.
LD_LIBRARY_PATH=. python execute.py
Hello World.
scons: done building targets.

So D extensions to CPython are possible without PyD!

Now to continue with PyD anyway so as to compare and contrast.

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



Re: Get struct template types

2014-02-04 Thread evilrat

On Tuesday, 4 February 2014 at 09:58:53 UTC, ed wrote:

On Tuesday, 4 February 2014 at 09:46:01 UTC, evilrat wrote:

On Tuesday, 4 February 2014 at 09:39:48 UTC, evilrat wrote:

On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:

Hi,

given a struct like so:

struct S(alias N, T) {...}

is there a way to get the template parameters of S? 
Something like:


S.typetuple[0] == N,
S.typetuple[1] == T

I've had a look at std.typecons and std.typetuple but I 
don't see what I'm missing something and cannot see a way to 
do the above.


Thanks,
ed


typeof(N).stringof will return type of N as string
for details read http://dlang.org/expression.html


oops sorry, it's for another people.

what are you trying to achieve? when passing types as template 
args it is still types, so again typeof should give you type 
for variables(in such cases there is typeof), otherwise type 
is type. also, afaik it is not possible to store types in 
tuple, but i think you can store variables in tuple and 
retrieve their types at runtime



I am trying to set up an isS, isSOfN or isSOfT so I can create 
functions that allow restricted types of S based on all, or 
only one of the template parameters.


Like so (pseudo-D code)
---
struct S(alias N, T) {}

//
// Hopefully it is clear what I am trying to do here
enum isSOfN = template(alias N,STYPE) {
static assert(N == STYPET.typetuple[0]);
}

enum isSOfT = template(T,STYPE) {
static assert(T == STYPET.typetuple[1]);
}
//


void f(T)(T t) if(isSOfN!(3, T)) { // allow S with N=3 and any 
T (see below)

}

...
...

auto s3i = S!(3,int);
auto s3f = S!(3,float);

s3i.f(); // Succeeds
s3f.f(); // Succeeds

auto S4i = S!(4,int);
s4i.f(); // compile time error
--

I'm looking into T.stringof now, I might be able to get it 
working with some compile-time string magic of D :)


If this is a bad way to go about it please let me know. I'm 
trying to learn idiomatic D as I go.


Thanks,
ed


for this simple case static if is pretty straightforward, but 
then if only purpose is to construct type it is better use 
regular generic function and typecons i think.


--
import std.stdio;

struct S(alias N, T)
{
  T x;
  void f()
  {
 static if(N==3)
 {
writeln(n = 3, type = , T.stringof);
 }
 else static if (N == 2)
 {
writeln(n = 2, type = , T.stringof);
 }
 else
static assert(0, NO!);
  }
}

void main()
{
S!(3, int) a; // ok
S!(2, float) b; // ok
S!(4, double) c; // oops
a.f();
b.f();
}

--


Re: Get struct template types

2014-02-04 Thread Jakob Ovrum

On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:

Hi,

given a struct like so:

struct S(alias N, T) {...}

is there a way to get the template parameters of S? Something 
like:


S.typetuple[0] == N,
S.typetuple[1] == T

I've had a look at std.typecons and std.typetuple but I don't 
see what I'm missing something and cannot see a way to do the 
above.


Thanks,
ed


Use type deduction:

---
struct S(size_t n, T) {}

enum isS(T) = is(T == S!(n, U), size_t n, U);

template isS(T, size_t n)
{
static if(is(T == S!(n2, U), size_t n2, U))
enum isS = n == n2;
else
enum isS = false;
}

alias MyS = S!(3, float);

static assert(isS!MyS);
static assert(isS!(MyS, 3));
static assert(is(MyS == S!(3, float)));
---


Re: Templates: generic return null;

2014-02-04 Thread Chris
On Tuesday, 4 February 2014 at 00:43:54 UTC, TheFlyingFiddle 
wrote:

On Monday, 3 February 2014 at 10:25:19 UTC, Chris wrote:
Is there a way I can make the return type in getAttribute 
generic? null does not work with numbers.


MyStruct(T) {
 T[T] attributes;
 // 
 public auto getAttribute(T attr) {
 if (!(attr in attributes)) {
   return null; // Doesn't work for numbers!
 }
 return attributes[attr];
   }
}

void main() {
 auto myStr = MyStruct!int(0); // Error
}


Whenever i am faced with this situation i do one (or more then 
one) of the following things.


struct MyStruct(T)
{
T[T] attributes;

   //(1) Forward the underlying access method Eg:
   auto opBinaryRight(string s : in)(T attrib)
   {
  return attrib in attributes;
   }

   //(2) make a try method.
   bool tryAttrib(T attrib, out T outAttrib)
   {
   auto p = attrib in attributes;
   if(p) outAttrib = *p;
   return p !is null;
   }



   //(3) Give user option to set default value.
   T attribOrDefault(T attrib, T default)
   {
   auto p = attrib im attributes;
   return p is null ? default : attrib;
   }


   //(4) Use Nullable!T (I prefer #5 over this one)
   Nullable!T attribOrNull(T attrib)
   {
   Nullable!T result;
   auto p = attrib ib attributes;
   if(p) result = *p;
   return result;
   }

   //(5) Use a pointer but not forward in operator.
   T* attribPtr(T attrib)
   {
  return attrib in attributes;
   }

   //(6) Throw exception (I only do this in combination with 
one of the above)

   T attribEx(T attrib)
   {
 return *enforce!AttribNotFoundEx(attrib in attributes);
   }
}


Thanks for this brief outline.

My personal preference using #2 and #3 in combination. #2 
covers the basic case Is this thing avalible? and #3 covers 
the case Give it to me if it is avalible or use this default 
value I think it gives a clear image of what your code is 
doing at the callsite. Only using #2 or #3 limits you in this 
sence.


Personally I don't like the idea of passing a default value on 
the user side in this particular case. If the attribute has not 
been set, there is a reason, and I don't want to operate with a 
return value of something that has not been set at all.


I introduced a check similar to #2:

bool hasAttribute(T attr) { ... }

Of course, the user has to use if. Experimentally, I introduced

 auto getAttribute(T attr) {
if (!(attr in attributes)) {
  return T.init;
}
return attributes[attr];
  }

to avoid the if statement and just gently move along, if the 
attribute has not been set, which again leads to the problem of 
#3, i.e. potentially operating with a value of something that 
does not exist in the first place.


For #1, #4 and #5 i personally stay away from them. They force 
the caller to either use an if or potentially trigger a null 
pointer derecerence. (Btw what is the benefit of #4? I have 
never used it since it seems pointless)


#4 is weird, but that's because I don't fully understand the 
concept behind it.


I very rarly use attribEx. I don't think code shuld just spew 
exceptions all over the place. They should be reserved for 
really bad stuff, like bounds checks. One exception i make to 
this rule is if i'm dealing with ranges. Since the other 
methods don't lend themselfs for UFCS-chaing.


I agree. Exceptions should be reserved for serious cases or cases 
where you simply cannot predict all cases (reading random input 
from the internet, for example).


Re: Decorators, Annotations, Macros, AST transforms…

2014-02-04 Thread Dicebot

P.P.S. proof-of-concept implemenation of function attributes a
bit more similar t python decorators :
https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/internal/meta/funcattr.d


Re: Python calling D

2014-02-04 Thread Artem Tarasov

On Tuesday, 4 February 2014 at 11:33:40 UTC, Russel Winder wrote:

The question is how to get this run.


Pointing out obvious things, part 2: wrap it into a C function 
and call that function when loading the Python module.


library.d:
...
extern (C) export void attach() { Runtime.initialize(); }

library/__init__.py:
...
lib = ctypes.CDLL(myawesomelib.so)
lib.attach()

def foo():
lib.foo()
...



How can i find my LAN IP Address using std.socket?

2014-02-04 Thread TheFlyingFiddle
I'm trying to find my own ip address using std.socket with little 
success. How would i go about doing this? (It should be a 
AddressFamily.INET socket)


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Dicebot
On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
wrote:
I'm trying to find my own ip address using std.socket with 
little success. How would i go about doing this? (It should be 
a AddressFamily.INET socket)


You can have lot of different local IP addresses on a single 
machine so question can't be answered properly. However you may 
use in `Socket.hostName` and resolve it via DNS to find IP 
machine itself currently consuders as its main pulbic IP.


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Stanislav Blinov
On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
wrote:
I'm trying to find my own ip address using std.socket with 
little success. How would i go about doing this? (It should be 
a AddressFamily.INET socket)


Create a connection to another LAN machine with a known address 
(e.g. gateway or router), then use Socket's localAddress property 
to get your IP. You cannot really do that before establishing a 
connection, as Dicebot already mentioned.


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Dicebot
On Tuesday, 4 February 2014 at 13:21:54 UTC, Stanislav Blinov 
wrote:
Create a connection to another LAN machine with a known address 
(e.g. gateway or router), then use Socket's localAddress 
property to get your IP.


Worth noting that this solution is not reliable in general either 
because your server can possibly have complicated routing 
configurations that will make, for example, LAN destination 
packets go via different network interface than WAN destination 
ones.


It is probably better to tell what high-level problem you are 
trying to solve to find most useful compromise.


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread TheFlyingFiddle

On Tuesday, 4 February 2014 at 13:13:07 UTC, Dicebot wrote:
On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
wrote:
I'm trying to find my own ip address using std.socket with 
little success. How would i go about doing this? (It should be 
a AddressFamily.INET socket)


You can have lot of different local IP addresses on a single 
machine so question can't be answered properly. However you may 
use in `Socket.hostName` and resolve it via DNS to find IP 
machine itself currently consuders as its main pulbic IP.


This works great thanks. I just need any (working) local IP 
address so getting more then one is not an issue for me.


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread TheFlyingFiddle
On Tuesday, 4 February 2014 at 13:21:54 UTC, Stanislav Blinov 
wrote:
On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
wrote:
I'm trying to find my own ip address using std.socket with 
little success. How would i go about doing this? (It should be 
a AddressFamily.INET socket)


Create a connection to another LAN machine with a known address 
(e.g. gateway or router), then use Socket's localAddress 
property to get your IP. You cannot really do that before 
establishing a connection, as Dicebot already mentioned.


Problem is that i don't know in what local network the server 
will be running, so this is unfortunatly not an option for me.


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Stanislav Blinov
On Tuesday, 4 February 2014 at 13:31:27 UTC, TheFlyingFiddle 
wrote:


Problem is that i don't know in what local network the server 
will be running, so this is unfortunatly not an option for me.


But if that's the case, the hostname solution may as well just 
give you your loopback address. :)


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Craig Dillabaugh
On Tuesday, 4 February 2014 at 15:48:50 UTC, Vladimir Panteleev 
wrote:
On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
wrote:
I'm trying to find my own ip address using std.socket with 
little success. How would i go about doing this? (It should be 
a AddressFamily.INET socket)


This program will print all of your computer's IP addresses:

import std.socket;
import std.stdio;

void main()
{
foreach (addr; getAddress(Socket.hostName))
writeln(addr.toAddrString());
}

This includes IPv6 addresses. You can filter the address family 
by checking addr's addressFamily property.


I am a bit lost in anything networking related, so I ran this on 
my machine just for fun and it printed:


127.0.0.1
127.0.0.1
127.0.0.1

which based on my understanding is the local loopback address (I 
am not completely, 100% ignorant).


However if I run /sbin/ifconfig I get:

enp7s0Link encap:Ethernet  HWaddr 50:E5:49:9B:29:49
  inet addr:10.1.101.52  Bcast:10.1.101.255  
Mask:255.255.255.0

  inet6 addr: fe80::52e5:49ff:fe9b:2949/64 Scope:Link

loLink encap:Local Loopback
  inet addr:127.0.0.1  Mask:255.0.0.0
  inet6 addr: ::1/128 Scope:Host

This computer is on a network with dynamically assigned IP 
address (DHCP).

So shouldn't the 10.1.101.52 address have been reported?


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Dicebot
On Tuesday, 4 February 2014 at 16:02:33 UTC, Craig Dillabaugh 
wrote:

However if I run /sbin/ifconfig I get:

enp7s0Link encap:Ethernet  HWaddr 50:E5:49:9B:29:49
  inet addr:10.1.101.52  Bcast:10.1.101.255  
Mask:255.255.255.0

  inet6 addr: fe80::52e5:49ff:fe9b:2949/64 Scope:Link

loLink encap:Local Loopback
  inet addr:127.0.0.1  Mask:255.0.0.0
  inet6 addr: ::1/128 Scope:Host

This computer is on a network with dynamically assigned IP 
address (DHCP).

So shouldn't the 10.1.101.52 address have been reported?


It results in all addresses you hostname resolvs to. On all 
desktop linux machines /etc/hosts is configured to resolve 
hostname to localhost by default. On servers it usually 
resolves to externally accessible one.


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Vladimir Panteleev
On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
wrote:
I'm trying to find my own ip address using std.socket with 
little success. How would i go about doing this? (It should be 
a AddressFamily.INET socket)


This program will print all of your computer's IP addresses:

import std.socket;
import std.stdio;

void main()
{
foreach (addr; getAddress(Socket.hostName))
writeln(addr.toAddrString());
}

This includes IPv6 addresses. You can filter the address family 
by checking addr's addressFamily property.


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Dicebot

On Tuesday, 4 February 2014 at 16:13:33 UTC, Dicebot wrote:
It results in all addresses you hostname resolvs to. On all 
desktop linux machines /etc/hosts is configured to resolve 
hostname to localhost by default. On servers it usually 
resolves to externally accessible one.


Update: I have just checked and this is actually distro-specific 
right now. My Arch box does not have that /etc/hosts entry and 
resolves host name to whatever first configured network interface 
has. Ubuntu does have explicit host name entry in /etc/hosts 
which resolves to localhost.


Re: Python calling D

2014-02-04 Thread Russel Winder
On Tue, 2014-02-04 at 12:45 +, Artem Tarasov wrote:
 On Tuesday, 4 February 2014 at 11:33:40 UTC, Russel Winder wrote:
  The question is how to get this run.
 
 Pointing out obvious things, part 2: wrap it into a C function 
 and call that function when loading the Python module.

I had thought of this and rejected it as an API fail. (Possibly wrongly,
but…)

 library.d:
 ...
 extern (C) export void attach() { Runtime.initialize(); }
 
 library/__init__.py:
 ...
 lib = ctypes.CDLL(myawesomelib.so)
 lib.attach()

This violates the principle of least surprise: you don't have to do this
with C or C++ extensions so it is an API fail for D to have to do this.

 def foo():
  lib.foo()
 ...
 

But it does lead to a working system :-)

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



Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Stanislav Blinov
On Tuesday, 4 February 2014 at 16:02:33 UTC, Craig Dillabaugh 
wrote:


This computer is on a network with dynamically assigned IP 
address (DHCP).

So shouldn't the 10.1.101.52 address have been reported?


Nope. In out-of-the-box simple network setups (i.e. home network 
in the form PC/laptop - router - internet) the address 
resolution won't go further than your hosts file, which in turn 
will always give you back the loopback address (more 
specifically, the address that is specified for the hostname in 
aforementioned file).


That's why both I and Dicebot mentioned there isn't any real way 
to query your local addresses without any live connection: only 
when a socket has a connection within a particular network can 
you tell your own IP address in that network. The address itself 
would depend on the network setup, your local routing 
configuration, etc. Your machine can have several network 
adaptors (ethernet boards, Wi-Fi, etc.), each configured with 
(numerous) routing setups, plus the routers they're connected to 
have their own routing setups... This can go on and on.


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Craig Dillabaugh

On Tuesday, 4 February 2014 at 16:13:33 UTC, Dicebot wrote:
On Tuesday, 4 February 2014 at 16:02:33 UTC, Craig Dillabaugh 
wrote:

However if I run /sbin/ifconfig I get:

enp7s0Link encap:Ethernet  HWaddr 50:E5:49:9B:29:49
 inet addr:10.1.101.52  Bcast:10.1.101.255  
Mask:255.255.255.0

 inet6 addr: fe80::52e5:49ff:fe9b:2949/64 Scope:Link

loLink encap:Local Loopback
 inet addr:127.0.0.1  Mask:255.0.0.0
 inet6 addr: ::1/128 Scope:Host

This computer is on a network with dynamically assigned IP 
address (DHCP).

So shouldn't the 10.1.101.52 address have been reported?


It results in all addresses you hostname resolvs to. On all 
desktop linux machines /etc/hosts is configured to resolve 
hostname to localhost by default. On servers it usually 
resolves to externally accessible one.


Thanks.


What does the alias attribute do here

2014-02-04 Thread Gary Willoughby

What does the alias attribute do here:

void foo(alias bar)
{
...
}

What is the idea behind this attribute when used here?


Re: What does the alias attribute do here

2014-02-04 Thread Adam D. Ruppe
On Tuesday, 4 February 2014 at 17:09:02 UTC, Gary Willoughby 
wrote:

What does the alias attribute do here:

void foo(alias bar)


This specifically won't compile, alias params are only allowed in 
a compile-time list. So


void foo(alias bar)() { ... }

would work.


Anyway, what it does is you pass another symbol to the 
function/template and the alias parameter works as the same 
thing. So let's play with:


void foo(alias bar)() {
import std.stdio;
writeln(bar);
}

void main() {
int a = 20;
foo!a(); // will print 20
}


What happened is foo!a passed the /symbol/, not just the variable 
contents, the variable itself, as the alias parameter. An 
important difference between this and a regular int parameter is 
you can assign to it too:



void foo(alias a)() {
import std.stdio;
writeln(a);
a = 50; // this is as if we literally wrote cool = 50; in 
main()

}

void main() {
int cool = 20;
foo!cool();
assert(cool == 50); // passes
}


alias parameters differ from regular parameters because a regular 
parameter can only be a type name. An alias parameter can be 
another variable.



You can also pass it functions and call them as if the user wrote 
the call themselves - no pointers/delegates involved.


Help with array maniipulation

2014-02-04 Thread ollie
I have a C Struct:

   typedef struct
   {
   enum LibRaw_image_formats type; 
   ushort  height,
   width,
   colors,
   bits;
   unsigned int  data_size; 
   unsigned char data[1]; 
   }libraw_processed_image_t;

with a D version:

   struct libraw_processed_image_t
   {
   LibRaw_image_formatstype; 
   ushort  height,
   width,
   colors,
   bits;
   uintdata_size; 
   ubyte[1]data; 
   }

libraw_dcraw_make_mem_thumb() returns a pointer to this struct.
I need to create a char[] from the data and data_size members,
ideally without copying, to send to GdkPixbufLoader.write(char[]).

If I do this:
   libraw_processed_image_t *img;
   img = libraw_dcraw_make_mem_thumb();
   char[] buf = cast(char[]) img.data[];
   GdkPixbufLoader.write(buf); // Runtime error

buf.ptr == img.data.ptr and length of both arrays is 1.
If I try:
   buf.length = img.data_size;
buf is reallocated.

Any suggestions to make buf.ptr == img.data.ptr and
buf.length = img.data_size?

Thanks
ollie


Re: Help with array maniipulation

2014-02-04 Thread Ali Çehreli

On 02/04/2014 10:58 AM, ollie wrote:

 I have a C Struct:

[...]

 uintdata_size;
 ubyte[1]data;

Is that the C extension where the last array in a struct can have more 
elements than its size? That will be a problem in D.


 }

 libraw_dcraw_make_mem_thumb() returns a pointer to this struct.
 I need to create a char[] from the data and data_size members,
 ideally without copying, to send to GdkPixbufLoader.write(char[]).

 If I do this:
 libraw_processed_image_t *img;
 img = libraw_dcraw_make_mem_thumb();
 char[] buf = cast(char[]) img.data[];

This:

  char[] buf = (cast(char*)img.data.ptr)[0  .. img.data_size];

However, as I said above, data won't have room for more than 1 element.

In any case, treating a C array as a D slice is achieved by the 
following syntax:


int[] D_slice = C_array[0 .. C_array_number_of_elements];

Ali



Re: Difference between is and ==

2014-02-04 Thread Steven Schveighoffer

On Tue, 04 Feb 2014 03:08:28 -0500, Suliman everm...@live.ru wrote:


What difference between
if ((x = stdin.readln().chomp) is q)
and
if ((x = stdin.readln().chomp) == q)

?


The first compares the pointer of the arrays. The second compares the  
contents of the array. Both check length as well for equality.


In other words, the first will always be false (the ROM literal q will  
never have the same address as some heap block), the second will be true  
if the input was the string q.


More generally, 'is' should be a bitwise comparison of the variables. '=='  
should check for logical equality, whatever that means for the variable  
types.


-Steve


Re: What does the alias attribute do here

2014-02-04 Thread Gary Willoughby

On Tuesday, 4 February 2014 at 17:17:13 UTC, Adam D. Ruppe wrote:
On Tuesday, 4 February 2014 at 17:09:02 UTC, Gary Willoughby 
wrote:

What does the alias attribute do here:

   void foo(alias bar)


This specifically won't compile, alias params are only allowed 
in a compile-time list. So


void foo(alias bar)() { ... }

would work.


Anyway, what it does is you pass another symbol to the 
function/template and the alias parameter works as the same 
thing. So let's play with:


void foo(alias bar)() {
import std.stdio;
writeln(bar);
}

void main() {
int a = 20;
foo!a(); // will print 20
}


What happened is foo!a passed the /symbol/, not just the 
variable contents, the variable itself, as the alias parameter. 
An important difference between this and a regular int 
parameter is you can assign to it too:



void foo(alias a)() {
import std.stdio;
writeln(a);
a = 50; // this is as if we literally wrote cool = 50; 
in main()

}

void main() {
int cool = 20;
foo!cool();
assert(cool == 50); // passes
}


alias parameters differ from regular parameters because a 
regular parameter can only be a type name. An alias parameter 
can be another variable.



You can also pass it functions and call them as if the user 
wrote the call themselves - no pointers/delegates involved.


Ah great, thanks that makes perfect sense.


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread TheFlyingFiddle

On Tuesday, 4 February 2014 at 13:24:59 UTC, Dicebot wrote:
On Tuesday, 4 February 2014 at 13:21:54 UTC, Stanislav Blinov 
wrote:
Worth noting that this solution is not reliable in general 
either because your server can possibly have complicated routing 
configurations that will make, for example, LAN destination 
packets go via different network interface than WAN destination 
ones.


It is probably better to tell what high-level problem you are 
trying to solve to find most useful compromise.


I'm setting up a simple local network enabling me to connect 
phones to the computer through the local wi-fi. The simplest way 
i could think of to make this work without relying on an external 
server was to simply broadcast the ip and port to all machines in 
the network.(Btw by server i mean my / my project groups windows 
boxes).


So well the problem is that i need a way for the phones to find 
running servers on the LAN.


Re: Help with array maniipulation

2014-02-04 Thread ollie
On Tue, 04 Feb 2014 11:16:00 -0800, Ali Çehreli wrote:


 Is that the C extension where the last array in a struct can have more 
 elements than its size? That will be a problem in D.

yes it is.

  int[] D_slice = C_array[0 .. C_array_number_of_elements];

Thank you, that gives me what I was looking for. I thought I had tried
something like that. Too many moving parts with threads and context
changes to call GDK/GTK functions make things very precarious. I am
still getting an access violation, but your help has moved me a step
nearer a solution.

Thanks,
ollie


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Johannes Pfau
Am Tue, 04 Feb 2014 16:19:08 +
schrieb Stanislav Blinov stanislav.bli...@gmail.com:

 On Tuesday, 4 February 2014 at 16:02:33 UTC, Craig Dillabaugh 
 wrote:
 
  This computer is on a network with dynamically assigned IP 
  address (DHCP).
  So shouldn't the 10.1.101.52 address have been reported?
 
 Nope. In out-of-the-box simple network setups (i.e. home network 
 in the form PC/laptop - router - internet) the address 
 resolution won't go further than your hosts file, which in turn 
 will always give you back the loopback address (more 
 specifically, the address that is specified for the hostname in 
 aforementioned file).
 
 That's why both I and Dicebot mentioned there isn't any real way 
 to query your local addresses without any live connection: only 
 when a socket has a connection within a particular network can 
 you tell your own IP address in that network. The address itself 
 would depend on the network setup, your local routing 
 configuration, etc. Your machine can have several network 
 adaptors (ethernet boards, Wi-Fi, etc.), each configured with 
 (numerous) routing setups, plus the routers they're connected to 
 have their own routing setups... This can go on and on.

As a last resort there are always OS specific APIs to iterate network
interfaces. For example, for linux:
http://man7.org/linux/man-pages/man3/getifaddrs.3.html

(Of course this doesn't tell you at all whether a local IP-address is
actually routable. And you probably have to figure out if you got
a link local / ULA IPv6 or a global one)


Re: How can i find my LAN IP Address using std.socket?

2014-02-04 Thread Dicebot
On Tuesday, 4 February 2014 at 20:19:14 UTC, TheFlyingFiddle 
wrote:
I'm setting up a simple local network enabling me to connect 
phones to the computer through the local wi-fi. The simplest 
way i could think of to make this work without relying on an 
external server was to simply broadcast the ip and port to all 
machines in the network.(Btw by server i mean my / my project 
groups windows boxes).


So well the problem is that i need a way for the phones to find 
running servers on the LAN.


I think it is close to impossible to do in portable way. Most 
reliable approach is to get list of all configured network 
interfaces via posix functions (or via `system` call as least 
resort), filter out lo and broadcast message for every such 
interface. I think you can also filter only wireless interfaces 
that way relatively easily too.


Re: Help with array maniipulation

2014-02-04 Thread bearophile

Ali Çehreli:

Is that the C extension where the last array in a struct can 
have more elements than its size? That will be a problem in D.


Since V.2.065 D supports well variable-length structs. You have 
to use ubyte[0] data, and then allocate a large enough memory for 
the whole variable-length struct. Here I have written an usage 
example:


http://rosettacode.org/wiki/Sokoban#Faster_Version

Bye,
bearophile


Re: What does the alias attribute do here

2014-02-04 Thread Mike

On Tuesday, 4 February 2014 at 17:17:13 UTC, Adam D. Ruppe wrote:


This specifically won't compile, alias params are only allowed 
in a compile-time list. So


void foo(alias bar)() { ... }

would work.
[...]


Thanks, Adam, for the thorough explanation.  This was quite 
helpful for me as well.


Re: Performant method for reading huge text files

2014-02-04 Thread Chris Williams
On Tuesday, 4 February 2014 at 00:04:23 UTC, Rene Zwanenburg 
wrote:

On Monday, 3 February 2014 at 23:50:54 UTC, bearophile wrote:

Rene Zwanenburg:

The problem is speed. I'm using LockingTextReader in 
std.stdio, but it't not nearly fast enough. On my system it 
only reads about 3 MB/s with one core spending all it's time 
in IO calls.


Are you reading the text by lines? In Bugzilla there is a 
byLineFast:

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

Bye,
bearophile


Nope, I'm feeding it to csvReader which uses an input range of 
characters. Come to think of it..


Well this is embarassing, I've been sloppy with my profiling 
:). It appears the time is actually spent converting strings to 
doubles, done by csvReader to read a row into my Record struct. 
No way to speed that up I suppose. Still I find it surprising 
that parsing doubles is so slow.


Parsing should be faster than I/O. Set up two buffers and have 
one thread reading into buffer A while you parse buffer B with a 
second thread.


Re: Performant method for reading huge text files

2014-02-04 Thread Chris Williams
Parsing should be faster than I/O. Set up two buffers and have 
one thread reading into buffer A while you parse buffer B with 
a second thread.


...and then flip buffers whenever the slower of the two has 
completed.