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

Leandro Lucarella llucax at gmail.com
Tue Dec 11 07:19:47 PST 2007


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? =)

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Y tuve amores, que fue uno sólo
El que me dejó de a pie y me enseñó todo...



More information about the Digitalmars-d mailing list