Suggestion: Remove implicit conversion from T[n] and T[] to T*
Don Clugston
dac at nospam.com.au
Thu Nov 30 23:31:07 PST 2006
Oskar Linde wrote:
> Removing the implicit conversion from static arrays (T[n]) and dynamic
> arrays (T[]) to pointers T* may be quite controversial, but would help
> clear up some problems and feels like the right step to take.
> Motivation:
>
> - Implicit conversions are generally bad. There are cases where they
> make sense, but those need a very good reason.
> - Both conversions are available by the .ptr property already and by
> using that the code is clearer and more self documenting.
> - T[] contains both a length and a ptr. Both are needed to retain the
> information. Symmetrically requiring (a.ptr, a.length) makes sense.
> - Decoupling arrays from pointers somewhat lifts D to a slightly higher
> abstraction level without losing any power.
I fully support this.
My experience so far is that the #1 cause of bugs in my D code is the
implicit conversion from char [] to char *.
When passing a char [] to a C function, it's so easy to forget to add
the null terminator. I've found this hard to track down, especially
because of DMD's habit of adding a null to the end of char[] arrays; it
means your code seems to work fine, until you use an array slice instead
of an array...
More information about the Digitalmars-d
mailing list