More D newb questions.
BCS
BCS at pathlink.com
Mon May 5 13:58:16 PDT 2008
Me Here wrote:
> BCS wrote:
>
>
>
>>for one thing "abcd[b1]" is not a slice, if you want a slice use
>>"abcd[b1..b1+1]"
>
>
> Yeah. Its just another tacky inconsistancy.
>
it's not really inconsistent (see below)
>
> int main( char[][] args ) {
> char[] a = "x";
> char b = 'x';
> writefln( a ~ b );
> writefln( a );
> // writefln( b ~ b );
> return 1;
> }
> ouputs:
>
> xx
> x
>
> So, D knows how to concatenate a single character char[] with a char
> and produce an intermediate result. Ie. The char[] isn't mutated.
>
> But uncomment the line that attempts to concatenate char to char, and you get:
>
> junk.d(8): Error: Can only concatenate arrays, not (int ~ int)
>
> which is silly. There's no technical reason for not allowing this.
> And there's no semantic reason either.
if Type ~ Type -> Type[] then why would Type[] ~ Type[] -> Type[]
based on what you ask for this would be valid
char[] a, b;
char[][] c = a ~ b; // ok
char[] d = a ~ b; // same expression, different type?
auto e = a ~ b; // what's the type of this then?
but for that to work you would need to look up and down the expression
tree just to figure out what something is. That gets just plain nasty.
> What elese could 'a' ~ 'b' mean other than char[] tmp = "ab"?
>
> Similarly, there's no technical reason for not allowing
> lvalue slice assignments to grow or shrink the target,
> or do overlapping copies.
I think there is a reason for it. D is a low level language, the
semantic you are asking for can be done in a function and, if included
in a language, would hide some types of errors. IMHO D should not have
any semantics that are not easy for the program to understand how they
are working.
for example, If I understand you correctly you would like this to work:
char[] t = "hello world";
auto u = t;
auto v = u[8];
t[5..5] = ",";
assert(t == "hello, world");
but this results in all sorts of fun stuff like "non-accessed" data
changing:
assert(v == u[8]); // it changes but I didn't change it.
>
> Such things are relatively trivial to write and are a part of library.
> But every programmer having to write their own code to detect
> whether the source and target of slice assignments are different
> sizes, or overlap, at each site and decide whether they can use a
> slice assignment or must revert to calling a library function
> is a nonsense.
IIRC the reason that overlapping copies are not allow is that the
overhead of checking them is non trivial and the code to do the copy is
to. D is taking the approach of "most copies are never overlapping and
if you need might need it say so" In a higher level language this would
be a problem, but for what D is intended for, this isn't a problem.
More information about the Digitalmars-d
mailing list