Re: Pure not acting pure.

2011-06-16 Thread Michel Fortin

On 2011-06-15 23:29:46 -0400, Charles McAnany mcana...@rose-hulman.edu said:

Ah, so does the compiler figure out which ones are strongly and weakly 
pure and then optimize as
appropriate? Is there a way to indicate that a function is strongly 
pure? Because it would seem odd

to call a function you thought was pure and wind up with a mutated argument.


Just make sure all the parameters are either const or immutable or 
passed by copy and do not contain any pointer or reference. That'll 
make the function strongly pure, and the compiler will be able optimize.


--
Michel Fortin
michel.for...@michelf.com
http://michelf.com/



Re: Pure not acting pure.

2011-06-16 Thread bearophile
Michel Fortin:

 Just make sure all the parameters are either const or immutable or 
 passed by copy and do not contain any pointer or reference. That'll 
 make the function strongly pure, and the compiler will be able optimize.

I have just done a little test on this, currently DMD calls sqr one time in 
main() only if you add nothrow too:

pure nothrow int sqr(int x) { return x * x; }
int main() {
enum int n = 10;
immutable int r = sqr(n) + sqr(n);
return r;
}

Bye,
bearophile


D documentation

2011-06-16 Thread Lloyd Dupont
I'm working a small but reasonably interesting D project which I'd like to, 
ultimately, open source.

To make it attractive I need to  document my class.
Is there anything remotely like Javadoc which works with D? 



Re: D documentation

2011-06-16 Thread bearophile
Lloyd Dupont:

 I'm working a small but reasonably interesting D project which I'd like to, 
 ultimately, open source.
 To make it attractive I need to  document my class.
 Is there anything remotely like Javadoc which works with D? 

http://www.digitalmars.com/d/2.0/ddoc.html
I suggest you to take a general look at D documentation.

Bye,
bearophile


Anybody want to help this Haskell programmer out about type classes?

2011-06-16 Thread Jimmy Cao
http://stackoverflow.com/questions/6328444/type-classes-in-d

This guy seems to be very optimistic about D's functional possibilities.
 Unfortunately, the only answer there right now is a generic ramble
concerning D.


Re: D documentation

2011-06-16 Thread Lloyd Dupont

Thanks bearophile!

bearophile  wrote in message news:itcs1k$1l8t$1...@digitalmars.com...

Lloyd Dupont:

I'm working a small but reasonably interesting D project which I'd like 
to,

ultimately, open source.
To make it attractive I need to  document my class.
Is there anything remotely like Javadoc which works with D?


http://www.digitalmars.com/d/2.0/ddoc.html
I suggest you to take a general look at D documentation.

Bye,
bearophile 



__traits, compile time member type info

2011-06-16 Thread Lloyd Dupont

I'm trying to build an introspection system for a project I have.
I already have a working template to get members value
=== working members getter ===
Variant GETTER(T, string member)(Object target)
{
   T tt = cast(T)target;
   if (!tt)
   throw new ReflectionException(target is null or not  ~T.stringof 
~ value:  ~target.toString());

   return Variant(__traits(getMember, tt, member));
}
===

Now I'm trying to implement a working setter. I have a problem for testing 
and converts the value.

Ideally I'd like something like the code below
= not compiling setter 
void SETTER(T, string member)(Object target, Variant value)
{
   T tt = cast(T)target;
   if (!tt)
   throw new ReflectionException(target is null or not  ~T.stringof 
~ value:  ~target.toString());


   if(!value.convertsTo!( typeid(__traits(getMember, T, member))) )
   throw new ReflectionException(Can't convert  ~value.stringof ~ to 
 ~typeid(__traits(getMember, T, member)).toString());
   __traits(getMember, tt, member) = value.coerce( 
typeid(__traits(getMember, T, member)) );

}
==

it doesn't compile because I don't know how to get the type, at compile 
time, of member member of type T
how do I do that please? 



Solved!

2011-06-16 Thread Lloyd Dupont

work with typeinfo!! :)

=
void SETTER(T, string member)(Object target, Variant value)
{
   T tt = cast(T)target;
   if (!tt)
   throw new ReflectionException(target is null or not  ~T.stringof 
~ value:  ~target.toString());


   if(!value.convertsTo!( typeof(__traits(getMember, T, member)) ))
   throw new ReflectionException(Can't convert  ~value.stringof ~ to 
 ~typeof(__traits(getMember, T, member)).stringof);
   __traits(getMember, tt, member) = value.coerce!( 
typeof(__traits(getMember, T, member)) );

}
==



Re: __traits, compile time member type info

2011-06-16 Thread Lloyd Dupont

works with typeinfo!

