Fibonacci with ranges
Jonathan M Davis
jmdavisProg at gmx.com
Sat Mar 12 03:05:54 PST 2011
On Saturday 12 March 2011 02:48:19 Russel Winder wrote:
> Jonathan,
>
> On Sat, 2011-03-12 at 10:31 +0000, Russel Winder wrote:
> [ . . . ]
>
> > > What's happening is that the parameter that you're passing n to for
> > > recurrence is size_t. And on 32-bit systems, size_t is uint, so
> > > passing n - which is long - to recurrence would be a narrowing
> > > conversion, which requires a cast. The correct thing to do would be
> > > make n a size_t. The other thing that you'd need to do is change
> > > declarative to return auto, since take returns a range, _not_ a long.
>
> To analyse this a bit more I temporarily deconstructed the expression:
>
> long declarative ( immutable long n ) {
> auto r = recurrence ! ( "a[n-1] + a[n-2]" ) ( 0L , 1L ) ;
> auto t = take ( r , cast ( size_t ) ( n ) ) ;
> return t [ n ] ;
> //return ( take ( recurrence ! ( "a[n-1] + a[n-2]" ) ( 0L , 1L )
> , cast ( size_t ) ( n ) ) ) [ n ] ; }
>
> So with the cast it compiles fine -- though it still seems to me to be
> beyond the point of comprehension as to why an applications programmer
> has to manually cast a long to a size_t. However the indexing of the
> range fails:
Um. Because it's a narrowing conversion on 32-bit machines. What else should it
be doing? If it allowed the narrowing conversion without a cast, then you'd run
into problems where you were losing precision without realizing it which would
cause plenty of other entertaining bugs. Most newer languages require casts for
narrowing conversions.
> fibonacci_d2.d(17): Error: no [] operator overload for type
> Take!(Recurrence!(fun,long,2u))
>
> Which elicits the response: for f$$$$ sake, I'm just copying the
> example from the manual.
>
> OK, so I am grumpy this morning, but that doesn't affect the fact that
> there appears to be a disconnect between documentation and what actually
> works.
take will only return a sliceable range if the range that you give it is
sliceable. recurrence does not return a sliceable range, so take used an the
result of recurrence doesn't return a sliceable range. The documentation for
take is completely correct. It's just that it only has an array in its example,
not a range which _isn't_ sliceable, so the one example that it does have
involves a sliceable range.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list