Re: Idiomatic async programming like C# async/await

2014-09-12 Thread Kagamin via Digitalmars-d-learn
async/await is not so much about futures/promises, but 
optimization of IO-bound operations, i.e. when you wait on 
network/disk, you don't consume stack, threads and similar 
resources, an analog in D is vibe.d


Re: Is º an unicode alphabetic character?

2014-09-12 Thread Ali Çehreli via Digitalmars-d-learn

On 09/11/2014 11:38 PM, AsmMan wrote:

> If I want ASCII and latin only alphabet which range should I use?
> ie, how should I rewrite is_id() function?

This seems to be it:

import std.stdio;
import std.uni;

void main()
{
alias latin = unicode.script.latin;
assert('ç' in latin);
assert('7' !in latin);

writeln(latin);
}

Ali



Re: std.range.byLine

2014-09-12 Thread Nordlöw
On Thursday, 11 September 2014 at 22:39:40 UTC, H. S. Teoh via 
Digitalmars-d-learn > Why not just use std.regex?


foreach (line; myInput.split(regex(`\n|\r\n|\r`)))
{
...
}


T


I'll try the lazy variant of std.regex

foreach (line; myInput.splitter(regex(`\n|\r\n|\r`)))
{
...
}

I wonder if this is compatible with a ctRegex aswell. I'll try 
later.


See also: http://dlang.org/phobos/std_regex.html#.splitter

Thx


Re: std.range.byLine

2014-09-12 Thread Nordlöw
On Thursday, 11 September 2014 at 22:39:40 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:

foreach (line; myInput.split(regex(`\n|\r\n|\r`)))


Shouldn't you use

foreach (line; myInput.split(regex("\n|\r\n|\r")))

here?


Extremely funny behavior .. could be a bug?

2014-09-12 Thread seany via Digitalmars-d-learn

consider the following :

in file a.d

module a;

class class_a
{

   struct RESULT{
   string[] raw;
   void* res;
  }


RESULT r;

void dothing()
{
   r = new RESULT;

   string aa = "string";

   r.raw ~= aa;
   r.res = cast(void*) aa;
}

}


in file b.d

import a;// import path is okey

class class_b;
{

   void doThings(class_a * ptr_a)
  {
 class_a A = &ptr_a;
 writeln(A.r.raw[0]); // prints aa;
 writeln(A.r.res);// fails : 
core.exception.OutOfMemoryError@(0)

  // but if i do comment the line :
  // writeln(A.r.raw[0]); out, then 
works

  }
}


in file c.d

import a;
import b;

void main() {

clsa = new class_a;
clsb = new class_b;

clsa.dothing();
clsa.doThings( & clsa);


}


I can not find a reason why accessing A.r.raw will erase (invoke 
the garbage collector) to remove A.r.res. Please help.


Re: std.range.byLine

2014-09-12 Thread monarch_dodra via Digitalmars-d-learn

On Friday, 12 September 2014 at 13:25:22 UTC, Nordlöw wrote:
On Thursday, 11 September 2014 at 22:39:40 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:

foreach (line; myInput.split(regex(`\n|\r\n|\r`)))


Shouldn't you use

foreach (line; myInput.split(regex("\n|\r\n|\r")))

here?


Probably not, as (AFAIK) the splitter engine *itself* will *also* 
escape the passed in characters. IE: It literally needs the 
characters '\' and 'n'.


Re: Extremely funny behavior .. could be a bug?

2014-09-12 Thread Ali Çehreli via Digitalmars-d-learn
There are multiple problems with the code. Is that really what you are 
using?


On 09/12/2014 06:35 AM, seany wrote:

consider the following :

in file a.d

module a;

class class_a
{

struct RESULT{
string[] raw;
void* res;
   }


RESULT r;

void dothing()
{
r = new RESULT;


Error: cannot implicitly convert expression (new RESULT) of type RESULT* 
to RESULT




string aa = "string";

r.raw ~= aa;
r.res = cast(void*) aa;
}

}


in file b.d

import a;// import path is okey

class class_b;


That semicolon should not be there?


