Re: Revisit synonyms and references / typing

edA-qa wrote:
> 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. 

For clarification, an instance is characterized by a duet of type and 
locality. Locality in turn determines types of references that are 
allowed to refer to that instance. The relationship between a reference 
type and locality of an instance is at the center of the C2 referencing 
model.

An object shall only be accessed via a reference (or less safely via a 
pointer). This is also true for value-types. Consider this example:

T localT;

Here, `localT' is a const reference to a stack object T (`localT' cannot 
be rebound). Object T will be destroyed deterministically, that is, when 
the `localT' reference goes out of scope.

T& st = localT;  // error, there cannot be a synonym to a (value) type,
                  //   only to a reference.

T local^& st = localT; // st: ok, synonym to reference to local T

One may think that the indexing operator brakes the "no synonym to 
value-type" rule:

[]<T> ar[5](); // array of 5 Ts initialized with default constructor

?? = ar[0]; // what does ar[0] return?

Here `ar' is a const reference to a local array of 5 (contiguous in 
memory) initialized elements of type T. operator[] returns a synonym to 
a const reference to local T. Elements of the array can be assigned to 
references to local T or to any T. Array will be destroyed when the `ar' 
reference goes out of scope.

> T local;
> T^ sharedRef = new T();
> T local^ localRef = @local;

T local^ localRef = local; // initialization of a reference
                            //   is binding. No `@'.

> 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();
> 

Pointers do not act like references. Consider,

T* p1;        // pointer to T

p1 = @sharedRef;

p1 -> func(); // invokes func on object pointed
               //   by p1.

++p1;         // increments pointer, returns pointer
++(*p1)       // calls T.operator++(), returns reference
               //   to any T

Iterators will overload operators (->, *T, ++) to imitate pointers
[ptr->func(), *ptr, ++ptr]. Operator dot `.' cannot be overloaded.

Pointer to void will be used for compatibility with C and in the
implementation of an Any type --safe, generic container for single
values of different value types, provided by C2 std. library [Note:
references to void are not allowed].

> This is implying that the standard operators, and function calls, are 
> working not on the reference, but on the instance.  

Except for pointers.

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

I agree.

--

-- 
Slawomir Lisznianski
C2 Language Group Principal (http://c2-lang.org)

-------------------------------------------------------
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