void SETTER(T, string member)(Object target, Variant value)
{
   T tt = cast(T)target;
   if (!tt)
   throw new ReflectionException(target is null or not  ~T.stringof 
~ value:  ~target.toString());


   if(!value.convertsTo!( typeof(__traits(getMember, T, member)) ))
   throw new ReflectionException(Can't convert  ~value.stringof ~ to 
 ~typeof(__traits(getMember, T, member)).stringof);
   __traits(getMember, tt, member) = value.coerce!( 
typeof(__traits(getMember, T, member)) );

}




Lloyd Dupont  wrote in message news:itcthc$1onk$1...@digitalmars.com...

I'm trying to build an introspection system for a project I have.
I already have a working template to get members value
=== working members getter ===
Variant GETTER(T, string member)(Object target)
{
   T tt = cast(T)target;
   if (!tt)
   throw new ReflectionException(target is null or not  ~T.stringof
~ value:  ~target.toString());
   return Variant(__traits(getMember, tt, member));
}
===

Now I'm trying to implement a working setter. I have a problem for testing
and converts the value.
Ideally I'd like something like the code below
= not compiling setter 
void SETTER(T, string member)(Object target, Variant value)
{
   T tt = cast(T)target;
   if (!tt)
   throw new ReflectionException(target is null or not  ~T.stringof
~ value:  ~target.toString());

   if(!value.convertsTo!( typeid(__traits(getMember, T, member))) )
   throw new ReflectionException(Can't convert  ~value.stringof ~ to
 ~typeid(__traits(getMember, T, member)).toString());
   __traits(getMember, tt, member) = value.coerce(
typeid(__traits(getMember, T, member)) );
}
==

it doesn't compile because I don't know how to get the type, at compile
time, of member member of type T
how do I do that please? 



templated overloaded operator problem.. :~

2011-06-16 Thread Lloyd Dupont

I have 2 overload of the opCall() in one of my class.
They cause a compile time error / conflict... any idea on how to solve it?
=
class MemberDesc
{
   Variant opCall(Object target)
   {
   return getter(target);
   }
   void opCall(T)(Object target, T value)
  {
   setter(target, Variant(value));
   }

   const string name;
   const TypeInfo type;
   const Variant function(Object target) getter;
   const void function(Object target, Variant value) setter;
   
   this(
string name, 
TypeInfo type,
Variant function(Object target) getter, 
void function(Object target, Variant value) setter = null)

   {
   this.name = name;
   this.type = type;
   this.getter = getter;
   this.setter = setter;
   }
}
=



Re: templated overloaded operator problem.. :~

2011-06-16 Thread David Nadlinger
Add a pair of parentheses to the first overload to add an empty template 
argument list – currently, template and non-template functions can't be 
overloaded.


David


On 6/16/11 2:59 PM, Lloyd Dupont wrote:

I have 2 overload of the opCall() in one of my class.
They cause a compile time error / conflict... any idea on how to solve it?
=
class MemberDesc
{
Variant opCall(Object target)
{
return getter(target);
}
void opCall(T)(Object target, T value)
{
setter(target, Variant(value));
}

const string name;
const TypeInfo type;
const Variant function(Object target) getter;
const void function(Object target, Variant value) setter;
this(
string name, TypeInfo type,
Variant function(Object target) getter, void function(Object target,
Variant value) setter = null)
{
this.name = name;
this.type = type;
this.getter = getter;
this.setter = setter;
}
}
=





Re: templated overloaded operator problem.. :~

2011-06-16 Thread Lloyd Dupont

Ho thanks, even better than the work around I just found! :)


David Nadlinger  wrote in message news:itcvbj$1td2$1...@digitalmars.com...

Add a pair of parentheses to the first overload to add an empty template
argument list – currently, template and non-template functions can't be
overloaded.

David



Re: Advice on threading/fibers/?

2011-06-16 Thread Lars T. Kyllingstad
On Wed, 15 Jun 2011 23:57:25 +, Justin Whear wrote:

 Consider the following:
 
 You have 10 million data points and you need to apply a multipass
 algorithm to them. Each pass is like a cellular automata: it can read
 from the previous pass but it doesn't know the current values. This
 makes the actual processing of each value trivially parallelizable. The
 actual operation for each value is fairly simple and cheap (essentially
 a multidimensional ancestor-child averaging operation).
 
 After each value has been operated on once, the pass is complete and the
 current and old buffers are switched (conceptually, the current
 buffer can only be written to, the old buffer can only be read--using
 __gshared here).
 
 The number of passes is not fixed; in the course of each value
 operation, an error is computed. When the worst individual error falls
 below a certain threshold, the algorithm is finished. Generally this
 will take between one thousand and ten thousand passes.
 
 How would you go about parallelizing this? My thought is to take the
 map/reduce approach within each pass: each thread/fiber takes a slice of
 the dataset, makes its modifications, then returns an error summary.
 These summaries are quickly combined and the algorithm loop decides
 whether to run again. Each pass shouldn't take more than a second or
 two, so I'm not sure whether introducing the overhead of spawning, say,
 10 threads each pass is worthwhile (times 5000 passes). On the other
 hand, I have plenty of CPUs to throw at it (at least 16 cores, each with
 hyperthreading) and am in a situation where as fast as possible is
 important (while individual datasets may not grow, the number of them
 is).
 
 Any thoughts appreciated.

