Re: Partial ordering of constructors with type parameters

2014-04-24 Thread monarch_dodra via Digitalmars-d-learn

On Wednesday, 23 April 2014 at 22:07:32 UTC, John Colvin wrote:
On Wednesday, 23 April 2014 at 18:04:05 UTC, monarch_dodra 
wrote:
If you can't update your compiler, an alternative is to make 
your non-template version an actual template:


class Foo{
   this(T : int)(T x){}
   this(T)(T x) {}
}


I haven't tested, but wouldn't this be more precisely 
equivalent?:


class Foo{
this()(int x){}
this(T)(T x) {}
}


*That* creates a conflict though :/


Re: On Concurrency

2014-04-24 Thread Bienlein via Digitalmars-d-learn


One key difference is that coroutines won't make your programs 
run faster. It is a modelling mechanism that can simplify your 
programs where you otherwise would have to implement a state 
machine.


This is also my impression when I look at this code (see 
http://www.99-bottles-of-beer.net/language-d-2547.html) that 
implements 99 bottles of beer in D with fibers. What seems to be 
happening is some alternating handover of the CPU.


But when I run the code all 4 cores of my machine are under load 
and it looks like the runtime were able to make things run in 
parallel somehow. Now I'm really confused ...




Re: Partial ordering of constructors with type parameters

2014-04-24 Thread Andrej Mitrovic via Digitalmars-d-learn
On 4/24/14, monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:
 *That* creates a conflict though :/

Are you sure? I can't reproduce.


Example of parse whole json answer.

2014-04-24 Thread Nicolas via Digitalmars-d-learn
I have a json string saved in a file ( example of json tweeter 
answer: 
https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline 
). I am trying to read the whole json answer and print specific 
data (created_at, retweet_count, ..) . I am new at D 
Programming language and i was wondering if someone can help me 
or post some example that show how to parse json strings using 
std.json library. Thanks in advance.


Re: Example of parse whole json answer.

2014-04-24 Thread Craig Dillabaugh via Digitalmars-d-learn

On Thursday, 24 April 2014 at 12:17:42 UTC, Nicolas wrote:
I have a json string saved in a file ( example of json tweeter 
answer: 
https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline 
). I am trying to read the whole json answer and print specific 
data (created_at, retweet_count, ..) . I am new at D 
Programming language and i was wondering if someone can help me 
or post some example that show how to parse json strings using 
std.json library. Thanks in advance.


Here is something to get you started.  The initial version of 
this code was from Ali Cehreli, I can't remember what 
modifications are mine, or if this is all straight form Ali.


If you intend to do any serious work with Json, I highly 
recommend you check out Vibe.d (www.vibed.org) - more stuff to 
learn but its JSON capabilities are awesome compared to what 
std.json gives you.


import std.stdio;
import std.json;
import std.conv;
import std.file;

/**
  Ali's example.
*/
struct Employee
{
string firstName;
string lastName;
}

void readEmployees( string employee_json_string )
{

	JSONValue[string] document = 
parseJSON(employee_json_string).object;

JSONValue[] employees = document[employees].array;

foreach (employeeJson; employees) {
JSONValue[string] employee = employeeJson.object;

string firstName = employee[firstName].str;
string lastName = employee[lastName].str;

auto e = Employee(firstName, lastName);
writeln(Constructed: , e);
}   
}

void main()
{
// Assumes UTF-8 file
auto content =
`{
  employees: [
{ firstName:Walter , lastName:Bright },
{ firstName:Andrei , lastName:Alexandrescu },
{ firstName:Celine , lastName:Dion }
  ]
}`;

readEmployees( content );

//Or to read the same thing from a file
readEmployees( readText( employees.json ) );

}


Re: Partial ordering of constructors with type parameters

2014-04-24 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 24 April 2014 at 10:12:15 UTC, Andrej Mitrovic via 
Digitalmars-d-learn wrote:

On 4/24/14, monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:

*That* creates a conflict though :/


Are you sure? I can't reproduce.


Weird. I can't either. I probably accidentally tested it on 
2.063? Who cares; yes, it's more practical.


Temporarily protect array from garbage collection

2014-04-24 Thread Lars T. Kyllingstad via Digitalmars-d-learn
Is it possible to temporarily prevent the garbage collector from 
collecting a memory block even if there are no references to it?


The use case is as follows:  I want to call a C library function 
which expects to take ownership of a buffer.  It looks something 
like this:


alias FreeFunc = extern(C) void function(void*, void*) 
nothrow;


extern(C) void foo(void* buf, size_t len,
   FreeFunc free, void* ctx) nothrow;

Here, 'buf' is a pointer to the buffer, 'len' is the length of 
the buffer, 'free' is a function to deallocate the buffer when 
the library is done with it, and 'ctx' is a user-supplied context 
pointer.  Upon deallocation, 'free' receives two parameters; the 
pointer to the buffer and the context pointer.  The latter can be 
anything, even null, as it is just passed to 'free' and not used 
for anything else.


Here is the problem:  I want to be able to use a 
garbage-collected dynamic array with this function, but I don't 
want to have to retain a reference to it in my program.  (I don't 
know when the C library will call the free function.)  In other 
words, I want something like this:


extern(C) void myFree(void* ptr, void* ctx)
{
enableGCFor(ptr);
}

auto arr = new int[123];
disableGCFor(arr);
foo(arr.ptr, arr.length, myFree, null);
arr = null;

Is this at all possible?

Thanks,
Lars


Re: Temporarily protect array from garbage collection

2014-04-24 Thread Justin Whear via Digitalmars-d-learn
On Thu, 24 Apr 2014 19:55:37 +, Lars T. Kyllingstad wrote:

 Is it possible to temporarily prevent the garbage collector from
 collecting a memory block even if there are no references to it?
 
 The use case is as follows:  I want to call a C library function which
 expects to take ownership of a buffer.  It looks something like this:
 
  alias FreeFunc = extern(C) void function(void*, void*)
 nothrow;
 
  extern(C) void foo(void* buf, size_t len,
 FreeFunc free, void* ctx) nothrow;
 
 Here, 'buf' is a pointer to the buffer, 'len' is the length of the
 buffer, 'free' is a function to deallocate the buffer when the library
 is done with it, and 'ctx' is a user-supplied context pointer.  Upon
 deallocation, 'free' receives two parameters; the pointer to the buffer
 and the context pointer.  The latter can be anything, even null, as it
 is just passed to 'free' and not used for anything else.
 
 Here is the problem:  I want to be able to use a garbage-collected
 dynamic array with this function, but I don't want to have to retain a
 reference to it in my program.  (I don't know when the C library will
 call the free function.)  In other words, I want something like this:
 
  extern(C) void myFree(void* ptr, void* ctx)
  {
  enableGCFor(ptr);
  }
 
  auto arr = new int[123];
  disableGCFor(arr);
  foo(arr.ptr, arr.length, myFree, null);
  arr = null;
 
 Is this at all possible?
 
 Thanks,
 Lars

You can use GC.addRoot() from core.memory before passing the pointer to 
the C function, then use GC.removeRoot in your myFree function.


Re: Temporarily protect array from garbage collection

2014-04-24 Thread Lars T. Kyllingstad via Digitalmars-d-learn

On Thursday, 24 April 2014 at 20:09:38 UTC, Justin Whear wrote:
You can use GC.addRoot() from core.memory before passing the 
pointer to

the C function, then use GC.removeRoot in your myFree function.


Perfect, thanks!


Problem with code coverage. No .lst files?

2014-04-24 Thread Jeremy DeHaan via Digitalmars-d-learn

Hey all,

I tried to use code coverage analysis for the first time tonight. 
I added the -cov switch to my unit test build, but no matter what 
I do, I can't seem to locate the produced .lst files. Is there 
something I should know that isn't in the docs that I might be 
doing wrong? I'm not sure what's going on here.


Thanks,
   Jeremy


Re: Problem with code coverage. No .lst files?

2014-04-24 Thread Ali Çehreli via Digitalmars-d-learn

On 04/24/2014 08:32 PM, Jeremy DeHaan wrote:

 added the -cov switch to my unit test build

Then you must execute the program. :)

Ali