Poll: Would you like to try const-by-default or not?
Bill Baxter
dnewsgroup at billbaxter.com
Tue Jun 12 21:17:00 PDT 2007
Daniel Keep wrote:
> I want to see option #2, but I think more work needs to be done to
> ensure that all the problems related to metaprogramming get resolved.
> For instance, this has me worried:
>
> void foo(T)(T a)
> {
> T b;
> }
>
> foo(new Object);
>
> What's T? If T is "const Object", then you've broken the "const by
> default on function arguments only" stipulation. If it's "Object" then
> the inferred type is one thing, and *actual type* the compiler sees is
> different. It's not exactly very pretty.
>
> There's probably other nasties lurking around, which is why I originally
> proposed the addition of a const by default switch as a purely temporary
> measure to see what happens.
>
> That said, if we can't have the switch, I still think we should give
> const by default a shot if only to see what the full impact is. If it
> turns out to be too onerous, then we can roll back to non const by default.
>
> So yeah; +1 on option 2, provided we can sort out any issues that code up.
>
> -- Daniel
In the world of const though, really what you want is for that to become
void foo(const Object a)
{
Object b;
}
So either way, const-by-default or not, you've got issues.
In C++ I think that's usually spelled something like:
template<T> void foo(const T a)
{
T b;
}
and foo(new Object) results in T being Object* and the function becoming
void foo(const Object* a)
{
Object* b;
}
For D const-by-default I would expect foo(new Object) to get T set to
Object, resulting in
void foo(Object a) // param const-static-final as expected
{
Object b; // not const as expected.
}
Would anything else make sense?
Similar to the C++ case, if you WANT something other than the default,
then you'll need to say so in the argument:
void foo(T)(mutable T a)
{
T b;
}
Now both a and b are modifiable in foo(new Object).
Here's a real potential issue. How do you overload on const?
void foo(Obj a) {...} // const by default
void foo(mutable Obj a) {...} // modifiable
foo(new Obj);
... or no. Actually that's not an issue either. You call the mutable
one unless you can't.
I'm starting to be hopeful that all these potential issues will just
melt away under closer inspection (or at least turn out to be no more
thorny for const-by-default than for const of any kind).
--bb
More information about the Digitalmars-d
mailing list