I would recommend you take a look at the new std.parallelism module, 
which was introduced in the most recent DMD release (2.053):

http://www.d-programming-language.org/phobos-prerelease/
std_parallelism.html

-Lars


smarter reflection issue

2011-06-16 Thread Lloyd Dupont

I have a MemberDesc class which describe a class's members.
I fill it with a template method like that (with GETTER and SETTER some 
other templated method I wrote)

=
MemberDesc MEMBER(T, string memberName)()
{
   TypeInfo ti = typeid( typeof(__traits(getMember, T, memberName)) );

   Variant function(Object target) getter = null;
   getter = GETTER!(T, memberName);


   void function(Object target, Variant value) setter = null;
   setter = SETTER!(T, memberName);

   return new MemberDesc(memberName, ti , getter, setter);
}
=

And it works except that I don't do any check that the setter / getter 
method exist!


I tried something like that

static if( __traits(compiles, __traits(getMember, T, member)) )
{
   getter = GETTER!(T, memberName);
}


but this always fail... mm.. how could I check for the getter?


and i guess it's even harder for the setter!
how about something like that (how to fix it?)
=
static if( __traits(compiles, __traits(getMember, T, member) = 
typeof(__traits(getMember, T, memberName)).init) )

{
   setter = SETTER!(T, memberName);
}
=



Re: Advice on threading/fibers/?

2011-06-16 Thread Jeremy Wright

On 06/16/2011 06:22 AM, Lars T. Kyllingstad wrote:

On Wed, 15 Jun 2011 23:57:25 +, Justin Whear wrote:


Consider the following:

You have 10 million data points and you need to apply a multipass
algorithm to them. Each pass is like a cellular automata: it can read
from the previous pass but it doesn't know the current values. This
makes the actual processing of each value trivially parallelizable. The
actual operation for each value is fairly simple and cheap (essentially
a multidimensional ancestor-child averaging operation).

After each value has been operated on once, the pass is complete and the
current and old buffers are switched (conceptually, the current
buffer can only be written to, the old buffer can only be read--using
__gshared here).

The number of passes is not fixed; in the course of each value
operation, an error is computed. When the worst individual error falls
below a certain threshold, the algorithm is finished. Generally this
will take between one thousand and ten thousand passes.

How would you go about parallelizing this? My thought is to take the
map/reduce approach within each pass: each thread/fiber takes a slice of
the dataset, makes its modifications, then returns an error summary.
These summaries are quickly combined and the algorithm loop decides
whether to run again. Each pass shouldn't take more than a second or
two, so I'm not sure whether introducing the overhead of spawning, say,
10 threads each pass is worthwhile (times 5000 passes). On the other
hand, I have plenty of CPUs to throw at it (at least 16 cores, each with
hyperthreading) and am in a situation where as fast as possible is
important (while individual datasets may not grow, the number of them
is).

Any thoughts appreciated.


I would recommend you take a look at the new std.parallelism module,
which was introduced in the most recent DMD release (2.053):

http://www.d-programming-language.org/phobos-prerelease/
std_parallelism.html

-Lars
I wrote an article on using std.parallelism for a bucket-sort algorithm. 
 http://www.codestrokes.com/archives/116


I hope it helps.


Re: Pure not acting pure.

2011-06-16 Thread Lars T. Kyllingstad
On Thu, 16 Jun 2011 06:52:45 -0400, Michel Fortin wrote:

 On 2011-06-15 23:29:46 -0400, Charles McAnany mcana...@rose-hulman.edu
 said:
 
 Ah, so does the compiler figure out which ones are strongly and weakly
 pure and then optimize as
 appropriate? Is there a way to indicate that a function is strongly
 pure? Because it would seem odd
 to call a function you thought was pure and wind up with a mutated
 argument.
 
 Just make sure all the parameters are either const or immutable or
 passed by copy and do not contain any pointer or reference. That'll make
 the function strongly pure, and the compiler will be able optimize.

If you want a strongly pure function, the parameters need to be immutable 
or implicitly convertible to immutable.  const references may be mutated 
elsewhere.

-Lars


Re: Is it reasonable to learn D

2011-06-16 Thread Jacob Carlborg

On 2011-06-07 22:45, Timon Gehr wrote:

Fabian wrote:

  - There are no maintained GUI libraries


I don't know about this but I think QtD and DWT are still being maintained?


DWT is still maintained, although very slowly.

