Re: Mixin Template: cannot mixin scope(exit)?

2013-01-15 Thread Nicolas Sicard

On Monday, 14 January 2013 at 06:26:33 UTC, 1100110 wrote:

On 01/13/2013 11:35 PM, 1100110 wrote:
Ok, I wish to create a standard timing system so that I can 
measure ~how

long each function takes to execute.

I wish to be able to place at the start of a function
version(Time) mixin TimeExecution(funcName);

mixin template TimeExecution(T) if(isSomeString!T) {
import std.stdio, std.datetime, std.conv;

auto sw = StopWatch(AutoStart.yes);
// Error: Declaration expected, not '('
scope(exit) writeln(T, : , to!Duration(sw.peek));
}


Why do I receive the Error when the scope statement is 
included?

Is this an error, or what is the rationale behind the decision?

Thank you.


It appears that you cannot mixin *any* statement with 
scope([exit,success,etc]) in it.


Mixin templates are supposed to introduce *declarations* not 
statements.


Eg. even this shouldn't compile, should it?
---
mixin template TimeExecution(T) if(isSomeString!T) {
import std.stdio;
writeln(T); // statement
}
---


Re: Mixin Template: cannot mixin scope(exit)?

2013-01-15 Thread mist
I thought template itself should compile but its statement-like 
instantiation should not.


By the way, if all you want is to split out some generic 
statement block without using dirty string mixins, template 
functions with alias parameters may do.

I.e. http://dpaste.1azy.net/68ad8133

Don't know what about inlining for it though.

Mixin templates are supposed to introduce *declarations* not 
statements.


Eg. even this shouldn't compile, should it?
---
mixin template TimeExecution(T) if(isSomeString!T) {
import std.stdio;
writeln(T); // statement
}
---




Re: Mixin Template: cannot mixin scope(exit)?

2013-01-15 Thread Nicolas Sicard

On Tuesday, 15 January 2013 at 11:19:50 UTC, mist wrote:
I thought template itself should compile but its statement-like 
instantiation should not.


The template shouldn't compile: the D grammar says that mixin 
templates inject declarations only. Hence the text of the error.


By the way, if all you want is to split out some generic 
statement block without using dirty string mixins, template 
functions with alias parameters may do.

I.e. http://dpaste.1azy.net/68ad8133


Yes, but only a string mixin can inject a scope statement.



Re: Does the new alias syntax not support extern for function types?

2013-01-15 Thread Mike Parker

On Monday, 14 January 2013 at 21:00:12 UTC, Simen Kjaeraas wrote:

alias foo = extern(System) void function();

Gives me an error about expecting basic type, not extern.


extern(System) alias void function() foo;


Re: Does the new alias syntax not support extern for function types?

2013-01-15 Thread Simen Kjaeraas

On 2013-04-15 13:01, Mike Parker aldac...@gmail.com wrote:


On Monday, 14 January 2013 at 21:00:12 UTC, Simen Kjaeraas wrote:

alias foo = extern(System) void function();

Gives me an error about expecting basic type, not extern.


extern(System) alias void function() foo;


But that's the old syntax, with which one can do this:

alias extern(System) void function() foo;

I'm talking about the new syntax, with =. Apparently, this does
support prefix extern, as you show:

extern(System) alias foo = void function();

I guess that's the solution, but it seems weird to me that it
does not support extern where the old syntax does.

--
Simen


Re: Enhancing foreach

2013-01-15 Thread Ary Borenszweig

On Thursday, 10 January 2013 at 17:36:15 UTC, Raphaël Jakse wrote:

Le 10/01/2013 10:23, monarch_dodra a écrit :
On Thursday, 10 January 2013 at 03:29:21 UTC, Peter Summerland 
wrote:


The only thing I'd want to be able to do is:
//
foreach ( ; 0 .. 5)
{
writeln(hello);
}
//

If I don't need a named variable, why force me to define a 
symbol?

http://d.puremagic.com/issues/show_bug.cgi?id=9009

I know I could just use i and move on, but when code starts 
getting
complex, and you already have i, j, k, ii, dummy etc..., it 
can make a

difference.



What about :

foreach (0 .. 5)
{
writeln(hello);
}

?


What about:

5 {
   writeln(hello);
}

It could even work with floats!

1.5 {
   writeln(nice);
}

prints:

nice
ni


Re: Enhancing foreach

2013-01-15 Thread Raphaël Jakse

Le 15/01/2013 19:41, Ary Borenszweig a écrit :

On Thursday, 10 January 2013 at 17:36:15 UTC, Raphaël Jakse wrote:

Le 10/01/2013 10:23, monarch_dodra a écrit :

