Re: Why is the constructor of B called?

2015-09-24 Thread Marc Schütz via Digitalmars-d-learn
On Thursday, 24 September 2015 at 01:01:09 UTC, Nicholas Wilson 
wrote:

On Wednesday, 23 September 2015 at 21:25:15 UTC, tcak wrote:
On Wednesday, 23 September 2015 at 21:14:17 UTC, Adam D. Ruppe 
wrote:

On Wednesday, 23 September 2015 at 21:08:37 UTC, tcak wrote:
I wouldn't expect B's constructor to be called at all unless 
"super" is used there.


"If no call to constructors via this or super appear in a 
constructor, and the base class has a constructor, a call to 
super() is inserted at the beginning of the constructor. "



from http://dlang.org/class.html#constructors

the idea is to make sure the base class construction work is 
done too.


Is there any way to prevent this behaviour?

Quickly checked whether Java acts in the same way. Answer is 
yes.


You might be able to swap out the vtbl entry  for a stub call 
it and trick the compiler and swap it back, but...


Urgh...

If you can modify the base class, and you really need it, you can 
check the dynamic type:


class Base {
this() {
if(!cast(Base) this) return;
// do the initialization
}
}


Re: Maximum number of threads

2015-09-24 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, September 24, 2015 08:55:22 Alex via Digitalmars-d-learn wrote:
> This should be a not so long question to answer, I hope.
>
> I took an example from the "Programming in D" book, chapter
> "Message Passing Concurrency", around page 550. The question of
> interest was, how many threads I can have spawned at the same
> time.
> So I made an array of robot objects from the example and found
> out: the maximum number is different on different PCs I have.
> For example:
> - on my mac I can have 2048 threads spawned at the same time
> - on my linux machine the maximum number is 32192.
> The numbers are quite fixed, however there were some small
> fluctuations on the linux machine.
>
> The questions still remains: how do I know, what maximum number
> of threads I can have? Is it possible to calculate this value
> during runtime of my program? The two machines I have available
> for testing are very different, what is the main parameter which
> controls the number of possible threads? (CPU maybe? Number of
> cores? RAM is not I think at the moment...)

It's entirely system specific. Not only does every OS handle it differently,
but it con depend on how your machine is configured. In general though, I
believe that it's a hard limit in the OS and has nothing to do with the
number of cores or amount of RAM in your machine (though even if you haven't
hit the limit, if the OS doesn't have enough resources to create another
thread, then it will fail).

So, if you want to know for a particular OS, you're probably going to have
to google for it. In the case of linux, a quick search seems to indicate
that getrlimit will do the trick:

http://linux.die.net/man/2/getrlimit

But you can find as much pretty quickly googling yourself. Maybe someone
else can tell you what to use on other OSes off the top of their head
though.

Regardless, D doesn't provid any special way to do this, so to figure it
out, you'd be calling whatever the C functions are that will tell you.

- Jonathan M Davis



Re: Dub package with C code

2015-09-24 Thread Sebastiaan Koppe via Digitalmars-d-learn
On Thursday, 24 September 2015 at 08:35:40 UTC, Edwin van Leeuwen 
wrote:

Alternatively you could use reggea to build both.


I want to use dub.

Simply because of code.dlang.org. Or can reggae also pull 
packages from there?


You could try including the c source in your repo and add 
preBuildCommands to the dub config which builds the static 
library.


preBuildCommands seems to be the way to go.

Thank you all.


Re: Maximum number of threads

2015-09-24 Thread Marc Schütz via Digitalmars-d-learn

On Thursday, 24 September 2015 at 08:55:25 UTC, Alex wrote:

This should be a not so long question to answer, I hope.

I took an example from the "Programming in D" book, chapter 
"Message Passing Concurrency", around page 550. The question of 
interest was, how many threads I can have spawned at the same 
time.
So I made an array of robot objects from the example and found 
out: the maximum number is different on different PCs I have.

For example:
- on my mac I can have 2048 threads spawned at the same time
- on my linux machine the maximum number is 32192.
The numbers are quite fixed, however there were some small 
fluctuations on the linux machine.


The questions still remains: how do I know, what maximum number 
of threads I can have? Is it possible to calculate this value 
during runtime of my program? The two machines I have available 
for testing are very different, what is the main parameter 
which controls the number of possible threads? (CPU maybe? 
Number of cores? RAM is not I think at the moment...)