--
/Jacob Carlborg


Re: Is it reasonable to learn D

2011-06-16 Thread Jacob Carlborg

On 2011-06-07 23:02, Fabian wrote:

  The community does not grow if people stay away because it is small.

Thank you for your answer. - You've got a big point!

  I don't know about this but I think QtD and DWT are still being
  maintained?

I can't see any changes on this web page:
http://www.dsource.org/projects/dwt/wiki


There's currently no reason to update the wiki, please see 
http://hg.dsource.org/projects/dwt2 for changelog.


--
/Jacob Carlborg


Re: Is it reasonable to learn D

2011-06-16 Thread Jacob Carlborg

On 2011-06-07 23:33, Jonathan M Davis wrote:

On 2011-06-07 14:02, Fabian wrote:

The community does not grow if people stay away because it is small.


Thank you for your answer. - You've got a big point!


I don't know about this but I think QtD and DWT are still being
maintained?


I can't see any changes on this web page:
http://www.dsource.org/projects/dwt/wiki

That's too bad :-(


http://hg.dsource.org/projects/dwt2

DWT was recently ported to D2. Also, QtD is definitely under active
development. But I'm sure that there's still plenty of work to be done.

- Jonathan M Davis


There is a lot of work to be done (for DWT) :

* Fixing bugs
* Finish porting the Mac version
* Update to later versions of SWT
* Port 64bit versions

--
/Jacob Carlborg


Re: Install DWT2 using DMD and Tango

2011-06-16 Thread Jacob Carlborg

On 2011-06-08 22:59, Andrew Wiley wrote:

On Wed, Jun 8, 2011 at 11:54 AM, Fabian contact-...@freenet.de
mailto:contact-...@freenet.de wrote:

Hi
I'm trying to install DWT2 to create GUI applications with D.
I have downloaded DWT2 with TortoiseHg already and I've installed
Ruby and Rake. But when I try to build the packages I get the
following error message:

http://imageshack.us/photo/my-images/62/dwt2error.png/

Is there anybody who can help me?
I believe it's possible to use DWT2 and Tango - isn't it?


It's called DWT2 because it's a port of the existing DWT code to D2.
Tango is a replacement standard library for D1, so no, they can't really
be used together.
I'm not sure about the state of DWT1, but it should be fairly stable,
even if it hasn't been worked on recently.

As an overview, D1 originally had Phobos as the standard library, but it
wasn't that great, so an independent team built Tango as a replacement.
Tango hasn't ever officially shipped with DMD, the reference compiler,
but it's pretty much become the standard for D1 (with a few exceptions
and attempts to reconcile it with Phobos).
Nowadays, we have D2 and Phobos2 (which is generally just called
Phobos). There's an ongoing project to port Tango to D2 as well, and
Druntime, the runtime library, has been separated from Phobos2 to
hopefully make Tango and Phobos2 coexist if/when that D2 port is
completed (back in D1, Phobos and Tango both included their own
runtimes, so you couldn't use both at the same time). In the meantime,
Phobos2 far better than Phobos1, and Andrei has been leading its
development.

Learn to Tango with D obviously refers to D1/Tango
The D Programming Language refers to D2/Phobos2

Hopefully that makes everything a bit clearer.


DWT2 is supposed to work with D2/Phobos and D1/Tango, anything other is 
a bug. I haven't compiled DWT with D1/Tango for a while now so it might 
not compile.


--
/Jacob Carlborg


Re: Install DWT2 using DMD and Tango

2011-06-16 Thread Jacob Carlborg

On 2011-06-08 20:54, Fabian wrote:

Hi
I'm trying to install DWT2 to create GUI applications with D.
I have downloaded DWT2 with TortoiseHg already and I've installed Ruby
and Rake. But when I try to build the packages I get the following error
message:

http://imageshack.us/photo/my-images/62/dwt2error.png/

Is there anybody who can help me?
I believe it's possible to use DWT2 and Tango - isn't it?

Greetings Fabian


I haven't tested to compile DWT with D1/Tango for a while so it might 
not be compiling, I'll look in to it.


--
/Jacob Carlborg


Re: Serialization libs?

2011-06-16 Thread Jacob Carlborg

On 2011-06-10 14:07, Robert Clipsham wrote:

On 10/06/2011 08:30, Nick Sabalausky wrote:

Are there any good D2 serialization libs out there that utilize
introspecition (ie, don't have to manually specify all the member of each
type), handle cyclic graphs and have flexible output?


I've never used it, and I don't know if it's any good or even does what
you need, but:

http://dsource.org/projects/orange/


Don't know if it works with the current DMD. I'm also in the middle of a 
complete rewrite of the library. But version 0.0.1 should at least work 
with some version of DMD.


--
/Jacob Carlborg


Re: Pure not acting pure.

2011-06-16 Thread Charles McAnany
Ok, I think I get it. That cleared it up. =).
So, if you have a functioned labelled pure, it's your job to not pass it 
mutable arguments, but the compiler's job to
make sure it doesn't mutate anything not in the arguments. And that's why a 
strongly pure function can call a weakly
pure one - only the first function's internal state can be mutated by a weakly 
pure function.
Thanks!


