Bad thread_attachThis() design

2012-01-10 Thread Alex Rønne Petersen

From comments:

/**
 * Registers the calling thread for use with the D Runtime.  If this 
routine
 * is called for a thread which is already registered, the result is 
undefined.

 */

I think this is bad design. There is no good way to know if the current 
thread is attached to the runtime, so how am I supposed to work around this?


- Alex


Re: associative arrays

2012-01-10 Thread Manfred Nowak
dennis luehring wrote:

 so your FileDelete would not return an FileDoesNotExists-Error?
Correct.
 
 would it not help to better understand big code if the remove
 would be renamed to remove_existing or to add something like this?
Maybe.

You possibly know about the `rm'-command of *nix-like systems and the 
by typo inserted space, which makes
  `rm -r *.obj' to
  `rm -r * .obj'
This will certainly result in the nice error-message:
  cannot remove `.obj': No such file or directory

Therefore: trying to help a minority possibly routes the majority.

-manfred 


Taking a function or delegate as argument.

2012-01-10 Thread simendsjo
If I want to have a method taking a callback function, I have to specify 
if it should take a function or delegate even if I don't really care. 
What's the best way to accept either? I cannot see any wrapper for 
something like this in std.typecons.



import std.stdio, std.traits;

void f(int i, void function(int) fn) {
fn(i);
}

void d(int i, void delegate(int) dg) {
dg(i);
}

// ugly..
void g(F)(int i, F callback) {
static assert(isSomeFunction!F, callback is not a function);
static assert(__traits(compiles, { callback(i); }), callback does 
not take int as a parameter);

callback(i);
}

void fcb(int i) {
writeln(i);
}

void main() {

// Error: function t.f (int i, void function(int) fn) is not 
callable using argument types (int,void delegate(int))

// f(1, (int i) { writeln(i); });
f(2, fcb);
d(3, (int i) { writeln(i); });
//Error: function t.d (int i, void delegate(int) dg) is not 
callable using argument types (int,void function(int))

//d(4, fcb);
g(5, fcb);
g(6, (int i) { writeln(i); });
//g(7, 7); // not a function
//g(8, (string s) { }); // does not take int as arg
}



import std.c.windows.windows;

2012-01-10 Thread DNewbie
I'm not sure I understand.. The page at http://dlang.org/windows.html says

  Instead of the:
  #include windows.h
  of C, in D there is:
  import std.c.windows.windows;

However, the samples at 
https://github.com/AndrejMitrovic/DWinProgramming/tree/master/Samples
use

  import win32.windef;
  import win32.winuser;

My question is which one should I use in my programs?

-- 

  D


Re: import std.c.windows.windows;

2012-01-10 Thread Mike Parker

On 1/10/2012 10:24 PM, DNewbie wrote:

I'm not sure I understand.. The page at http://dlang.org/windows.html says

   Instead of the:
   #includewindows.h
   of C, in D there is:
   import std.c.windows.windows;

However, the samples at 
https://github.com/AndrejMitrovic/DWinProgramming/tree/master/Samples
use

   import win32.windef;
   import win32.winuser;

My question is which one should I use in my programs?



Those samples use a binding to the Win32 API[1] that does not ship with 
DMD. So in your case, std.c.windows.windows is probably what you want 
for now.



[1]http://dsource.org/projects/bindings/wiki/WindowsApi


Re: Taking a function or delegate as argument.

2012-01-10 Thread Mike Parker

On 1/10/2012 10:43 PM, Mike Parker wrote:

On 1/10/2012 10:05 PM, simendsjo wrote:

If I want to have a method taking a callback function, I have to specify
if it should take a function or delegate even if I don't really care.
What's the best way to accept either? I cannot see any wrapper for
something like this in std.typecons.


The simple way:

void callback(int i, void delegate(int) dg)
{
dg(i);
}

void callback(int i, void function(int) fn)
{
void wrap(int j)
{
function(j);
}
callback(i, wrap);
}


And of course, wrap should be calling fn(j), not function(j)!


Re: Taking a function or delegate as argument.