Thanks in advance
Alex


From the numbers (2048 = 2^11, 32192 = ca 2^15), it's clear that 
you're not running out of memory, but that these are artificial 
limits imposed by the OS.


This stackoverflow answer describes the situation on Linux:
http://unix.stackexchange.com/a/47600/34768


Re: Maximum number of threads

2015-09-24 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Thursday, 24 September 2015 at 08:55:25 UTC, Alex wrote:

- on my mac I can have 2048 threads spawned at the same time
- on my linux machine the maximum number is 32192.
The numbers are quite fixed, however there were some small 
fluctuations on the linux machine.


I only know of `cat /proc/sys/kernel/threads-max` on linux, which 
will give you the max number of threads system-wide.


Re: Dub package with C code

2015-09-24 Thread Rikki Cattermole via Digitalmars-d-learn

On 24/09/15 5:51 PM, Sebastiaan Koppe wrote:

On Thursday, 24 September 2015 at 04:17:14 UTC, Rikki Cattermole wrote:

Is libxlsxwriter available in the systems package manager?


Pacman says no.


Let e.g. Windows users figure theirs out.


libxlsxwriter is not supported on windows. Which is kind-of funny.


I've taken a look at it.
Why not just port it?


Re: Dub package with C code

2015-09-24 Thread Sebastiaan Koppe via Digitalmars-d-learn
On Thursday, 24 September 2015 at 06:00:50 UTC, Rikki Cattermole 
wrote:

On 24/09/15 5:51 PM, Sebastiaan Koppe wrote:
On Thursday, 24 September 2015 at 04:17:14 UTC, Rikki 
Cattermole wrote:

Is libxlsxwriter available in the systems package manager?


Pacman says no.


Let e.g. Windows users figure theirs out.


libxlsxwriter is not supported on windows. Which is kind-of 
funny.


I've taken a look at it.
Why not just port it?


Because I want to focus on the product I am building right now, 
not on side-projects.


Maximum number of threads

2015-09-24 Thread Alex via Digitalmars-d-learn

This should be a not so long question to answer, I hope.

I took an example from the "Programming in D" book, chapter 
"Message Passing Concurrency", around page 550. The question of 
interest was, how many threads I can have spawned at the same 
time.
So I made an array of robot objects from the example and found 
out: the maximum number is different on different PCs I have.

For example:
- on my mac I can have 2048 threads spawned at the same time
- on my linux machine the maximum number is 32192.
The numbers are quite fixed, however there were some small 
fluctuations on the linux machine.


The questions still remains: how do I know, what maximum number 
of threads I can have? Is it possible to calculate this value 
during runtime of my program? The two machines I have available 
for testing are very different, what is the main parameter which 
controls the number of possible threads? (CPU maybe? Number of 
cores? RAM is not I think at the moment...)


Thanks in advance
Alex


Re: Dub package with C code

2015-09-24 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Thursday, 24 September 2015 at 06:21:02 UTC, Sebastiaan Koppe 
wrote:
Because I want to focus on the product I am building right now, 
not on side-projects.


You could try including the c source in your repo and add 
preBuildCommands to the dub config which builds the static 
library. Alternatively you could use reggea to build both.


Re: Maximum number of threads

2015-09-24 Thread Temtaime via Digitalmars-d-learn
Offtop: i think if number of threads > number of real cores, than 
there's something wrong with your design. Maybe fibers suit 
better ?


question about multiple alias this

2015-09-24 Thread steven kladitis via Digitalmars-d-learn

class A{
  int i;
  bool b;
  alias i this;
  alias b this;
}

void main()
{
  auto a = new A;
  int i = a;
  bool b = a;
}

--- this will not compile in dmd 2068.1.
--- ok, but what technique do you use to emulate this type of 
behavior??
--- would interfaces be the answer, if so could I see an example. 
and would you use the

--- same for structs ??
--- thanks, Steven


Re: Why is the constructor of B called?

2015-09-24 Thread Nicholas Wilson via Digitalmars-d-learn

On Thursday, 24 September 2015 at 11:26:12 UTC, Marc Schütz wrote:
On Thursday, 24 September 2015 at 01:01:09 UTC, Nicholas Wilson 
wrote:

