const(Object)ref is here!

Michel Fortin michel.fortin at michelf.com
Mon Dec 6 12:49:14 PST 2010


On 2010-12-06 15:14:28 -0500, "Steven Schveighoffer" 
<schveiguy at yahoo.com> said:

> On Mon, 06 Dec 2010 15:01:25 -0500, Michel Fortin  
> <michel.fortin at michelf.com> wrote:
> 
>> On 2010-12-06 14:46:51 -0500, spir <denis.spir at gmail.com> said:
>> 
>>> On Mon, 6 Dec 2010 13:44:41 -0500
>>> Michel Fortin <michel.fortin at michelf.com> wrote:
>>> 
>>>> Since we're speaking of the optional use of 'ref', here's a little  quiz:
>>>>  	alias Object A;
>>>> 	alias Object ref B;
>>>>  	A ref a;
>>>> 	B ref b;
>>>>  What happens here? Should there be an error somewhere? Where? Also,
>>>> what happens if we apply different modifiers at different places?
>>>  As I understand the (non-)semantics of 'ref', there should be no  error. Thi
>>> s code should considered by the compiler as equivalent to:
>>> 	Object a;
>>> 	Object b;
>> 
>> And that's what my patch does. It only gets confusing when you add  modifiers:
>> 
>> 	alias const(Object)ref A;
>> 	alias const(Object ref) B;
>> 	alias const(Object) C;
>> 
>> 	A ref a;
>> 	B ref b;
>> 	C ref c;
>> 
>> With my patch, variables 'a', 'b', and 'c' are all of the same type:  
>> "const(Object)ref", the later 'ref' changing the constness of the  
>> reference specified in the 'B' alias.
>> 
>> Note that while this behaviour is allowed with an alias, it is *not*  
>> allowed directly, so you can't write this for instance:
>> 
>> 	const(Object ref) ref d;
>> 
>> That's an error.
> 
> I don't know if that's good.  Redundancy is OK as long as it is  
> consistent.  If Object ref is treated as Object everywhere it is used,  
> that helps to be able to explain how it works.
> 
> Similarly Object ref ref should probably reduce to Object.
> 
> We really need to ensure that all types using this notation are reduced 
> to  their canonical types internally.  Otherwise, strange results would 
> occur.

Don't worry. Internally, my patch makes sure types "Object ref" and 
"Object" have the exact same internal representation. The double-ref 
check is a check against the syntax tree, not against the actual type. 
That's why it's still possible to have two 'ref' if one is in the alias 
definition and the other is part of the variable declaration.


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



More information about the Digitalmars-d mailing list