Copying and in-place methods [was: Why is array truth tied to .ptr?]
renoX
renosky at free.fr
Tue Dec 11 12:55:25 PST 2007
Leandro Lucarella a écrit :
> 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 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.
>
> In D it could be something like:
>
> class T
> {
> void sort()
> {
> // in-place sort implementation
> }
>
> T dupsort() // default compiler implementation
> {
> auto t = new T(this);
> t.sort();
> return t;
> }
> }
>
> I think the regular name in D should be used for the in-place version, and
> dup<name> (or d<name> for short) could be used for the copying version,
> because there is also a well known .dup property (and I think it fits
> better in D the in-place because of it's high performance orientation).
>
> Maybe some syntactic sugar can be used too when using the dup version,
> something like:
>
> auto t = new T();
> auto w = t.sort.dup(); // calls dupsort()
>
> You can see it as function were objects (like in Python, or Ruby AFAIK)
> which has a property "dup" =)
>
> Which makes me think that it could be defined as:
>
> class T
> {
> void sort()
> {
> // in-place sort implementation
>
> T dup() // default compiler implementation
> {
> auto t = new T(this);
> t.sort();
> return t;
> }
> }
>
> }
>
> The dup() nested function could be not present (if the default
> implementation is enought(). I know we have a problem to access to dup()
> nested function because it needs a stack frame, but maybe the compiler can
> be aware of that and use it as a special case. Or maybe we can use the
> syntax for pre/post conditions:
>
> class T
> {
> void sort()
> body
> {
> // in-place sort implementation
> }
> dup // default compiler implementation
> {
> auto t = new T(this);
> t.sort();
> return t;
> }
>
> }
>
> Again, you should only have to provide a dup function if the compilers
> default is not enought.
>
> Too much? =)
I like it but I think that the calls are a bit confusing:
- t.dup.sort(): duplicate t then sort in-place the duplicate.
- t.sort().dup: sort in-place t then duplicate it.
- t.sort.dup(): by default duplicate t then sort in-place the duplicate,
so it's the equivalent of t.dup.sort() except that it can be overloaded
in the sort function to provide a faster implementation.
OK, it all make sense when you look closely and it's D-ish but it's also
a pitfall: "t.sort().dup" and "t.sort.dup()" do very different things
yet look very very similar..
The ruby way is more concise and less risky IMHO (though a bit obscure
for beginners/non-rubyist).
I wish that D have copied more things from Ruby's syntax which I like
very much..
renoX
More information about the Digitalmars-d
mailing list