On Wednesday, 23 September 2015 at 21:25:15 UTC, tcak wrote:
On Wednesday, 23 September 2015 at 21:14:17 UTC, Adam D. 
Ruppe wrote:

[...]


Is there any way to prevent this behaviour?

Quickly checked whether Java acts in the same way. Answer is 
yes.


You might be able to swap out the vtbl entry  for a stub call 
it and trick the compiler and swap it back, but...


Urgh...

If you can modify the base class, and you really need it, you 
can check the dynamic type:


class Base {
this() {
if(!cast(Base) this) return;
// do the initialization
}
}
doesn't upcasting always work? iirc only down casting can return 
null.


Re: Why is the constructor of B called?

2015-09-24 Thread Artur Skawina via Digitalmars-d-learn
On 09/24/15 13:26, Marc Schütz via Digitalmars-d-learn wrote:
> On Thursday, 24 September 2015 at 01:01:09 UTC, Nicholas Wilson wrote:
>> On Wednesday, 23 September 2015 at 21:25:15 UTC, tcak wrote:
>>> On Wednesday, 23 September 2015 at 21:14:17 UTC, Adam D. Ruppe wrote:
 On Wednesday, 23 September 2015 at 21:08:37 UTC, tcak wrote:
> I wouldn't expect B's constructor to be called at all unless "super" is 
> used there.

 "If no call to constructors via this or super appear in a constructor, and 
 the base class has a constructor, a call to super() is inserted at the 
 beginning of the constructor. "


 from http://dlang.org/class.html#constructors

 the idea is to make sure the base class construction work is done too.
>>>
>>> Is there any way to prevent this behaviour?
>>>
>>> Quickly checked whether Java acts in the same way. Answer is yes.
>>
>> You might be able to swap out the vtbl entry  for a stub call it and trick 
>> the compiler and swap it back, but...
> 
> Urgh...
> 
> If you can modify the base class, and you really need it, you can check the 
> dynamic type:
> 
> class Base {
> this() {
> if(!cast(Base) this) return;
> // do the initialization
> }
> }

If you're going to do this then you can just use overloading.
That will both avoid the runtime check and require the hack
to be explicitly enabled in the derived class. IOW:

   class B {
   this() {
   writeln("B.constructor");
   foo();
   }

   struct SkipBCtor {}
   this(SkipBCtor) {}

   void foo() {
   writeln("B.foo");
   }
   }

   class D : B {
   this() {
   super(SkipBCtor());
   writeln("D.constructor");
   }

   override void foo() {
   writeln("D.foo overrides B.foo");
   }
   }

artur


Re: ORM libraries for D

2015-09-24 Thread Rikki Cattermole via Digitalmars-d-learn

On 25/09/15 1:18 AM, David Nadlinger wrote:

Hi all,

I'm having a look at ORM libraries in D right now. So far, I've come
across hibernated and dvorm.

Are there any other libraries that I should have a look at, particularly
actively maintained ones? dvorm and hibernated seem to have received no
work during the last couple of months.

  — David


Dvorm is more or less feature complete :)
I am the author of it, but unless issues come up I do not intend to 
continue working upon it.


I have another one being worked upon but think 2 year plan. There are 
many more important libraries to fill my time with, unfortunately.


Re: ORM libraries for D

2015-09-24 Thread Rikki Cattermole via Digitalmars-d-learn

On 25/09/15 1:30 AM, Edwin van Leeuwen wrote:

On Thursday, 24 September 2015 at 13:24:14 UTC, Rikki Cattermole wrote:

Dvorm is more or less feature complete :)
I am the author of it, but unless issues come up I do not intend to
continue working upon it.


You could consider bumping it up to version 1.0.0 to highlight this.


Put it this way, doing so would also bump it up on code.dlang.org.
I have not even ran the unittests in like a year. So who knows if it 
compiles with 2.068. But nobody has complained so lets assume yes.




Re: ORM libraries for D

2015-09-24 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Thursday, 24 September 2015 at 13:24:14 UTC, Rikki Cattermole 
wrote:

Dvorm is more or less feature complete :)
I am the author of it, but unless issues come up I do not 
intend to continue working upon it.


You could consider bumping it up to version 1.0.0 to highlight 
this.





Re: Dub package with C code

