edA-qa | 3 Oct 10:35

Revisit synonyms and references / typing

I would like to revist Slawek's example from before of the invoker series:

template<class T> void InvokerA( T^ fun )
{ fun.doSomething(); }

template<class T> void InvokerB( T^ fun )
{ InvokerA( fun ); }

...
T local;
T^ shared = new T();
InvokerB( local );
InvokerB( shared );

I believe this meets the requirements of no copying of the parameters 
without needing a reference.  Here's why, and it has to do with typing, 
an issue that has been somewhat ignored.

In my previous statemtn about references and instances, I am of the 
opinion, that in the way C2 works, the reference type has nothing to do 
with the actual instance type.  Therefore:

T local;
T^ sharedRef = new T();
T local^ localRef = @local;
T* ptr = /*something*/;

Unlike C/C++, in C2 all of the above references are used in the same 
fashion:
	local.func();
	sharedRef.func();
	localRef.func();
	ptr.func();

This is implying that the standard operators, and function calls, are 
working not on the reference, but on the instance.  In which case, all 
of the above have these types:
	local;	//object type T, locality: stack, ref type: value
	sharedRef;	//object type T, locality: heap, ref type: shared
	localRef;	//object type: T, locality: stack
	ptr;	//object type: T, locality: unknown, ref type: pointer

Now, given a function signature:
	template<typename Q> void func( Q any^ ref )

How does type resolution work?  In the above case it is requesting any 
reference to Q, thus the object type is of most importance, so it works 
with the object type, not reference type, thus:

func( local ):
	matches object type, to which a reference can be made:	
		T local^ tempRef @= local;
		ref @= tempRef;

func( sharedRef ):
	matches object type, to which a reference can be made:
		T shared^ tempRef @= sharedRef;
		ref @= tempRef;

This is based on an observation of the following.

Given a variable:
	T any^ ref;
The following statements are possible, and yield a reference to the object:

	ref @= local;
	ref @= sharedRef;
	ref @= localRef;
	ref @= ptr;

Thus, there is no reason that our invoker would yield a T^^, as the 
first conversion checked above, works fine.

This comes around to the synonym notation (&), which may be needed 
elsewhere, but to meet Slawek's requirements, is not needed.

(Note: This is difficult to explain the above, so I may have to redo it. 
In any case it leaves a lot of open questions about automatic conversions.)

--

-- 
edA-qa mort-ora-y
Idea Architect
http://disemia.com/

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

Gmane