On Thursday, 10 January 2013 at 03:29:21 UTC, Peter Summerland wrote:

The only thing I'd want to be able to do is:
//
foreach ( ; 0 .. 5)
{
writeln(hello);
}
//

If I don't need a named variable, why force me to define a symbol?
http://d.puremagic.com/issues/show_bug.cgi?id=9009

I know I could just use i and move on, but when code starts getting
complex, and you already have i, j, k, ii, dummy etc..., it can make a
difference.



What about :

foreach (0 .. 5)
{
writeln(hello);
}

?


What about:

5 {
writeln(hello);
}

It could even work with floats!

1.5 {
writeln(nice);
}

prints:

nice
ni



D should definitively implement the GWPM (Gess What the Programmer 
Meant) compile-time feature. That would be awesome.





Re: Processes

2013-01-15 Thread Nick Sabalausky
On Sun, 13 Jan 2013 15:01:30 +0100
Tomas butkustomas...@gmail.com wrote:

 Hey, guys.
 
 I need to get all running processes list, and kill one. (example: 
 Find all processes and if skype.exe is running, kill it.)

Sounds like a fantastic tool! I'd love to see it when it's done! I
might actually start using the PC version of Skype :)



Re: Processes

2013-01-15 Thread David
 ---
 import std.stdio;
 import std.process;
 
 //assuming we want to kill htop
 void main() {
 killProcess(htop);
 }
 
 void killProcess(string n){
 version(linux) {
 auto processNumber = shell(pgrep ~ n);
 writeln(n ~ process number is: ~processNumber);
 
 shell(kill ~ processNumber);
 writeln(n ~ has been killed.);
 }
 
 version(Windows) {// I don't know windows enough, your turn.
 
 }
 }
 ---

If you're already using `shell`: shell(killall %s.format(process));



using sqlite3

2013-01-15 Thread Knud Soerensen
When using etc.c.sqlite3  i get the following errors.