2012-01-10 Thread Mike Parker

On 1/10/2012 10:05 PM, simendsjo wrote:

If I want to have a method taking a callback function, I have to specify
if it should take a function or delegate even if I don't really care.
What's the best way to accept either? I cannot see any wrapper for
something like this in std.typecons.


The simple way:

void callback(int i, void delegate(int) dg)
{
dg(i);
}

void callback(int i, void function(int) fn)
{
void wrap(int j)
{
function(j);
}
callback(i, wrap);
}


Re: Taking a function or delegate as argument.

2012-01-10 Thread simendsjo

On 10.01.2012 14:43, Mike Parker wrote:

On 1/10/2012 10:05 PM, simendsjo wrote:

If I want to have a method taking a callback function, I have to specify
if it should take a function or delegate even if I don't really care.
What's the best way to accept either? I cannot see any wrapper for
something like this in std.typecons.


The simple way:

void callback(int i, void delegate(int) dg)
{
dg(i);
}

void callback(int i, void function(int) fn)
{
void wrap(int j)
{
function(j);
}
callback(i, wrap);
}


Yeah, but a bit tedious.. I found toDelegate: 
http://dlang.org/phobos/std_functional.html#toDelegate


Re: import std.c.windows.windows;

2012-01-10 Thread DNewbie
On Tue, Jan 10, 2012, at 10:37 PM, Mike Parker wrote:
 
 Those samples use a binding to the Win32 API[1] that does not ship with 
 DMD. So in your case, std.c.windows.windows is probably what you want 
 for now.
 
 
 [1]http://dsource.org/projects/bindings/wiki/WindowsApi
 

Ok.. I still don't understand why such a 'binding' is needed.
Is std.c.windows.windows not enough for everyone?

-- 
  
  D


Re: import std.c.windows.windows;

2012-01-10 Thread Andrej Mitrovic
std.c.windows.windows is missing *a lot* of definitions. It also
doesn't provide aliases to ASCII/UTF16 functions like WindowsAPI does
via the Unicode flag, so you have to explicitly use e.g.
MessageBoxA/MessageBoxW instead of MessageBox. WindowsAPI is nicely
modularized, and is based on existing MinGW headers.


Re: Taking a function or delegate as argument.

2012-01-10 Thread Jacob Carlborg

On 2012-01-10 14:48, simendsjo wrote:

On 10.01.2012 14:43, Mike Parker wrote:

On 1/10/2012 10:05 PM, simendsjo wrote:

If I want to have a method taking a callback function, I have to specify
if it should take a function or delegate even if I don't really care.
What's the best way to accept either? I cannot see any wrapper for
something like this in std.typecons.


The simple way:

void callback(int i, void delegate(int) dg)
{
dg(i);
}

void callback(int i, void function(int) fn)
{
void wrap(int j)
{
function(j);
}
callback(i, wrap);
}


Yeah, but a bit tedious.. I found toDelegate:
http://dlang.org/phobos/std_functional.html#toDelegate


Or make it a template parameter and check if it's callable using 
std.traits.isCallable.


--
/Jacob Carlborg


Re: Taking a function or delegate as argument.

2012-01-10 Thread simendsjo

On 10.01.2012 14:43, Mike Parker wrote:

On 1/10/2012 10:05 PM, simendsjo wrote:

If I want to have a method taking a callback function, I have to specify
if it should take a function or delegate even if I don't really care.
What's the best way to accept either? I cannot see any wrapper for
something like this in std.typecons.


The simple way:

void callback(int i, void delegate(int) dg)
{
dg(i);
}

void callback(int i, void function(int) fn)
{
void wrap(int j)
{
function(j);
}
callback(i, wrap);
}


I tried the following, but I get some error messages:

h(9, (int i) { writeln(i); });

t.d(46): Error: template t.h(F) if (isCompatibleFunction!(F,void 
function(int))) does not match any function template declaration
t.d(46): Error: template t.h(F) if (isCompatibleFunction!(F,void 
function(int))) cannot deduce template function from argument types 
!()(int,void delegate(int))



