www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - should chain be pure

reply "Daniel Davidson" <nospam spam.com> writes:
I would like to correctly annotate my functions with pure. I've 
hit a function that is calling chain which breaks purity. Is 
chain really not pure?

The relevant section of code is:

       ...
       auto sortedRage = assumeSorted!("a.when < 
b.when")(opSlice());
       auto trisection = sortedRage.trisect(needle);
       auto ccRate = Rate(0.0);

       if(trisection[0].length) {
         ccRate = trisection[0][$-1].value;
       }
       foreach(dateRate; chain(trisection[1], trisection[2])) {
       ...

Is there a reasonable work around?

Thanks
Dan
Oct 15 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Daniel Davidson:

 I would like to correctly annotate my functions with pure. I've 
 hit a function that is calling chain which breaks purity. Is 
 chain really not pure?
Phobos is slowly being annotated with pure/nothrow (and safe) but not all functions are already tagged. You could fix the problem sending a Phobos patch. Bye, bearophile
Oct 15 2013
parent reply "Daniel Davidson" <nospam spam.com> writes:
On Tuesday, 15 October 2013 at 13:43:55 UTC, bearophile wrote:
 Daniel Davidson:

 I would like to correctly annotate my functions with pure. 
 I've hit a function that is calling chain which breaks purity. 
 Is chain really not pure?
Phobos is slowly being annotated with pure/nothrow (and safe) but not all functions are already tagged. You could fix the problem sending a Phobos patch. Bye, bearophile
That is probably beyond my pay-grade at the moment :-) If I just add pure to chain and run unittests all sorts of errors cascade and I don't think I'm in a position to pull it off. If you are agreeing that chain should be pure and it is just following all the calls and making all of them pure, until that happens by the professionals - is there a casting solution so I can fake a pure and move on?
Oct 15 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Daniel Davidson:

 If you are agreeing that chain should be pure and it is just 
 following all the calls and making all of them pure, until that 
 happens by the professionals - is there a casting solution so I 
 can fake a pure and move on?
chain is a template, and in Phobos often templates are not annotated with pure/nothrow, the compiler infers those attributes. Regarding your code, perhaps you can put your call in an impure delegate and than cast it, but D has no direct means to "cast" purity, because it's highly unsafe and it's against the idea of having purity in the language. So I suggest to replace the pure in your function/method tree with /*pure*/, and later fix the code if/when chains becomes pure. Bye, bearophile
Oct 15 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 15 October 2013 at 14:15:15 UTC, bearophile wrote:
 Daniel Davidson:

 If you are agreeing that chain should be pure and it is just 
 following all the calls and making all of them pure, until 
 that happens by the professionals - is there a casting 
 solution so I can fake a pure and move on?
chain is a template, and in Phobos often templates are not annotated with pure/nothrow, the compiler infers those attributes. Regarding your code, perhaps you can put your call in an impure delegate and than cast it, but D has no direct means to "cast" purity, because it's highly unsafe and it's against the idea of having purity in the language. So I suggest to replace the pure in your function/method tree with /*pure*/, and later fix the code if/when chains becomes pure. Bye, bearophile
bearophile: The problem is actually with voldemort. Chain is implemented as: auto chain(Arg...)(Args args) { static struct Result //Non template struct { auto front(); //Non template function in a non-template struct. } } The problem is that the whole inference things stops at this level: the attributes of "front" are not infered, so chain is not pure simply because it isn't a template. This could be simply solved by making Result a non voldermort "ChainResult" outside of the body of chain. I'd do this, but Kenji had mentioned before that he thought the attributes should be inferred. So for now, I didn't personally make the effort of doing anything to fix it myself. But if someone else where make the effort, I'd review and probably pull.
Oct 16 2013
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, October 16, 2013 09:29:31 monarch_dodra wrote:
 The problem is that the whole inference things stops at this
 level: the attributes of "front" are not infered, so chain is not
 pure simply because it isn't a template.
http://d.puremagic.com/issues/show_bug.cgi?id=10329 And I think that there are older bug reports which are similar. Regardless, attribute inference is pretty poor right now. A _lot_ of Phobos doesn't have its attributes being properly inferred precisely because the compiler stops at the first level of templated stuff rather than fully inferring a template, which quickly makes attribute inference nearly useless. It should definitely be fixed at the compiler level though. Anything else would just be patching holes in a sinking ship. - Jonathan M Davis
Oct 16 2013