{

void doThings(class_a * ptr_a)
   {
  class_a A = &ptr_a;


Error: cannot implicitly convert expression (& ptr_a) of type class_a** 
to a.class_a


[...]

Ali



Re: Idiomatic async programming like C# async/await

2014-09-12 Thread Cliff via Digitalmars-d-learn

On Friday, 12 September 2014 at 07:15:33 UTC, Kagamin wrote:
async/await is not so much about futures/promises, but 
optimization of IO-bound operations, i.e. when you wait on 
network/disk, you don't consume stack, threads and similar 
resources, an analog in D is vibe.d


I should have been more clear - it's not the async/await bit I am
interested in so much as the Task behavior - that I have some
object which represents the (future) completed state of a task
without the recipient of that object having to know what the type
of the task function is as they are only interested in the task
result.

I'll take a closer look at vibe.d and see if they already have a
system representing this before I cook up my own.


Re: Extremely funny behavior .. could be a bug?

2014-09-12 Thread seany via Digitalmars-d-learn

On Friday, 12 September 2014 at 15:26:34 UTC, Ali Çehreli wrote:


you are right, it was a reduced form, of a very complex software.

in file a.d

module a;

class class_a
{

   struct RESULT{
   string[] raw;
   void* res;
  }


RESULT * r;

void dothing()
{
   r = new RESULT;

   string aa = "string";

   r.raw ~= aa;
   r.res = cast(void*) aa;
}

}


in file b.d

import a;// import path is okey

class class_b
{

   void doThings(class_a * ptr_a)
  {
 class_a A = *ptr_a;
 writeln(A.r.raw[0]); // prints aa;
 writeln(A.r.res);// fails : 
core.exception.OutOfMemoryError@(0)

  // but if i do comment the line :
  // writeln(A.r.raw[0]); out, then 
works

  }
}


in file c.d

import a;
import b;

void main() {

clsa = new class_a;
clsb = new class_b;

clsa.dothing();
clsa.doThings( & clsa);


}


Re: std.range.byLine

2014-09-12 Thread Nordlöw

On Friday, 12 September 2014 at 14:16:07 UTC, monarch_dodra wrote:
Probably not, as (AFAIK) the splitter engine *itself* will 
*also* escape the passed in characters. IE: It literally needs 
the characters '\' and 'n'.


I ended up with this.

https://github.com/nordlow/justd/blob/30806a85a5c976f3e891ca11bde3d87a16ecf5e6/algorithm_ex.d#L1858

Does it seem ok?


Re: Extremely funny behavior .. could be a bug?

2014-09-12 Thread Ali Çehreli via Digitalmars-d-learn

On 09/12/2014 12:16 PM, seany wrote:

> On Friday, 12 September 2014 at 15:26:34 UTC, Ali Çehreli wrote:
>
>
> you are right, it was a reduced form, of a very complex software.

Maybe others can figure it out by reading the code but a working code 
that reproduces the problem is very important for me. :)


I still had to do three modifications:

1) In b.d, add 'import std.stdio;'

2) In c.d, use 'auto' when constructing clsa and clsb

3) In c.d, call doThings() on clsb, not clsa

Unfortunately, there were no errors for me with a very recent dmd git 
head and I got the following output:


string
4293D0

Ali



Re: DUB Dependency Tree Configuration Error

2014-09-12 Thread Nordlöw

On Thursday, 11 September 2014 at 09:55:05 UTC, Nordlöw wrote:

On Thursday, 11 September 2014 at 09:53:34 UTC, Nordlöw wrote:

I've tried removing ~/.dub/packages with no progres.


I'm using dub git master.


See https://github.com/D-Programming-Language/dub/issues/418

for an explanation.


Should dmd have given me a warning at least?

2014-09-12 Thread WhatMeWorry via Digitalmars-d-learn


// the following two lines compile cleanly but when executed, I 
get

// D:\Projects\Derelict>02_SimpleOpenGL_3_3_program.exe
// object.Error: Access Violation
// 

string glShadingLangVer = 
to!string(glGetString(GL_SHADING_LANGUAGE_VERSION));

writeln("glShadingLangVer is ", glShadingLangVer);




glGetString has the following signature:

const GLubyte* glGetString(GLenum name);

I presume the const is causing the problem.  Is there a work 
around?


Thanks.


Re: Should dmd have given me a warning at least?

2014-09-12 Thread Ali Çehreli via Digitalmars-d-learn

On 09/12/2014 03:44 PM, WhatMeWorry wrote:


// the following two lines compile cleanly but when executed, I get
// D:\Projects\Derelict>02_SimpleOpenGL_3_3_program.exe
// object.Error: Access Violation
// 

string glShadingLangVer =
to!string(glGetString(GL_SHADING_LANGUAGE_VERSION));


According to the documentation, you are supposed to make sure that 
glGetString does not return 0:


  https://www.opengl.org/sdk/docs/man2/xhtml/glGetString.xml

(See Notes and Errors there.)


writeln("glShadingLangVer is ", glShadingLangVer);




glGetString has the following signature:

const GLubyte* glGetString(GLenum name);

I presume the const is causing the problem.