2015-09-24 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 24 September 2015 at 06:21:02 UTC, Sebastiaan Koppe 
wrote:
Because I want to focus on the product I am building right now, 
not on side-projects.


We should write a C to D converter. We have htod but I'm talking 
the whole source of it. That might be useful for times like this.


But I have other things to do too...


Re: ORM libraries for D

2015-09-24 Thread Rikki Cattermole via Digitalmars-d-learn

On 25/09/15 1:43 AM, David Nadlinger wrote:

On Thursday, 24 September 2015 at 13:24:14 UTC, Rikki Cattermole wrote:

Dvorm is more or less feature complete :)
I am the author of it, but unless issues come up I do not intend to
continue working upon it.


Do you know whether somebody has written an SQLite provide for it? I was
going to use SQLite initially, hoping to just switch to MySQL/PostgreSQL
later in case I unexpectedly need more than that later.

  — David


As far as I am aware no.
Although I would recommend against taking it up unless you feel like 
taking ownership of it(Dvorm).


ORM libraries for D

2015-09-24 Thread David Nadlinger via Digitalmars-d-learn

Hi all,

I'm having a look at ORM libraries in D right now. So far, I've 
come across hibernated and dvorm.


Are there any other libraries that I should have a look at, 
particularly actively maintained ones? dvorm and hibernated seem 
to have received no work during the last couple of months.


 — David


Re: ORM libraries for D

2015-09-24 Thread David Nadlinger via Digitalmars-d-learn
On Thursday, 24 September 2015 at 13:24:14 UTC, Rikki Cattermole 
wrote:

Dvorm is more or less feature complete :)
I am the author of it, but unless issues come up I do not 
intend to continue working upon it.


Do you know whether somebody has written an SQLite provide for 
it? I was going to use SQLite initially, hoping to just switch to 
MySQL/PostgreSQL later in case I unexpectedly need more than that 
later.


 — David


Client socket sending data only one time.

2015-09-24 Thread holo via Digitalmars-d-learn

Hello

I'm trying to connect to server and send data, with such simple 
client:


#!/usr/bin/rdmd

import std.stdio;
import std.socket;
import std.socketstream;
import std.process;
import std.conv;
import core.time;

void main()
{
char[1024] buffer = 0;

Socket client = new TcpSocket();
auto addrServer = new InternetAddress("localhost", 8080);
client.connect(addrServer);

while(1)
{

client.send(readln());
client.receive(buffer);
writeln(buffer);
buffer = 0;
 }
}

It is working but only one time, when I'm trying to send again 
(second loop of while) it's stooping on readln() but not sending 
input data.


What am i missing here?

Thanks in advance for any help.


Re: dis...@dlang.org

2015-09-24 Thread Aidan via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 17:43:43 UTC, Ali Çehreli 
wrote:

On 09/23/2015 06:01 AM, Aidan wrote:
I am just starting to look into D and i have to say I am 
loving it at
the moment. But I have ran into an issue that i can't seem to 
find any

libraries for Api hooking.

If anyone knows of a well documented source for this it would 
be much

appreciated.


I don't understand the question but it may be related to lack 
of fiber in one's diet: :)


  http://vibed.org/features#fibers

  http://ddili.org/ders/d.en/fibers.html

Ali


I am reading your book On D at the moment! finding it very useful.


Re: dis...@dlang.org

2015-09-24 Thread Aidan via Digitalmars-d-learn

On Wednesday, 23 September 2015 at 18:09:22 UTC, novice2 wrote:

http://forum.dlang.org/thread/hrzfcjrltftgzansd...@forum.dlang.org
https://github.com/Trass3r/hooksample


Yes this is what I was looking for. thank you.


Re: question about multiple alias this

2015-09-24 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, September 24, 2015 13:04:20 steven kladitis via 
Digitalmars-d-learn wrote:
> class A{
>int i;
>bool b;
>alias i this;
>alias b this;
> }
>
> void main()
> {
>auto a = new A;
>int i = a;
>bool b = a;
> }
>
> --- this will not compile in dmd 2068.1.
> --- ok, but what technique do you use to emulate this type of
> behavior??
> --- would interfaces be the answer, if so could I see an example.
> and would you use the
> --- same for structs ??
> --- thanks, Steven

