auto ref

Michel Fortin michel.fortin at michelf.com
Thu Dec 17 12:17:22 PST 2009


On 2009-12-17 14:52:40 -0500, "Steven Schveighoffer" 
<schveiguy at yahoo.com> said:

>> The interesting thing about it, beside not taking a keyword, is that it 
>>  can scale in the future if we need to add many distinct constness to 
>> the  same function signature:
>> 
>> 	const?(Object) func(const?(Object) o, const?2(Object) o2, out  
>> const?2(Object) o3) {
>> 		o3 = o2;
>> 		return o;
>> 	}
> 
> This can never work.  a const?(Object) is const during the function  
> execution, and cannot be assigned to.

I'm not sure why, but I always forget that const(Object) is not 
rebindable. My mistake, here is the corrected example:

	const?(MyStruct)* func(const?(MyStruct)* s, const?2(MyStruct)* s2, 
const?2(MyStruct)* s3) {
		o2 = o3;
		return s;
	}


>> Furthermore, the concept could be extended to any type. This could be  
>> useful with class hierarchies:
>> 
>> 	Object? func(Object? o) {
>> 		writeln(o.toString());
>> 		return o;
>> 	}
>> 
>> 	MyObject o = func(new MyObject);
>> 
>> Here, "Object?" means Object or a derived type.
> 
> This doesn't have the same utility as vconst, since you can't apply 
> Object  to other types like you can constancy.
> 
> Plus you can already do this with a template and have a virtual version 
> of  the func:
> 
> T func(T)(T o) if(T : Object) {func_virt(o); return o; }
> protected void func_virt(Object o) {writeln(o.toString());}

Indeed. I was mostly trying to show that the "const?" notation can 
easily be extended to all sort of things, which makes it a better 
choice than other notations.

We could do the same with an old idea that didn't get in the language: 
scope arguments.

	// *a and *b are in the same scope, so you can swap a and b
	void swap(scope?(int)* a, scope?(int)* b) {
		int tmp = a;
		a = b;
		b = tmp;
	}

But the scope problem would require more thought.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list