Why do we have transitive const, again?

Jonathan M Davis jmdavisProg at gmx.com
Fri Sep 23 15:56:42 PDT 2011


On Friday, September 23, 2011 15:27 Mehrdad wrote:
> On 9/23/2011 2:55 PM, Jonathan M Davis wrote:
> > On Friday, September 23, 2011 14:37 Mehrdad wrote:
> >> On 9/23/2011 11:47 AM, Jonathan M Davis wrote:
> >>> ...You'd have to duplicate functions all over the place... - Jonathan
> >>> M Davis
> >> 
> >> Can't you avoid that trivially with templates?
> > 
> > Then all of your functions have to be templated.
> 
> Right... but we're already doing that with Phobos anyway...
> 
> > Templated functions can't be
> > virtual. And even if they _could_ be, that still requires that
> > programmers template or duplicate functions all over the place. It not
> > only increases the amount of bloat in the binary, but it creates more
> > work for the programmer.
> 
> ... didn't I mention the same thing before, regarding too many templates
> in Phobos? I thought I always said functions shouldn't be templated, but
> everyone said it's OK because (1) that's how it is in C++, (2) it's
> faster, (3) etc., etc... I'm really confused by your [seemingly]
> contradictory reasoning. :\

There's plenty of stuff that's templated, but forcing you to template on const 
vs immutable would make the situation worse, and having const be intransitive 
while immutable is transitive could make creating such emplated functions 
problematic in some cases, because the semantics would be different.

The really big problem though is that templated functions cannot be templated, 
so every class member function that needed to be overridable would have to be 
duplicated to handle both const and immutable.

It's just _way_ simpler to make it so that both T and immutable(T) are 
implicitly convertible to const(T), and for that to work, const must be 
transitive. If we made it so that immutable(T) wasn't implicitly convertible 
to const(T), we'd be flooded with complaints about it - likely far more than 
we're seeing about const now.

> > Honestly, I don't understand what the problem with transitive const is.
> > What are you trying to do that the transitiveness of const causes issues?
> > I just don't see the problem.
> > 
> > - Jonathan M Davis
> 
> Wait, what?
> Haven't we already had enough complaints on the newsgroup about the
> problems with const? Are they all bogus or something?

The complaints have generally been about the lack of logical const and the 
inability to cast away const and modify variables _not_ about transitiveness. 
I'm not say that there have been _no_ complaints about the transitiveness 
about const, but I can't recall even _one_ issue with it at the moment. It's 
the lack of logical const that's generally complained about and has shown 
itself to be a major blocker for const in some cases.

- Jonathan M Davis


More information about the Digitalmars-d mailing list