Re: Pure not acting pure.

2011-06-16 Thread Lars T. Kyllingstad
On Thu, 16 Jun 2011 17:38:27 +, Charles McAnany wrote:

 Ok, I think I get it. That cleared it up. =). So, if you have a
 functioned labelled pure, it's your job to not pass it mutable
 arguments, but the compiler's job to make sure it doesn't mutate
 anything not in the arguments. And that's why a strongly pure function
 can call a weakly pure one - only the first function's internal state
 can be mutated by a weakly pure function. Thanks!

Exactly. :)

-Lars


Re: Pure not acting pure.

2011-06-16 Thread Jonathan M Davis
On 2011-06-16 10:38, Charles McAnany wrote:
 Ok, I think I get it. That cleared it up. =).
 So, if you have a functioned labelled pure, it's your job to not pass it
 mutable arguments, but the compiler's job to make sure it doesn't mutate
 anything not in the arguments. And that's why a strongly pure function can
 call a weakly pure one - only the first function's internal state can be
 mutated by a weakly pure function. Thanks!

Well, essentially. But it's a question of parameters, not arguments. It 
doesn't matter whether you pass the function mutable arguments or not. What 
matters is whether its parameters are immutable or implicitly immutable. If 
they are, then you'll be forced to pass it arguments which are immutable or 
implicitly immutable. If they aren't, then the function is weakly pure and no 
optimizations can take place (but the function still can't access mutable 
global variables and can still be called from other pure functions), 
regardless of how mutable the arguments are.

- Jonathan M Davis


Re: Pure not acting pure.

2011-06-16 Thread Steven Schveighoffer
On Thu, 16 Jun 2011 14:33:17 -0400, Jonathan M Davis jmdavisp...@gmx.com  
wrote:



On 2011-06-16 10:38, Charles McAnany wrote:

Ok, I think I get it. That cleared it up. =).
So, if you have a functioned labelled pure, it's your job to not pass it
mutable arguments, but the compiler's job to make sure it doesn't mutate
anything not in the arguments. And that's why a strongly pure function  
can

call a weakly pure one - only the first function's internal state can be
mutated by a weakly pure function. Thanks!


Well, essentially. But it's a question of parameters, not arguments. It
doesn't matter whether you pass the function mutable arguments or not.


Actually, it can matter.  For instance, a pure function like this:

pure int foo(const(int)* m);

can be strong pure if you pass it a pointer to immutable data.

-Steve


sorting a BigInt[] with algorithm.sort()

2011-06-16 Thread Charles McAnany
Hi, all. This is a strange one.
import std.bigint;
import std.algorithm;
void main(){
BigInt[] ar = [BigInt(2), BigInt(1), BigInt(3)];
sort(ar);
}

object.Exception@src\rt\arraycat.d(40): overlapping array copy

Is this a library bug? Or is sort() not supposed to work on this data?


Oh, additionally,
bool res = ar[0]  ar[1]; compiles and executes just fine, as does the normal 
swapping algorithm:
BigInt t = ar[0];
ar[0] = ar[1];
ar[1] = t;

Thanks,
Charles.


Re: Pure not acting pure.

2011-06-16 Thread Jonathan M Davis
On 2011-06-16 11:59, Steven Schveighoffer wrote:
 On Thu, 16 Jun 2011 14:33:17 -0400, Jonathan M Davis jmdavisp...@gmx.com
 
 wrote:
  On 2011-06-16 10:38, Charles McAnany wrote:
  Ok, I think I get it. That cleared it up. =).
  So, if you have a functioned labelled pure, it's your job to not pass it
  mutable arguments, but the compiler's job to make sure it doesn't mutate
  anything not in the arguments. And that's why a strongly pure function
  can
  call a weakly pure one - only the first function's internal state can be
  mutated by a weakly pure function. Thanks!
  
  Well, essentially. But it's a question of parameters, not arguments. It
  doesn't matter whether you pass the function mutable arguments or not.
 
 Actually, it can matter.  For instance, a pure function like this:
 
 pure int foo(const(int)* m);
 
 can be strong pure if you pass it a pointer to immutable data.

I believe that every time that Don has discussed it, he has made it clear that 
it's entirely a matter of the function's signature and that all parameters 
must be immutable or implicitly convertible to immutable for a function to be 
strongly pure. So, that's what I'm going by. However, I can see how passing 
immutable values to a pure function with const parameters could be optimized 
out just like if the parameters were actually immutable. So, it may be that 
things were changed so that that counts as well. Everything that I've seen 
discussed on it though has been about the parameters all having to be 
immutable or implicitly convertible to immutable.

