Bartosz Milewski seems to like D more than C++ now :)

Jonathan M Davis jmdavisProg at gmx.com
Fri Sep 20 15:55:43 PDT 2013


On Friday, September 20, 2013 12:29:33 Andrei Alexandrescu wrote:
> On 9/20/13 10:52 AM, Jonathan M Davis wrote:
> > On Friday, September 20, 2013 10:47:15 Andrei Alexandrescu wrote:
> >> On 9/20/13 10:02 AM, Szymon Gatner wrote:
> >>> On Friday, 20 September 2013 at 16:57:43 UTC, Jonathan M Davis wrote:
> >>>> If an object is const, then all of its members are const, which means
> >>>> that any
> >>>> ranges you get from its members will be const, making such ranges
> >>>> useless.
> >>> 
> >>> That is so weird to hear considering I added ranges to my C++ code and
> >>> my Vector<T>::all() const can easily return non-const range even tho
> >>> container is itself const. This kinda looks like D is more limited in
> >>> that area than C++... Or I really am not getting something.
> >> 
> >> Yah, it should be possible for a const container to offer ranges over
> >> its (non-modifiable) elements.
> > 
> > That's probably easy to do if the container is well-written, because the
> > container creates the range. The problem is converting a const range to
> > tail const one, which is what you have to do if you ever end up with a
> > const range for any reason, and if you're using const much, that's
> > trivial to do (especially if you ever have a range as a member variable).
> > Unfortunately, in practice, that conversion is quite difficult to do with
> > user-defined types even though it works just fine with arrays.
> 
> I'm not sure such a conversion is needed all that often.

Every time that there's a member variable which is a range, you end up needing 
it if you ever need to iterate over the members of that range when the object 
itself is const - the prime example being when the range is returned from a 
getter or property function.

I think that you tend to be able to avoid it if you're not doing much OO and 
your code is very functional in nature, but if you've got many objects with 
data, then you start running into it (probably forcing you to move the 
contents of the range into an array in many cases).

Also, there are plenty of folks who tend to try and use const and/or immutable 
as much as possible in their code, and when you do that, ranges become useless 
pretty quickly, whereas if you could easily get a tail-const version of the 
range when slicing it, ranges can then still be useful in code that uses const 
and immutable heavily.

As it stands, you pretty much have to avoid const or immutable when doing much 
with ranges thanks to the lack of tail-const conversion for user-defined 
ranges, and while that certainly isn't a fatal restriction, it's certainly 
frustrating when const and immutable are supposed to be major advantages of D. 
I suspect that the main reason that we don't get more complaints about it is 
because arrays don't have the problem, but the problem does still come up from 
time to time.

- Jonathan M Davis


More information about the Digitalmars-d mailing list