Cannot have properties on const references?

Jonathan M Davis jmdavisProg at gmx.com
Sun Sep 25 03:21:55 PDT 2011


On Sunday, September 25, 2011 12:10:42 Tobias Pankrath wrote:
> > Property functions are still functions. A member function must be const
> > for it to be callable on a const object.
> 
> Since we have transitive const, why can't the compiler deduce which
> methods can be called on const instances?

You mean, why can't it just figure out whether a particular function can be 
const or not? Well technically, it probably could, but that complicates the 
compiler, and there are cases where you want to enforce that a particular 
function be const. If it were always inferred, you couldn't do that.

D _does_ now have inferrence for pure, nothrow, and @safe for templated 
functions, because it has to have that in order to be able to reasonably use 
pure, nothrow, or @safe with templated functions (since whether a templated 
function can be pure, nothrow, or @safe often depends on the template's 
arguments). But even with that added to the language, the compiler doesn't 
infer any of those attributes for non-templated functions. The programmer 
should be able to figure that out and decide whether they want them to be const 
or pure or whatever in those cases. The inference is only done when it's done, 
because it's needed..

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list