The only implicit conversion that's built into user-defined types is casting
from a derived class to a base class or interface that it implements. All
other implicit conversions have to be implemented via alias this, and
currently, you can only have one alias this. So, what you're trying to do
really doesn't work right now. You could declare an opCast for each type you
want to be able to convert to and do explicit casts, but alias this is the
only way to add implicit conversions.

We'll probably have support for multiple alias thises at some point, but I
don't know when.

- Jonathan M Davis



Re: Dub package with C code

2015-09-24 Thread Laeeth Isharc via Digitalmars-d-learn
On Thursday, 24 September 2015 at 02:43:20 UTC, Sebastiaan Koppe 
wrote:
I have just created bindings for libxlsxwriter, an c library 
for creating excel files.


Used the htod tool to do most of the work, and only had to 
adjust some things - mainly because libxlsxwriter uses data 
structures written in macro's.


Right now I am making a dub package and I would like to aim for 
convenience for end-users (read: me).


Therefor I decided to include the compiled static library 
inside the package. I only use Linux 64-bit myself, but this is 
obviously limiting for other people.


The other option I had was to include the whole c code, depend 
on gcc or clang, and have dub  (somehow) first build 
libxlsxwriter. But that seemed a bit too much...


Another option would be to require end-users to build 
libxlsxwriter themselves.


What do you guys recommend?


nice work!

worth a little blog post on the experience so others can be 
inspired by and benefit from yours?




Re: Maximum number of threads

2015-09-24 Thread Alex via Digitalmars-d-learn

On Thursday, 24 September 2015 at 12:38:41 UTC, Temtaime wrote:
Offtop: i think if number of threads > number of real cores, 
than there's something wrong with your design. Maybe fibers 
suit better ?


Well... you got my idea :) So it is not so far offtop, as you 
think )))
Fibers DO suit better as an idea of light workers. But the 
problem I have to manage is, that my program has to incorporate 
unpredictability in the work cycle of my workers. It is one of 
the main idea of the process itself. So the fact that a fiber 
yields at fixed points of his work is just the opposite from what 
I want.


But the portability of code is an issue too... and my tries show, 
that I can't rely on this with threads...


Having said that, I got an idea today, how I could try to 
incorporate fibers for my needs. If I manage to hide the yields 
calls of my workers in some random environment, I could express, 
that a worker can but must not give up his control at a specific 
point of his work. I have to try this idea next days.
Using fibers instead of threads would save me from a lot of 
troubles anyway... ;)


Thanks to everybody for hints about my question!


Re: How to setup mono-D for shared libraries?

2015-09-24 Thread Jacob via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 04:34:45 UTC, Rikki Cattermole 
wrote:

On 23/09/15 8:20 AM, Jacob wrote:
How do I setup mono-D for creating shared libraries and 
including them
into other projects? When I drag the .d files to create the 
library
from, which is not my own, I get undefined references. I have 
the lib
files, which are a bunch of separate libs, that I want to 
include into
one big lib. Once that's done I want to include that lib into 
another

project.

I'd rather not modify si.ini. Are there any tutorials for 
getting

started with Mono-D? (setup, not coding)


Well you could go the route of dub, which configuration files 
can be loaded directly into it as a project.


To create the library, what do I do?

'dub init myLib'

then delete app.d,

and then add all the library.d files?

Then what? Where do I tell it to find the .lib files without 
adding them to sc.ini?


Do I just mess with dub.json to and add all the proper build 
options to get both the lib and the dependent apps working or are 
there other steps I'm missing?








Re: Dub package with C code

2015-09-24 Thread Sebastiaan Koppe via Digitalmars-d-learn
On Thursday, 24 September 2015 at 18:19:49 UTC, Laeeth Isharc 
wrote:

nice work!


To be honest it took me 2 hours, starting from git clone

worth a little blog post on the experience so others can be 
inspired by and benefit from yours?


I would be happy to write something. Any particular blog 
platform? Or do I first have to roll out my own?




Re: Dub package with C code

2015-09-24 Thread Sebastiaan Koppe via Digitalmars-d-learn
On Thursday, 24 September 2015 at 12:55:12 UTC, Adam D. Ruppe 
wrote:
On Thursday, 24 September 2015 at 06:21:02 UTC, Sebastiaan 
Koppe wrote:
Because I want to focus on the product I am building right 
now, not on side-projects.


We should write a C to D converter. We have htod but I'm 
talking the whole source of it. That might be useful for times 
like this.