template isCompatibleFunction(Src, Dest) {
static assert(isSomeFunction!Src, Source is not a function);
static assert(isSomeFunction!Dest, Destination is not a function);
enum bool isCompatibleFunction =
is(ParameterTypeTuple!Src == ParameterTypeTuple!Dest) 
is(ParameterStorageClassTuple!Src == 
ParameterStorageClassTuple!Dest) 

is(ReturnType!Src == ReturnType!Dest);
}

void h(F)(int i, F callback) if(isCompatibleFunction!(F, void 
function(int))) {

callback(i);
}



Re: Taking a function or delegate as argument.

2012-01-10 Thread simendsjo

On 10.01.2012 15:53, Jacob Carlborg wrote:

On 2012-01-10 14:48, simendsjo wrote:

On 10.01.2012 14:43, Mike Parker wrote:

On 1/10/2012 10:05 PM, simendsjo wrote:

If I want to have a method taking a callback function, I have to
specify
if it should take a function or delegate even if I don't really care.
What's the best way to accept either? I cannot see any wrapper for
something like this in std.typecons.


The simple way:

void callback(int i, void delegate(int) dg)
{
dg(i);
}

void callback(int i, void function(int) fn)
{
void wrap(int j)
{
function(j);
}
callback(i, wrap);
}


Yeah, but a bit tedious.. I found toDelegate:
http://dlang.org/phobos/std_functional.html#toDelegate


Or make it a template parameter and check if it's callable using
std.traits.isCallable.



Like this?
void callback(F)(int i, F fn) if(isCallable!F) {
  fn(i);
}

.. but then the parameters wouldn't be documented.


Re: Taking a function or delegate as argument.

2012-01-10 Thread bls

On 01/10/2012 06:53 AM, Jacob Carlborg wrote:

On 2012-01-10 14:48, simendsjo wrote:

On 10.01.2012 14:43, Mike Parker wrote:

On 1/10/2012 10:05 PM, simendsjo wrote:

If I want to have a method taking a callback function, I have to
specify
if it should take a function or delegate even if I don't really care.
What's the best way to accept either? I cannot see any wrapper for
something like this in std.typecons.


The simple way:

void callback(int i, void delegate(int) dg)
{
dg(i);
}

void callback(int i, void function(int) fn)
{
void wrap(int j)
{
function(j);
}
callback(i, wrap);
}


Yeah, but a bit tedious.. I found toDelegate:
http://dlang.org/phobos/std_functional.html#toDelegate


Or make it a template parameter and check if it's callable using
std.traits.isCallable.


What's wrong with toDelegate ? Seems to be pretty handy.

//simple snip
import std.functional;

int main()
{
int delegate( int i) dg;
alias dg callback;
callback = toDelegate(test);
writeln( callback( 12 ) );
readln();

return 0;
}

int test(int i) { return 30 +i;}




Re: Taking a function or delegate as argument.

2012-01-10 Thread Jacob Carlborg

On 2012-01-10 20:24, bls wrote:

On 01/10/2012 06:53 AM, Jacob Carlborg wrote:

On 2012-01-10 14:48, simendsjo wrote:

On 10.01.2012 14:43, Mike Parker wrote:

On 1/10/2012 10:05 PM, simendsjo wrote:

If I want to have a method taking a callback function, I have to
specify
if it should take a function or delegate even if I don't really care.
What's the best way to accept either? I cannot see any wrapper for
something like this in std.typecons.


The simple way:

void callback(int i, void delegate(int) dg)
{
dg(i);
}

void callback(int i, void function(int) fn)
{
void wrap(int j)
{
function(j);
}
callback(i, wrap);
}


Yeah, but a bit tedious.. I found toDelegate:
http://dlang.org/phobos/std_functional.html#toDelegate


Or make it a template parameter and check if it's callable using
std.traits.isCallable.


What's wrong with toDelegate ? Seems to be pretty handy.

//simple snip
import std.functional;

int main()
{
int delegate( int i) dg;
alias dg callback;
callback = toDelegate(test);
writeln( callback( 12 ) );
readln();

return 0;
}

