Generic collection/element function signatures in D2 versus D1
Jacob Carlborg
doob at me.com
Sat Sep 4 03:11:52 PDT 2010
On 2010-09-04 10:02, Nick Sabalausky wrote:
> "Nick Sabalausky"<a at a.a> wrote in message
> news:i5su5e$23mk$1 at digitalmars.com...
>> In D1 I did this sort of thing a fair amount:
>>
>> void foo(T)(T[] collection, T elem)
>> {
>> // Blah, whatever
>> }
>>
>> Worked for any of the string types, worked for any array, or anything with
>> the appropriate opIndexes, and for all I know there may be some
>> improvement that could still be made. But of course, in D2 strings have
>> that extra immutable part that mucks up the above for strings (and then
>> there's ranges), so: Is there a typical generally-best way in D2 to
>> declare a function signature for operating on collections and elements? I
>> know it would involve using the standard range interfaces in the body and
>> choosing the most restrictive range type that gets the job done, and I'm
>> fine with all that, but is there a good example of a typical
>> "best-practice" generic-function signature in D2?
>>
>
> Oh, also, and perhaps more importantly (I forgot, this was my main original
> reason for even posting the question):
>
> What would be the *right* D2 version of the above code that *didn't* bother
> with ranges, and just stuck with arrays and strings? Sometimes I need to do
> something in CTFE and using ranges leads to using "std.algorithm", and CTFE
> still tends to choke on a lot of "std.algorithm".
If you're not going to modify the content of the array I think this will
work:
void foo (T) (const(T)[] collection, T elem) {}
This will allow both mutable, immutable and const arrays. But it will
not let you modify the array like this:
collection[3] = 'a';
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list