Copying and in-place methods [was: Why is array truth tied to .ptr?]

Oskar Linde oskar.lindeREM at OVEgmail.com
Thu Dec 13 06:56:23 PST 2007


Leandro Lucarella wrote:
> Daniel Keep, el 11 de diciembre a las 13:37 me escribiste:
>>
>> Robert Fraser wrote:
>>> Daniel Keep wrote:
>>>> [2] Sometimes, I really wish '?' and '!' were valid characters in
>>>> identifiers, like in Ruby.
>>> That gives me the answer to the Object.toString() vs. Object.toUtf8()
>>> problem posed before... just change it to Object.wtf?()
>> '?' is used for function that check a property of a type, '!' is used
>> for functions that perform in-place modification.
>>
>> if( "my string".empty? )
>>
>> [3,1,2].sort  --> int[]
>> [3,1,2].sort! --> void
>>
>> Without "?", you can get away by putting "is" in front of everything,
>> but "!" is a hard one, unless you come up with some sort of trailing
>> sigil using normal characters (like _ip).

One convention I have grown comfortable with is using the adjective form 
of the verb to define the non-in place versions:

sort - sorted
reverse - reversed
transpose - transposed
capitalize - capitalized
strip - stripped
rotate - rotated
normalize - normalized
project - projected

But doesn't work in 100 % of the cases, and I would prefer if the 
in-place version wasn't the shortest form and stood out a bit more.

> One thing that I think Ruby doesn't have is automatic not-in-place
> function generation. I mean, if I have implemented SomeClass.sort! but not
> SomeClass.sort, the later can be created by copying the object, sorting
> the copy in-place and returning that copy. You can see it as a copy
> constructor in C++, which is generated by default by the compiler, if you
> don't provide one.

One problem here is that the functional version is the generic one. Not 
all algorithms have in-place versions and some that do are much more 
complicated.

-- 
Oskar



More information about the Digitalmars-d mailing list