int test(int i) { return 30 +i;}


A template parameter with a template constraint will accept any callable 
type. Function pointer, delegate, struct/class overloading the call 
operator and so on.


--
/Jacob Carlborg


etc.c.curl...?

2012-01-10 Thread simendsjo

Anyone using this module?
For a complete curl newbie, it's far from simple.. The names have 
changed, and are inconsistent. Is it CurlOpt.write_function or 
CurlOpt.writefunction? CurlOpt.connect_only or CurlOpt.connectonly?


I'm having a hard time finding any tutorials on libcurl. Or rather... 
I'm having a hard time finding example code on doing the most elementary 
task: Get a html page as a string! :|


The simplest c example shows how to do this in 4 lines, but not how to 
get the actual result! Where is the page I just downloaded?


auto curl = curl_easy_init();
assert(curl);
scope(exit) curl_easy_cleanup(curl);

curl_easy_setopt(curl, CurlOption.url, toStringz(url));
assert(curl_easy_perform(curl) == 0);

And now..?


Re: etc.c.curl...?

2012-01-10 Thread Jimmy Cao
There's a wrapper for it that will be included in Phobos.  This wrapper is
easier to work with for D.

It has  structs for HTTP, FTP and SMTP.

https://github.com/jcd/phobos/blob/curl-wrapper/etc/curl.d

2012/1/10 simendsjo simend...@gmail.com

 Anyone using this module?
 For a complete curl newbie, it's far from simple.. The names have changed,
 and are inconsistent. Is it CurlOpt.write_function or
 CurlOpt.writefunction? CurlOpt.connect_only or CurlOpt.connectonly?

 I'm having a hard time finding any tutorials on libcurl. Or rather... I'm
 having a hard time finding example code on doing the most elementary task:
 Get a html page as a string! :|

 The simplest c example shows how to do this in 4 lines, but not how to get
 the actual result! Where is the page I just downloaded?

auto curl = curl_easy_init();
assert(curl);
scope(exit) curl_easy_cleanup(curl);

curl_easy_setopt(curl, CurlOption.url, toStringz(url));
assert(curl_easy_perform(curl) == 0);

 And now..?



Re: etc.c.curl...?

2012-01-10 Thread Jonathan M Davis
On Tuesday, January 10, 2012 16:01:01 Jimmy Cao wrote:
 There's a wrapper for it that will be included in Phobos. This wrapper is
 easier to work with for D.
 
 It has structs for HTTP, FTP and SMTP.
 
 https://github.com/jcd/phobos/blob/curl-wrapper/etc/curl.d

Yeah. That would be the better route. etc.c.curl is basically just bindings to 
the C library, so for the most part, any usability issues originate with 
libcurl, and it's libcurl documentation that you'll need in order to figure out 
to use it. But the idea is that you'd use a D wrapper with a much friendlier 
API. The one above has been voted into Phobos but hasn't been merged in yet 
(and may end up with some minor tweaks before it does, but what's in that 
repository should be essentially what's going to be merged in for either 2.058 
or 2.059 - depending on how soon a pull request for it is generated and how 
long it takes for it to actually be merged in).

- Jonathan M Davis


Re: Taking a function or delegate as argument.

2012-01-10 Thread Stewart Gordon

On 10/01/2012 19:56, Jacob Carlborg wrote:
snip

A template parameter with a template constraint will accept any callable type. 
Function
pointer, delegate, struct/class overloading the call operator and so on.


Indeed, this is done in the C++ STL quite a lot.

The drawback is that templated methods lose their virtuality, because it cannot be known 
in advance on what types the template will be instantiated in order to populate the vtable.


FWIW my utility library includes a delegate wrapper:
http://pr.stewartsplace.org.uk/d/sutil/

(dgwrap works in both D1 and D2, though other bits of the library need updating 
to current D2)

Stewart.


Re: Taking a function or delegate as argument.

2012-01-10 Thread Stewart Gordon

On 10/01/2012 19:56, Jacob Carlborg wrote:
snip

A template parameter with a template constraint will accept any callable type. 
Function
pointer, delegate, struct/class overloading the call operator and so on.