- Jonathan M Davis


Re: sorting a BigInt[] with algorithm.sort()

2011-06-16 Thread bearophile
Charles McAnany:

 object.Exception@src\rt\arraycat.d(40): overlapping array copy
 
 Is this a library bug? Or is sort() not supposed to work on this data?

It's a known bug:
http://d.puremagic.com/issues/show_bug.cgi?id=5705
(Is Bugzilla down?)

Bye,
bearophile


Re: Pure not acting pure.

2011-06-16 Thread Steven Schveighoffer
On Thu, 16 Jun 2011 16:36:01 -0400, Jonathan M Davis jmdavisp...@gmx.com  
wrote:



On 2011-06-16 11:59, Steven Schveighoffer wrote:
On Thu, 16 Jun 2011 14:33:17 -0400, Jonathan M Davis  
jmdavisp...@gmx.com


wrote:
 On 2011-06-16 10:38, Charles McAnany wrote:
 Ok, I think I get it. That cleared it up. =).
 So, if you have a functioned labelled pure, it's your job to not  
pass it
 mutable arguments, but the compiler's job to make sure it doesn't  
mutate
 anything not in the arguments. And that's why a strongly pure  
function

 can
 call a weakly pure one - only the first function's internal state  
can be

 mutated by a weakly pure function. Thanks!

 Well, essentially. But it's a question of parameters, not arguments.  
It

 doesn't matter whether you pass the function mutable arguments or not.

Actually, it can matter.  For instance, a pure function like this:

pure int foo(const(int)* m);

can be strong pure if you pass it a pointer to immutable data.


I believe that every time that Don has discussed it, he has made it  
clear that
it's entirely a matter of the function's signature and that all  
parameters
must be immutable or implicitly convertible to immutable for a function  
to be
strongly pure. So, that's what I'm going by. However, I can see how  
passing
immutable values to a pure function with const parameters could be  
optimized
out just like if the parameters were actually immutable. So, it may be  
that
things were changed so that that counts as well. Everything that I've  
seen

discussed on it though has been about the parameters all having to be
immutable or implicitly convertible to immutable.


Don's the one who brought it to my attention (have no idea what thread it  
was in...) that the optimizations could be done based on this.


AFAIK, this optimization is not implemented.

-Steve


Re: smarter reflection issue

2011-06-16 Thread Jesse Phillips
MemberDesc MEMBER(T, string memberName)() if(std.traits.hasMember!(T, 
memberName))
{
...
}

Lloyd Dupont Wrote:

 I have a MemberDesc class which describe a class's members.
 I fill it with a template method like that (with GETTER and SETTER some 
 other templated method I wrote)
 =
 MemberDesc MEMBER(T, string memberName)()
 {
 TypeInfo ti = typeid( typeof(__traits(getMember, T, memberName)) );
 
 Variant function(Object target) getter = null;
 getter = GETTER!(T, memberName);
 
 
 void function(Object target, Variant value) setter = null;
 setter = SETTER!(T, memberName);
 
 return new MemberDesc(memberName, ti , getter, setter);
 }
 =
 
 And it works except that I don't do any check that the setter / getter 
 method exist!
 
 I tried something like that
 
 static if( __traits(compiles, __traits(getMember, T, member)) )
 {
 getter = GETTER!(T, memberName);
 }
 
 
 but this always fail... mm.. how could I check for the getter?
 
 
 and i guess it's even harder for the setter!
 how about something like that (how to fix it?)
 =
 static if( __traits(compiles, __traits(getMember, T, member) = 
 typeof(__traits(getMember, T, memberName)).init) )
 {
 setter = SETTER!(T, memberName);
 }
 =
 



Re: Pure not acting pure.

