Fibonacci with ranges
Russel Winder
russel at russel.org.uk
Sat Mar 12 02:48:19 PST 2011
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:
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.
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at russel.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110312/3b7ee554/attachment.pgp>
More information about the Digitalmars-d-learn
mailing list