But I have other things to do too...


It would be really cool to have that. But aren't there a lot of 
corner cases that require manual intervention?


Having said that, htod has been invaluable. And if I decide to 
port it, ctod will be invaluable as well, even though it won't 
get it 100% right.


Re: Dub package with C code

2015-09-24 Thread Atila Neves via Digitalmars-d-learn
On Thursday, 24 September 2015 at 11:38:08 UTC, Sebastiaan Koppe 
wrote:
On Thursday, 24 September 2015 at 08:35:40 UTC, Edwin van 
Leeuwen wrote:

Alternatively you could use reggea to build both.


I want to use dub.

Simply because of code.dlang.org. Or can reggae also pull 
packages from there?


Yes.

Atila


Re: How to setup mono-D for shared libraries?

2015-09-24 Thread Rikki Cattermole via Digitalmars-d-learn

On 25/09/15 9:58 AM, Jacob wrote:

On Wednesday, 23 September 2015 at 04:34:45 UTC, Rikki Cattermole wrote:

On 23/09/15 8:20 AM, Jacob wrote:

How do I setup mono-D for creating shared libraries and including them
into other projects? When I drag the .d files to create the library
from, which is not my own, I get undefined references. I have the lib
files, which are a bunch of separate libs, that I want to include into
one big lib. Once that's done I want to include that lib into another
project.

I'd rather not modify si.ini. Are there any tutorials for getting
started with Mono-D? (setup, not coding)


Well you could go the route of dub, which configuration files can be
loaded directly into it as a project.


To create the library, what do I do?

'dub init myLib'

then delete app.d,

and then add all the library.d files?

Then what? Where do I tell it to find the .lib files without adding them
to sc.ini?

Do I just mess with dub.json to and add all the proper build options to
get both the lib and the dependent apps working or are there other steps
I'm missing?


"targetType": "dynamicLibrary"
http://code.dlang.org/package-format?lang=json#target-types

I would also recommend looking at sub packages.
http://code.dlang.org/package-format?lang=json#sub-packages
That'll make things a lot easier. To have both an executable and a 
shared library built from the same project directory :)


Re: Dub package with C code

2015-09-24 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Friday, 25 September 2015 at 01:36:11 UTC, Atila Neves wrote:
On Thursday, 24 September 2015 at 11:38:08 UTC, Sebastiaan 
Koppe wrote:

Or can reggae also pull packages from there?


Yes.


That is nice.

I look at reggae once or twice. It looked like a lot of 
bootstrapping vs. `dub init && dub build`. And to be honest, I 
couldn't make immediate sense of the api.


Is there any small example that builds some d code (including 
pulling deps)?


Re: Can I check if a value is convertible to a valid value of an enum?

2015-09-24 Thread thedeemon via Digitalmars-d-learn
On Friday, 25 September 2015 at 03:12:20 UTC, French Football 
wrote:

...without having to loop over the enum?
writeln( test( valid_value ) ); //prints true


Since `value` is known only at run time, some checks need to be 
performed at run time anyway. One way of doing it without 
iterating over all variants is to create a static hash table


bool[string] valid_strings;

and populate it in static constructor, then in your function you 
can just write


if (value in valid_strings) ...


Re: Client socket sending data only one time.

2015-09-24 Thread tcak via Digitalmars-d-learn

On Thursday, 24 September 2015 at 14:20:39 UTC, holo wrote:

Hello

I'm trying to connect to server and send data, with such simple 
client:


#!/usr/bin/rdmd

import std.stdio;
import std.socket;
import std.socketstream;
import std.process;
import std.conv;
import core.time;

void main()
{
char[1024] buffer = 0;

Socket client = new TcpSocket();
auto addrServer = new InternetAddress("localhost", 
8080);

client.connect(addrServer);

while(1)
{

client.send(readln());
client.receive(buffer);
writeln(buffer);
buffer = 0;
 }
}

It is working but only one time, when I'm trying to send again 
(second loop of while) it's stooping on readln() but not 
sending input data.


What am i missing here?

Thanks in advance for any help.


Where is the other side of this client? There must be a TCP 
Server to listen connections, and create a second TCPSocket for 
communication. There is only one TCPSocket in your code. A lot of 
things are missing there. I would suggest you to check "C 
TCPSocket example" in your favourite search engine.


