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

Robert DaSilva sp.unit.262+digitalmars at gmail.com
Tue Dec 11 19:26:46 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 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()

Don't you mean t.dup.sort(). t.sort.dup() would do a inplace sort and
then duplicate it. (t.dup().sort and t.dup.sort() mean the *exact* same
thing, the ending () are optional for *all* function that don't tack
arguments.)



More information about the Digitalmars-d mailing list