On Wednesday, 16 October 2013 at 21:11:19 UTC, H. S. Teoh wrote:
On Wed, Oct 16, 2013 at 10:09:50PM +0200, Daniel Davidson wrote:
[...]
I reported my issue with the `chain` function to this NG and tried to start annotating items used by chain with pure to see how far the thread led. Honestly it was quickly clear that it led too far for me to follow it and someone else indicated the problem had to do with Voldermort types. If there is more I could do to "benefit us all", beyond learning how it works and what to avoid in my own code - I
will be glad to try.

Hmm. I just did a quick-n-dirty change to Phobos, and it seems to make chain() usable with pure code. I'm not sure why the compiler didn't infer pure for it -- it should. (Or perhaps I'm missing something obvious -- I didn't run the Phobos unittest so maybe the following
change breaks something.)

- In the Phobos source, edit std/range.d and look for the function `auto chain(Ranges...)(Ranges rs)` (around line 2022 or thereabouts), then
  the struct Result inside this function.
- Find the ctor for this struct (circa line 2074), and annotate it with
  pure.
- Now the following code compiles:

        import std.range;

        auto pureFunc() pure {
                return chain([1,2,3], [2,3,4]);
        }

        void main() {
                auto r = pureFunc();
        }

This is just a hack, of course. The compiler *should* be able to
correctly infer that the ctor is pure. So the real fix is to find out
why the compiler isn't doing that.


T

I am able to see your code work. However, when I make that change and try to use chain in a pure function:

      foreach(dateRate; chain(trisection[1], trisection[2])) {
        Date earlyEnd = min(dateRate.when, end);
result = moveValueInTime(result, ccRate, currentDate, earlyEnd);
        ccRate = dateRate.value;
        currentDate = earlyEnd;
        if(earlyEnd == end) break;
      }

I get: pure function 'plus.tvm.rate_curve.RateCurve.scaleFromTo' cannot call impure function 'std.range.chain!(SortedRange!(const(TimePointValue!(Date, CcRate))[], "a.when < b.when"), SortedRange!(const(TimePointValue!(Date, CcRate))[], "a.when < b.when")).chain.Result.empty'

So it seems more work is needed for real purity.

Thanks
Dan

Reply via email to