usr/include/dmd/phobos/etc/c/sqlite3.d:(.text._Dmain+0x98): undefined
reference to `sqlite3_open'
/usr/include/dmd/phobos/etc/c/sqlite3.d:(.text._Dmain+0xa7): undefined
reference to `sqlite3_errmsg'
/usr/include/dmd/phobos/etc/c/sqlite3.d:(.text._Dmain+0xcc): undefined
reference to `sqlite3_close'

What am I doing wrong ?


My code look like this.



import etc.c.sqlite3;

void main()
{

 sqlite3* db;
  int code=sqlite3_open(file.db, db);

  if (SQLITE_OK != code)
   {
  printf(DB create error: %s\n, sqlite3_errmsg(db));
   }
   printf(DB open!\n);

   sqlite3_close(db);
   printf(DB closed.\n);


}


-- 
Join me on
Skype  knudhs
Facebook   http://www.facebook.com/profile.php?id=1198821880
Linkedin   http://www.linkedin.com/pub/0/117/a54
Twitterhttp://twitter.com/knudsoerensen
bitcoin donations: 13ofyUKqFL43uRJHZtNozyMVP4qxKPsAR2


Re: using sqlite3

2013-01-15 Thread Adam D. Ruppe
You might have to add -L-lsqlite3 to your dmd command line, to 
link in the library.


May also be necessary to install the sqlite library, e.g. yum 
install sqlite-devel on red hat linuxes.


Re: using sqlite3

2013-01-15 Thread Knud Soerensen
On 2013-01-15 22:02, Adam D. Ruppe wrote:
 You might have to add -L-lsqlite3 to your dmd command line, to link in
 the library.
 
 May also be necessary to install the sqlite library, e.g. yum install
 sqlite-devel on red hat linuxes.
Thanks, I just added it to my /etc/dmd.conf



-- 
Join me on
Skype  knudhs
Facebook   http://www.facebook.com/profile.php?id=1198821880
Linkedin   http://www.linkedin.com/pub/0/117/a54
Twitterhttp://twitter.com/knudsoerensen
bitcoin donations: 13ofyUKqFL43uRJHZtNozyMVP4qxKPsAR2


Re: static vs non-static

2013-01-15 Thread Era Scarecrow

On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:

Maxim Fomin:


dmd allows to call static functions on instance.


I think that's a D design mistake (and I think Jonathan Davis 
agrees with me), but Walter prefers the current behavour.


 I'll have to disagree and it should remain an error.

 Consider his example, if it were allowed and you do:

 Gun gun;
 Gun.bar();
 gun.bar();

 What would happen? The first would call the static and the 
second would call the non-static. Overloading-wise their 
signatures are identical (static doesn't count). The difference 
in the calls is a single letter and can easily go under the 
radar; Without an actual difference in the signature it would be 
easy to confuse the two. Now had one required an input then they 
couldn't be messed up.


 Let's go with a slightly more confounding example.
  struct S {
int x;
int opIndex(int i){return i;}

//non-instance is always 0
static int length() {return 0;}
int length() const {return x;}
  }

 Assuming a function should call this (Say a foreach? once 
there's more methods?), which is the correct length to call? If 
the static is preferred then length is always 0, if non-static is 
preferred (although slimmer) the length of 0 being returned by 
accident is always there. If the static length is private, then 
inside the struct S you always have to reference 'this.length' vs 
'S.length' to keep them separate just to be sure.



 I recall reading somewhere that in Java that technically calling 
a static function by a instance was an error but instead only 
gave you a warning; Kinda makes sense since methods affect it's 
instance but static functions don't have an instance to work 
with; They were more for functions that needed to be free but 
couldn't due to the strict 'everything is an object' setup.


Re: parallel() and random number generation

2013-01-15 Thread Nathan M. Swan
On Monday, 14 January 2013 at 22:24:22 UTC, Joseph Rushton 
Wakeling wrote:

Hello all,

One of the claims made for pseudo-random number generation in D 
is that rndGen (default RNG) is thread-safe, that is, each 
instance is unique to its thread and is seeded with 
unpredictableSeed, which should strongly limit the chances of 
two threads having correlated sequences of pseudo-random 
numbers.


Now consider the following code:


import std.random, std.range, std.stdio;

void main()
{
  rndGen.seed(1001);

  foreach(i; iota(12))
writeln(uniform(0.0, 1.0));
}


Obviously, because we seed rndGen, this produces exactly the 
same sequence every time.  But now suppose we use a parallel 
foreach:



import std.parallelism, std.random, std.range, std.stdio;

void main()
{
  rndGen.seed(1001);

  foreach(i; iota(12).parallel())
writeln(uniform(0.0, 1.0));
}


Now, I'd expect that suddenly a number of the random variates 
would suddenly become unpredictable with each run, and that the 
number thereof would be proportional to the number of threads 
-- so with 2 threads, we'd expect half the numbers to suddenly 
be unpredictable with each run -- because only one thread would 
be using the seeded pseudo-random sequence, and the others 
would be using a separate rndGen with unpredictable seed.


But actually, in my experience, the number of random variates 
that differ from the predictable sequence is not in proportion 
to the number of threads and often corresponds only to the last 
3-4 variates.


This is a bit worrying, because it raises the question of 
whether the same rndGen is being used in the different threads, 
and thus whether in fact threads might generate correlated 
random sequences.


Advice/thoughts/explanations?

Thanks  best wishes,

 -- Joe


It's thread-safe when you use std.concurrency; std.parallelism is 
_not_ really safe at all.


NMS


Re: static vs non-static

2013-01-15 Thread Maxim Fomin

On Tuesday, 15 January 2013 at 22:03:24 UTC, Era Scarecrow wrote:

On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:

Maxim Fomin:


dmd allows to call static functions on instance.


I think that's a D design mistake (and I think Jonathan Davis 
agrees with me), but Walter prefers the current behavour.


 I'll have to disagree and it should remain an error.

 Consider his example, if it were allowed and you do:

 Gun gun;
 Gun.bar();
 gun.bar();



The problem is that code depends on presence of static and 
no-static functions. If no one is preferred, compiler would issue 
error (like dmd behaves today), if one of them is preferred (for 
e.x. non-static) the semantic would be silently changed if you 
write gun.bar() and then add non-static function to class.


Is this a bug?

2013-01-15 Thread Jordi Sayol
Hello,

When compiling next code to 64-bit.

app.d

import std.stdio;

void main()
{
ubyte u1 = cast(byte)-1;
byte u2 = cast(short)-1;
uint u3 = cast(int)-1;
int u4 = cast(long)-1;
writefln(%d, u1);
writefln(%d, u2);
writefln(%d, u3);
writefln(%d\n, u4);

writefln(long.sizeof: %d  ulong.max: %20d, ulong.sizeof, ulong.max);
writefln(long.sizeof: %d   long max: %20d, long.sizeof, long.max);
}


Prints this:

255
-1
4294967295
-1

long.sizeof: 8  ulong.max:   4294967295
long.sizeof: 8   long max:  9223372036854775807


ulong.max value is incorrect

Tested with dmd version 2.056, 2.057, 2.058, 2.059, 2.060 and 2.061 

No problem when compiling to 32-bit.

-- 
Jordi Sayol