Performance improvements for D / DMD compiler.
Bill Baxter
dnewsgroup at billbaxter.com
Sun Jan 21 17:44:04 PST 2007
James Dennett wrote:
> Walter Bright wrote:
>> Bill Baxter wrote:
>>> The question is how can we write functions in D that take big structs
>>> as parameters in such a way that it is:
>>> A) efficient and
>>> B) guaranteed not to change the caller's value
>>>
>>> In C++ the answer is const LargeStruct&.
>> C++ doesn't *guarantee* it won't change. There are at least two legal
>> ways to do it.
>
> This is an oft-repeated point, but it also misses the users'
> perspective. It's important from an implementors perspective
> that this use of const as a type modifier doesn't provide the
> compiler with a guarantee of immutability. Given sensible
> users, that doesn't generally matter to programmers writing
> code. const documents an interface; it doesn't enforce it,
> though
Exactly. The main reason I want to be able to have something like a
const inout is because I want the compiler to let _me_ know when I'm
writing some function if I accidentally modify some value that I
promised in my interface that I wouldn't modify.
It doesn't bother me that the constness can be subverted by a cast. In
fact I'd *like* to be able to choose to modify some private bit of state
in an object if necessary (e.g. private mutable members used for caching
or somesuch). D is a down-to-the-metal systems programming language,
after all, not a nanny language. In fact, I really don't see much
practical use for a totally watertight, totally irrevocable const.
(Unless it maybe opens the door for some sweet optimizations. But I'd
argue that should be a separate thing from this const that's more of a
promise about the interface.)
That said, I'm also certainly not in love with how const works in C++.
At the end of the day, all I really want is an efficient way to pass big
structs without suggesting in the interface that they'll be modified.
But that leads naturally to const references, and then that basically
leads to const everywhere, just like C++, since you can't pass any const
parameter to another function that isn't also const. :-(
--bb
More information about the Digitalmars-d
mailing list