Moreover, if you want to save the callback for later use, you need to 
distinguish the cases.

But it really just boils down to:
- if it's a global or static function, wrap it in a delegate
- if it's a type with static opCall, wrap class.opCall in a delegate
- if it's an object with an opCall, just use obj.opCall

I've just had a look at std.functional.toDelegate and it seems it does this straight off. 
 But the way it wraps a static function in a delegate is a lot more complicated than what 
my library does - is this just in order to support non-D linkage?


And I see it has the same limitation of not supporting variadics.

Stewart.


Re: import std.c.windows.windows;

2012-01-10 Thread Mike Parker

On 1/10/2012 11:44 PM, Andrej Mitrovic wrote:

std.c.windows.windows is missing *a lot* of definitions. It also
doesn't provide aliases to ASCII/UTF16 functions like WindowsAPI does
via the Unicode flag, so you have to explicitly use e.g.
MessageBoxA/MessageBoxW instead of MessageBox. WindowsAPI is nicely
modularized, and is based on existing MinGW headers.


Yes, but it's not necessarily the best place to start for someone who 
hasn't figured out the toolchain yet.


Re: import std.c.windows.windows;

2012-01-10 Thread Mike Parker

On 1/10/2012 10:57 PM, DNewbie wrote:

On Tue, Jan 10, 2012, at 10:37 PM, Mike Parker wrote:


Those samples use a binding to the Win32 API[1] that does not ship with
DMD. So in your case, std.c.windows.windows is probably what you want
for now.


[1]http://dsource.org/projects/bindings/wiki/WindowsApi



Ok.. I still don't understand why such a 'binding' is needed.
Is std.c.windows.windows not enough for everyone?



Unfortunately, no. It is not a complete binding of the Win32 API. It has 
a lot of stuff, but if you need more you need to look elsewhere. Andrej 
hit on some of the deficiencies in his post.


Const lazy arguments?

2012-01-10 Thread bearophile
I ask here first before submitting about this to Bugzilla.

If lazy arguments can't be lvalues:


void foo(lazy int x) {
x = x;
}
void main() {}

==
test.d(2): Error: lazy variables cannot be lvalues


What's the point of accepting const lazy arguments?

void foo(const lazy int x) {}
void main() {}

Bye,
bearophile


Re: Taking a function or delegate as argument.

2012-01-10 Thread Jacob Carlborg

On 2012-01-11 02:05, Stewart Gordon wrote:

On 10/01/2012 19:56, Jacob Carlborg wrote:
snip

A template parameter with a template constraint will accept any
callable type. Function
pointer, delegate, struct/class overloading the call operator and so on.


Indeed, this is done in the C++ STL quite a lot.

The drawback is that templated methods lose their virtuality, because it
cannot be known in advance on what types the template will be
instantiated in order to populate the vtable.

FWIW my utility library includes a delegate wrapper:
http://pr.stewartsplace.org.uk/d/sutil/

(dgwrap works in both D1 and D2, though other bits of the library need
updating to current D2)

Stewart.


Yeah, it all depends on what the needs are.

--
/Jacob Carlborg


Re: Taking a function or delegate as argument.

2012-01-10 Thread Jacob Carlborg

On 2012-01-11 02:21, Stewart Gordon wrote:

On 10/01/2012 19:56, Jacob Carlborg wrote:
snip

A template parameter with a template constraint will accept any
callable type. Function
pointer, delegate, struct/class overloading the call operator and so on.


Moreover, if you want to save the callback for later use, you need to
distinguish the cases.

But it really just boils down to:
- if it's a global or static function, wrap it in a delegate
- if it's a type with static opCall, wrap class.opCall in a delegate
- if it's an object with an opCall, just use obj.opCall

I've just had a look at std.functional.toDelegate and it seems it does
this straight off. But the way it wraps a static function in a delegate
is a lot more complicated than what my library does - is this just in
order to support non-D linkage?

And I see it has the same limitation of not supporting variadics.

Stewart.


I've been doing the same thing myself.

--
/Jacob Carlborg