No, const would have caused a compilation error. Access Violation is a 
runtime error caused by invalid memory access location like 0. ;)


Ali



Re: Is º an unicode alphabetic character?

2014-09-12 Thread AsmMan via Digitalmars-d-learn

On Friday, 12 September 2014 at 07:57:43 UTC, Ali Çehreli wrote:

On 09/11/2014 11:38 PM, AsmMan wrote:

> If I want ASCII and latin only alphabet which range should I
use?
> ie, how should I rewrite is_id() function?

This seems to be it:

import std.stdio;
import std.uni;

void main()
{
alias latin = unicode.script.latin;
assert('ç' in latin);
assert('7' !in latin);

writeln(latin);
}

Ali


Sorry, I shouldn't asked for latin but an alphabet like French 
instead of: 
http://www.importanceoflanguages.com/Images/French/FrenchAlphabet.jpg 
(including the diacritics, of course)


As you mentioned, º happend to be a letter so it still pass in: 
assert('º' in latin);


so isn't different from isAlpha(). Is the UTF-8 table organized 
so that I can use a range (like we do for ASCII ch >= 'a' && ch 
<= 'z' || ch >= 'A' && ch <= 'Z') or should I put these alpha 
characters myself on table and then do look up?


Re: Is º an unicode alphabetic character?

2014-09-12 Thread AsmMan via Digitalmars-d-learn

Thanks Ali, I think I get close:

bool is_id(dchar c)
{
	return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= 0xc0 
&& c <= 0x0d || c >= 0xd8 && c <= 0xf6 || c >= 0xf8 && c <= 0xff;

}

this doesn't include some math symbols. like c >= 0xc0 did.


Re: Should dmd have given me a warning at least?

2014-09-12 Thread WhatMeWorry via Digitalmars-d-learn

On Friday, 12 September 2014 at 22:53:35 UTC, Ali Çehreli wrote:

On 09/12/2014 03:44 PM, WhatMeWorry wrote:


// the following two lines compile cleanly but when executed, 
I get

// D:\Projects\Derelict>02_SimpleOpenGL_3_3_program.exe
// object.Error: Access Violation
// 

string glShadingLangVer =
to!string(glGetString(GL_SHADING_LANGUAGE_VERSION));


According to the documentation, you are supposed to make sure 
that glGetString does not return 0:


  https://www.opengl.org/sdk/docs/man2/xhtml/glGetString.xml

(See Notes and Errors there.)


writeln("glShadingLangVer is ", glShadingLangVer);




glGetString has the following signature:

const GLubyte* glGetString(GLenum name);

I presume the const is causing the problem.


No, const would have caused a compilation error. Access 
Violation is a runtime error caused by invalid memory access 
location like 0. ;)


Ali


Isn't this a contradiction.  The documentation says "glGetString
returns a pointer to a static string..."  But further on down it
then says "If an error is generated, glGetString returns 0."
This is the numeric value zero, right?

So how can glGetString() return both a string and a zero?

Btw, your book is excellent, Ali.



Re: Should dmd have given me a warning at least?

2014-09-12 Thread Ali Çehreli via Digitalmars-d-learn

On 09/12/2014 05:52 PM, WhatMeWorry wrote:

> Isn't this a contradiction.  The documentation says "glGetString
> returns a pointer to a static string..."

They are talking about a C string, which is normally a 'char*' (their 
API returns 'GLubyte*' but it doesn't matter here).


> But further on down it
> then says "If an error is generated, glGetString returns 0."
> This is the numeric value zero, right?
>
> So how can glGetString() return both a string and a zero?

In C (and C++ and D) numerical 0 is a placeholder for the null pointer 
value, whatever the actual null pointer value for that platform may be. 
(As far as I know, the actual null pointer value of all modern systems 
is also 0.)


So, both are pointers: a C string is represented as a char* and 0 is the 
null pointer value. No contradiction there.


I hope others with GL experience will answer your question.

> Btw, your book is excellent, Ali.

Thank you very much. :)

Ali



Why do 'abstract' methods with 'in' or 'out' contracts require a body?

2014-09-12 Thread Trey Brisbane via Digitalmars-d-learn

Hey all,

I have a class method defined like so:

abstract class MyClass {
public:
@property
abstract SomeClassType getField() pure nothrow
out(result) {
assert(result !is null, "Error: getField() returned null.");
}
}

As you can see, this method is abstract, as well as in an 
abstract class. Why, then, do I get the following error when 
compiling it?


"Error: function module.MyClass.getField in and out contracts 
require function body"


Is this a compiler bug, or is there a reason for this?

Thanks for your time!