2011-06-16 Thread Jonathan M Davis
On 2011-06-16 13:47, Steven Schveighoffer wrote:
 On Thu, 16 Jun 2011 16:36:01 -0400, Jonathan M Davis jmdavisp...@gmx.com
 
 wrote:
  On 2011-06-16 11:59, Steven Schveighoffer wrote:
  On Thu, 16 Jun 2011 14:33:17 -0400, Jonathan M Davis
  jmdavisp...@gmx.com
  
  wrote:
   On 2011-06-16 10:38, Charles McAnany wrote:
   Ok, I think I get it. That cleared it up. =).
   So, if you have a functioned labelled pure, it's your job to not
  
  pass it
  
   mutable arguments, but the compiler's job to make sure it doesn't
  
  mutate
  
   anything not in the arguments. And that's why a strongly pure
  
  function
  
   can
   call a weakly pure one - only the first function's internal state
  
  can be
  
   mutated by a weakly pure function. Thanks!
   
   Well, essentially. But it's a question of parameters, not arguments.
  
  It
  
   doesn't matter whether you pass the function mutable arguments or not.
  
  Actually, it can matter. For instance, a pure function like this:
  
  pure int foo(const(int)* m);
  
  can be strong pure if you pass it a pointer to immutable data.
  
  I believe that every time that Don has discussed it, he has made it
  clear that
  it's entirely a matter of the function's signature and that all
  parameters
  must be immutable or implicitly convertible to immutable for a function
  to be
  strongly pure. So, that's what I'm going by. However, I can see how
  passing
  immutable values to a pure function with const parameters could be
  optimized
  out just like if the parameters were actually immutable. So, it may be
  that
  things were changed so that that counts as well. Everything that I've
  seen
  discussed on it though has been about the parameters all having to be
  immutable or implicitly convertible to immutable.
 
 Don's the one who brought it to my attention (have no idea what thread it
 was in...) that the optimizations could be done based on this.
 
 AFAIK, this optimization is not implemented.

Well, conceptually, it seems solid. And ultimately, it's up to the compiler 
what it decides is actually strongly pure, so it'll probably be implemented at 
some point. I think that it's safe to say that in the long run at least, we'll 
optimize pure functions as much as we can reasonably get away with. But 
regardless, I'm _very_ glad that Don came up with the weakly pure idea, or 
pure would have been almost unusable.

- Jonathan M Davis


Re: smarter reflection issue

2011-06-16 Thread Lloyd Dupont

Hi Jesse, this won't work!
It's my fault in not explaining my problem well though...

I forget to mention something...
I'm using property syntax and try to call the method

Say I have
=
private int _foo;
@property public int Foo() { return _foo; }
@property public void Foo(int value) { _foo = value; }
=
if I call MEMBER(MyClass, Foo)
I'd like to do some static test for int Foo() and void Foo(int)

So... how will I go on solving that?



Jesse Phillips  wrote in message news:itdqs4$mv0$1...@digitalmars.com...

MemberDesc MEMBER(T, string memberName)() if(std.traits.hasMember!(T, 
memberName))

{
...
}

Lloyd Dupont Wrote:


I have a MemberDesc class which describe a class's members.
I fill it with a template method like that (with GETTER and SETTER some
other templated method I wrote)
=
MemberDesc MEMBER(T, string memberName)()
{
TypeInfo ti = typeid( typeof(__traits(getMember, T, memberName)) );

Variant function(Object target) getter = null;
getter = GETTER!(T, memberName);


void function(Object target, Variant value) setter = null;
setter = SETTER!(T, memberName);

return new MemberDesc(memberName, ti , getter, setter);
}
=

And it works except that I don't do any check that the setter / getter
method exist!

I tried something like that

static if( __traits(compiles, __traits(getMember, T, member)) )
{
getter = GETTER!(T, memberName);
}


but this always fail... mm.. how could I check for the getter?


and i guess it's even harder for the setter!
how about something like that (how to fix it?)
=
static if( __traits(compiles, __traits(getMember, T, member) =
typeof(__traits(getMember, T, memberName)).init) )
{
setter = SETTER!(T, memberName);
}
=



allocating gobs of memory to my program

