Compiler/Phobos/Types problem — panic level due to timing.

lithium iodate whatdoiknow at doesntexist.net
Sun May 5 19:18:47 UTC 2019


On Sunday, 5 May 2019 at 18:53:08 UTC, Russel Winder wrote:
> Hi,
>
> I had merrily asumed I could implement nth Fibonacci number 
> with:
>
>     takeOne(drop(recurrence!((a, n) => a[n-1] + a[n-2])(zero, 
> one), n)).front
>
> where zero and one are of type BigInt, and n is of type size_t. 
> However both dmd and ldc2 complain saying:
[…]
> I am now at the WTF stage – how can I show this example on 
> Thursday in my DevoxxUK presentation?
>
> I am close to giving up and imbibing of too much Pernod.

`recurrence` takes the `CommonType` of the initial values and 
declares its internal state as an array of this type, however 
when at least one of the values is const or immutable, the 
`CommonType` is const too, or even immutable in the case when all 
values are immutable.
The state being const/immutable means that the following step, 
initializing it, can't work, since, well, the array cannot be 
modified (hence the errors).
I'd say this can be considered to be a bug with `recurrence`.
You can solve this issue by constructing and passing mutable 
versions of `one` and `zero` to `recurrence`.



More information about the Digitalmars-d-learn mailing list