More D newb questions.

Steven Schveighoffer schveiguy at yahoo.com
Tue May 6 13:42:52 PDT 2008


"Me Here" wrote
> Janice Caron wrote:
>
>> But.... Suppose I replace the built in array type with my own custom
>> container, Array. Then I can override opCat and opCat_r to give me
>>
>>     MyArray ~ MyArray = MyArray
>>     MyArray ~ char = MyArray
>>     char ~ array = MyArray
>>
>> However, there is no way I can persuade my custom container to give me
>> the effect:
>>
>>     char ~ char = MyArray
>>
>> because the bottom line is, a container needs to know about its
>> elements, but elements do not need to know about their container.
>
> It doesn't need to. Think about how this would come about:
>
> char a = 'a';
> char b = 'b';
> MyArray ma = a ~ b;
>
> The concatenation happens on the right hand side and forms an intermediate
> result. If the built-in base types knew how to do that, then so long as 
> you've
> implemented opAssign taking a char[], then it doesn't need to /do/ 
> anything. It
> would just work.

What about calling a function with the given concatenation?

f(MyArray ma);

f(a ~ b); // would this work?

You would probably get there with opImplicitCast, but it seems rather 
inefficient to build a temporary array just to build a MyArray structure.

> [ 'a', 'b', 'c', 'd' ] is neater and more elegant than 'a' ~ 'b' ~ 'c' ~ 
> 'd';
> so I'm happy with that.
>
> The clumsyness and cost of having to do:
>
>        s[ i .. i+10 ] = "" ~ toupper( s[ i .. i+10 ] );
>
> is more of a problem.

First, as has been pointed out in other threads, you can't toupper a char[] 
array in place because some unicode uppercase characters take up more space 
than their lowercase versions.  But even if that was not true, this is not a 
deficiency in the language, more a deficiency in the library.  Assuming 
ASCII only characters, a function like:

toupperInPlace(s[i..i+10]);

Should do exactly what you want.  If it existed :)

-Steve 





More information about the Digitalmars-d mailing list