2011-06-16 Thread Charles McAnany
Hi, all. I'm back! I've got an enormous array that I need to store,
preferably in RAM. (It's iterated a bunch.)
I have 16 Gb on my machine, and at any time, about 12 Gb is free.
I'd like to be able to use about 10 Gb for this program. But when I
try to use more than about 800 Mb, I get Memory allocation failed.
(I'm using new long[], not malloc(), but I do free() variables
because the garbage collector was having trouble keeping up with
me.) Is there a switch I can mark to say that the runtime might have
to deal with lots of memory?
Incidentally,
Win7 x64, Intel I7 @4.4 GHz, compiling with dmd -O -release -inline.

Thanks,
Charles


Re: allocating gobs of memory to my program

2011-06-16 Thread David Nadlinger

On 6/17/11 12:32 AM, Charles McAnany wrote:

Win7 x64, Intel I7 @4.4 GHz, compiling with dmd -O -release -inline.


Regardless whether you are running on x86 or x86_64, DMD is only able to 
create 32 bit binaries on Windows.


David


Type qualifiers - inout error

2011-06-16 Thread Claudiu Verdes
Hi,

I'm new to D and trying to follow Alexandrescu's TDPL code examples I came
across an error on the code below:

class A
{
inout(int) val() inout
{
return _val; // error - see below
}
private int _val;
};

The compiler (dmd v2.052) complains on the marked line with the message
Error: inout on return means inout must be on a parameter as well for inout
inout(int)().

What am I doing wrong? TDPL has a very similar example...

Regards,
Claudiu

P.S. Is there a netiquette (a la C++ FAQ lite) about posting on this forum
that I should be aware of?


Instantiating a Tuple with immutable fields.

2011-06-16 Thread Jose Armando Garcia
Hi,

I am trying to instantiate a Tuple that contains an immutable field.
Is there a way to do this with the current implementation? The
compiler gives me this error:

std/typecons.d(383): Error: can only initialize const member
_field_field_0 inside constructor
tuple_test.d(5): Error: template instance
std.typecons.Tuple!(immutable(int)).Tuple.__ctor!(int) error
instantiating
tuple_test.d(3): Error: function D main has no return statement, but
is expected to return a value of type int

here is the code:

import std.typecons;

int main()
{
  Tuple!(immutable(int)) var = Tuple!(immutable(int))(5);
  assert(var[0] == 5);
}

Thanks!


Re: allocating gobs of memory to my program

2011-06-16 Thread Charles McAnany
Hm. I'm not too good on architecture - does that mean it's impossible for an 
x32 program to have access to more memory? 
Is there, maybe, an x64 C library that I could use to abstract the memory out 
(Just a huge array wrapper, basically)? Or, that failing, does GCC 
automatically generate x64 code on an x64 machine? I could probably write the 
procedure in C... but yuck.

David Nadlinger Wrote:

 On 6/17/11 12:32 AM, Charles McAnany wrote:
  Win7 x64, Intel I7 @4.4 GHz, compiling with dmd -O -release -inline.
 
 Regardless whether you are running on x86 or x86_64, DMD is only able to 
 create 32 bit binaries on Windows.
 
 David



Re: allocating gobs of memory to my program

2011-06-16 Thread Jose Armando Garcia
On Thu, Jun 16, 2011 at 10:20 PM, Charles McAnany
mcana...@rose-hulman.edu wrote:
 Hm. I'm not too good on architecture - does that mean it's impossible for an 
 x32 program to have access to more memory?
 Is there, maybe, an x64 C library that I could use to abstract the memory out 
 (Just a huge array wrapper, basically)? Or, that failing, does GCC 
 automatically generate x64 code on an x64 machine? I could probably write the 
 procedure in C... but yuck.

 David Nadlinger Wrote:

 On 6/17/11 12:32 AM, Charles McAnany wrote:
  Win7 x64, Intel I7 @4.4 GHz, compiling with dmd -O -release -inline.

 Regardless whether you are running on x86 or x86_64, DMD is only able to
 create 32 bit binaries on Windows.

 David


I don't know why you can't allocate more than 800mb but if you want to
get around this by caching in other processes' memory then I can
recommend memcache: http://memcached.org/


Re: Type qualifiers - inout error

2011-06-16 Thread bearophile
Claudiu Verdes:

 What am I doing wrong? TDPL has a very similar example...

I think this TDPL example is not good. And generally inout is not fully 
implemented in D yet.


 P.S. Is there a netiquette (a la C++ FAQ lite) about posting on this forum
 that I should be aware of?

I think there isn't one. Anything goes! ;-)

Bye,
bearophile


Re: Instantiating a Tuple with immutable fields.

2011-06-16 Thread bearophile
Jose Armando Garcia:

 I am trying to instantiate a Tuple that contains an immutable field.
 Is there a way to do this with the current implementation?

I think this is not yet possible. I am not sure, but maybe even small changes 
to conts are needed to fix this problem.

Bye,
bearophile


Re: Pure not acting pure.

2011-06-16 Thread bearophile
Jonathan M Davis:

 I'm _very_ glad that Don came up with the weakly pure idea, or 
 pure would have been almost unusable.

I think Don has said this wasn't fully his idea. The D community is good at 
creating ideas in group :-)

Bye,
bearophile


Strange behavior when concatenating array

2011-06-16 Thread Jose Armando Garcia
It looks like the rt is not calling the postblit constructor when
concatenating arrays. For example, the following code:

import std.stdio;

struct Test
{
   this(this) { writeln(copy done); }
   void opAssign(Test rhs) { writeln(assignment done); }
   ~this() { writeln(destructor called); }
}

void main()
{
   Test[] tests = new Test[1];
   {
  Test test;
  tests ~= test;
   }
   writeln(done);
}

Gives the following output:

destructor called
done

The dtr for 'Test test;' is getting call after the scope exits but the
postblit ctr for 'tests[0]' is never called. I believe the output of
this code should either be:

done

or:

copy done
destructor called
done

Thanks!
-Jose


Re: Strange behavior when concatenating array

2011-06-16 Thread Jose Armando Garcia
On Fri, Jun 17, 2011 at 1:05 AM, Jose Armando Garcia jsan...@gmail.com wrote:
      tests ~= test;

Btw, if you replace this with 'test[0] = test;' it works as expected.
The postblit ctor and the assignment operator get called and the dtor
is called twice.