Fibonacci with ranges
Jonathan M Davis
jmdavisProg at gmx.com
Sat Mar 12 02:47:20 PST 2011
On Saturday 12 March 2011 02:31:20 Russel Winder wrote:
> Jonathan,
>
> Thanks for the info, very helpful. One point though:
>
> On Sat, 2011-03-12 at 01:56 -0800, Jonathan M Davis 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.
>
> It seems that D is falling into the same bear trap as C++ (and C?)
> descended into long ago. When a programmer want an int or a long, they
> actually have to decide whether they need a size_t. To be honest this
> is a simple WTF!!!
>
> Go really has this right. int does not exist, neither does long.
> int32, int64 -- no ambiguity. Why C++ and D have to continue with the
> pretence of platform independent types when they are far from platform
> independent seems counter-productive.
>
> <post-facto-rant-warning/>
>
> Thanks for the pointer about the take, I need to select just the last
> entry in the range and return that.
>
> > In any case, it _would_ be nice if the compiler gave a more informative
> > message about _why_ the template failed to instantiate - especially
> > since it's _not_ the template constraint which is the problem - but
> > unfortunately, the compiler just isn't that smart about template
> > instantiation errors.
>
> C++ is bad enough, if D cannot improve on it . . . :-((
size_t pretty much needs to be platform-dependent unless you want to restrict
arrays to uint.size as their maximum size. And that really isn't acceptable for
a systems language ( there are probably other reasons why size_t is need -
that's just the most obvious). size_t is used anywhere that an index would be
used. take is one of those places. Besides indices though, size_t isn't
something that you're going to run into all that often in Phobos.
Regardless, size_t has _nothing_ to do with your problem here. The problem is
that you're trying to pass a long to a uint parameter. That problem would exist
even if uint was used directly instead of size_t. It's a narrowing conversion,
and those require casts. I expect that Go would have exactly the same problem
with int32 and int64 - unless Go allows narrowing conversions without casts like
C and C++ do, which causes a whole host of other problems.
So, while you may not like size_t, it really isn't your problem here. Now, the
error on the failed template instantiation could certainly use some improvement,
but that's completely separate from the type issue. That's just it doing poorly
at reporting the error caused by the type issue.
D certainly could use further improvement with regards to template errors, but
on the whole, D's templates are _far_ better than C++'s.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list