More D newb questions.

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue May 6 05:55:47 PDT 2008


Me Here wrote:
> Walter Bright wrote:
> 
>>> (*BTW. Why does char[] replaceSlice(char[] string, char[] slice, char[]
>>> replacement) require both the target string and target slice of that
>>> string?)
>> Because all three parameters are needed to give sufficient information.
> 
> I kinda guessed it was done for a reason, but was looking for a clue as to
> what that reason was. I (probably wrongly, given how bad my assumptions
> about what D is doing under thc covers so far have panned out) that a 
> slice carried enough information to identify the array it is a slice of.
> 
> Eg. (something like) struct slice { T[]* parent; type_t pos; type_t length }

A slice is stored in the exact same way as a regular dynamic array: a 
length and a pointer.
The GC could figure out what block of memory the pointer points into, 
but that's probably rather inefficient and doesn't work for string 
literals (since they aren't heap-allocated so the GC doesn't know about 
them). And even ignoring all that: the 'string' argument to replaceSlice 
may itself be a slice of a larger string, in which case there's no way 
to determine what it was given only the 'slice' argument even if it 
*was* heap-allocated.



More information about the Digitalmars-d mailing list