Top 5

Steven Schveighoffer schveiguy at yahoo.com
Fri Oct 10 08:28:48 PDT 2008


"Andrei Alexandrescu" wrote
> Sergey Gromov wrote:
>> Fri, 10 Oct 2008 08:22:10 -0500,
>> Andrei Alexandrescu wrote:
>>> Sergey Gromov wrote:
>>>> My T[] is useful when you want to recursively split a megabyte file 
>>>> into a couple thousands of tokens, and then modify some of those 
>>>> tokens.  For that, your T[] must be lightweight, it must reference a 
>>>> bigger piece of data, and it must guarantee not to write anything into 
>>>> memory outside its boundaries.
>>>>
>>>> The Array is for appending.  It must always own its memory.  Therefore 
>>>> you should be able to pass it around by reference, so Array is a 
>>>> *class* and cannot be nearly as lightweight as T[].
>>>>
>>>> You see, many of their properties are orthogonal.  If you drop one, you 
>>>> lose flexibility.
>>>>
>>>>> Besides, Array!(T) is not a good name for build-in type.
>>>> Names are placeholders here, not an actual proposal.
>>> What's wrong with making Array a library type?
>>
>> Well, I'd like
>>   new Object[15];
>> to be immediately appendable and therefore a syntactic sugar for new 
>> Array!(Object)(15);
>
> I have a nagging impression the syntax Array!(Object) strikes you as hard 
> on the hand and the eyes...
>
> Anyhow the syntax new Object[15] is idiotic because Object[15] is a type 
> in itself. The syntax makes it next to impossible to actually generate a 
> fixed-sized array dynamically.
>
> In fact here's a challenge for you. Please generate a pointer to an 
> Object[15] using new.

struct StaticArray(T, int x)
{
   T[x] arr;
}

Object[15] * x = &((new StaticArray!(Object, 15)).arr);

lovely, no ;)  I'd probably make a static function in the template to do it, 
if it was really important to do that...

There may be a way to do it nicer with a template alias, but I'm not sure it 
will work.

-Steve 





More information about the Digitalmars-d mailing list