Re: Maximum number of threads

2015-09-24 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, September 24, 2015 12:38:39 Temtaime via Digitalmars-d-learn wrote:
> Offtop: i think if number of threads > number of real cores, than
> there's something wrong with your design. Maybe fibers suit
> better ?

That depends on what the threads are doing. If they're all CPU-intensive,
then yeah, having more than about the number of real cores isn't going to
work very well, but in many programs, a lot of the threads aren't doing much
(e.g. waiting for something to happen and sitting idly in the meantime). If
anything, I'd guess that it's far more common to have a lot more threads
than cores if a program isn't single-threaded. I'd say that whether the
number of cores matters particularly depends heavily on what your program is
doing. But there are certainly cases where using fibers instead of threads
makes a lot of sense.

- Jonathan M Davis



Re: I can't get passing an array by reference to work anymore...

2015-09-24 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 25 September 2015 at 02:37:22 UTC, TheGag96 wrote:
So I'm just doing a small test program here: 
http://pastebin.com/UYf2n6bP


(I'm making sure I know quicksort for my algorithms class, I 
know functionally this won't work as-is)


I'm on Linux, 64-bit, DMD 2.068.1, and when I try to compile 
this I'm getting:


quicksort.d(18): Error: function quicksort.quickSort (ref int[] 
arr) is not callable using argument types (int[])
quicksort.d(19): Error: function quicksort.quickSort (ref int[] 
arr) is not callable using argument types (int[])


No matter how I attempt to define the array test, it will never 
allow me to pass it by reference no matter what. I even 
copy-pasted some example code from here 
(https://en.wikibooks.org/wiki/D_(The_Programming_Language)/d2/Pointers,_Pass-By-Reference_and_Static_Arrays#Pass_By_Reference, bottom of the page), and it gave the same error.


What's the problem here? I SWEAR I've passed arrays by 
reference before just like this. Thanks guys.


Im not sure why but..
Are you sure that you want to pass the array by ref?
In D the type int[] is like struct { size_t length; int* ptr;} 
which means you are accessing through a pointer and thus changing 
the data but not reassigning the array as you would with ref.


That link may well be out of date, Ali's Book 
(http://ddili.org/ders/d.en/index.html) is a great resource if 
you are learning.


Alternately look at the std algorithm implementation of quicksort.

Nic


Can I check if a value is convertible to a valid value of an enum?

2015-09-24 Thread French Football via Digitalmars-d-learn

...without having to loop over the enum?

enum SomeType : string { CHEESE = "cheese", POTATO = "potato" }
string valid_value = "cheese";
string invalid_value = "pizza";

bool test( string value ){
if( value.isConvertableToSomeType ){ // this be the magic I 
seek

//do something
return true;
}
return false;
}

void main(){
writeln( test( valid_value ) ); //prints true
writeln( test( invalid_value ) ); //prints false
}

Or would I need to foreach over the enum somehow?


Re: BidirectionalRange switching direction

2015-09-24 Thread Tofu Ninja via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 03:26:29 UTC, John Colvin 
wrote:
On Wednesday, 23 September 2015 at 02:10:22 UTC, Tofu Ninja 
wrote:
Trying to implement a bi directional range and it is slightly 
unclear what the semantics are supposed to be and just wanted 
some clarification.


Are bidirectional ranges supposed to be able to support 
switching direction mid iteration? Like if I do popFront 
popFront popBack should that be equal to just a single 
popFront? Or once you start on a direction should switching be 
considered an error? Or is popFront and popBack supposed to 
consume from both ends of the range and the range is empty 
when they meet?


-tofu


The last one. E.g. for arrays (except narrow strings... ugh)

auto popFront(T)(ref T[] a)
{
a = a[1 .. $];
}

auto popBack(T)(ref T[] a)
{
a = a[0 .. $-1];
}


Ok cool, that's what I ended up doing.


Re: Can I check if a value is convertible to a valid value of an enum?

2015-09-24 Thread Nicholas Wilson via Digitalmars-d-learn
On Friday, 25 September 2015 at 03:12:20 UTC, French Football 
wrote:

...without having to loop over the enum?

enum SomeType : string { CHEESE = "cheese", POTATO = "potato" }
string valid_value = "cheese";
string invalid_value = "pizza";

bool test( string value ){
if( value.isConvertableToSomeType ){ // this be the magic I 
seek

//do something
return true;
}
return false;
}

void main(){
writeln( test( valid_value ) ); //prints true
writeln( test( invalid_value ) ); //prints false
}

Or would I need to foreach over the enum somehow?


find + EnumMembers should do the trick
if ( (EnumMembers!SomeType).canFind(value))
{
// ...
}



Re: I can't get passing an array by reference to work anymore...

2015-09-24 Thread Ali Çehreli via Digitalmars-d-learn

On 09/24/2015 09:14 PM, Mike Parker wrote:

> I'm seeing the same error, but I haven't yet determined why.

It is because rvalues cannot be bound to 'ref' parameters:

void quickSort(ref int[] arr) {
// ...

  quickSort(arr[0..wall]);  // This slice is rvalue
  quickSort(arr[wall+1..$]);// ditto
}

As it has already been said, there is no reason for the ref for this 
function but the compilation error can be removed by making them lvalues:


  auto left = arr[0..wall];   // now lvalue
  quickSort(left);

  auto right = arr[wall+1..$];// ditto
  quickSort(right);

Then it will expose another problem with the code. ;)

Ali



Deduplicating Template Parameter List of std.variant.Algebraic

2015-09-24 Thread Nordlöw via Digitalmars-d-learn
I just noticed that Algebraic doesn't deduplicate its types 
before construction because this compiles:


import std.variant : Algebraic;
auto x = Algebraic!(int, int)(5);

Is this really sane?


I can't get passing an array by reference to work anymore...

2015-09-24 Thread TheGag96 via Digitalmars-d-learn
So I'm just doing a small test program here: 
http://pastebin.com/UYf2n6bP


(I'm making sure I know quicksort for my algorithms class, I know 
functionally this won't work as-is)


I'm on Linux, 64-bit, DMD 2.068.1, and when I try to compile this 
I'm getting:


quicksort.d(18): Error: function quicksort.quickSort (ref int[] 
arr) is not callable using argument types (int[])
quicksort.d(19): Error: function quicksort.quickSort (ref int[] 
arr) is not callable using argument types (int[])


No matter how I attempt to define the array test, it will never 
allow me to pass it by reference no matter what. I even 
copy-pasted some example code from here 
(https://en.wikibooks.org/wiki/D_(The_Programming_Language)/d2/Pointers,_Pass-By-Reference_and_Static_Arrays#Pass_By_Reference, bottom of the page), and it gave the same error.


What's the problem here? I SWEAR I've passed arrays by reference 
before just like this. Thanks guys.


Re: I can't get passing an array by reference to work anymore...

2015-09-24 Thread Mike Parker via Digitalmars-d-learn

On Friday, 25 September 2015 at 02:37:22 UTC, TheGag96 wrote:
What's the problem here? I SWEAR I've passed arrays by 
reference before just like this. Thanks guys.


I'm seeing the same error, but I haven't yet determined why. At 
any rate, this works:


```
import std.stdio;

void append(ref int[] arr, int val) {
arr ~= val;
}

void main() {
auto a1 = [10,20,30];
a1.append(40);
writeln(a1);
}
```

The compiler error aside, there's a big difference between this 
function and your quicksort. This one is modifying the structure 
(or metadata) of the source array. Your quicksort is only dealing 
with the elements. You don't need to use ref to effect changes on 
the elements of the source array. arr[1] = 2 on a function 
parameter will be reflected in the source array even when ref is 
absent. Only when you need to modify the length of the source 
array, or cause it to point to a new memory block, would you need 
to use ref.


So in your particular case, you can drop the ref from your 
parameter and the compiler error will go away. However, the 
function as written is producing a range violation :)


I'd still like to know what's causing the compiler error with ref 
in this case, though.


pi program

2015-09-24 Thread Charanjit Singh via Digitalmars-d-learn

import std.stdio;
import std.math;
void main()

{
float sum,pi,t;
 int n=1;
sum=0;
while (n<100 )
{
t=1/( n*n);
n=n+1;
sum=sum+t;


   }
writeln("value of PI=  " , (sum*6)^^.5);
 that is pi program as
(pi^2)/6=   1/1+  1/4  +  1/9  +  1/16  +  1/25
but output of my program is 2.44