const(Object)ref is here!

Steven Schveighoffer schveiguy at yahoo.com
Mon Dec 6 10:57:47 PST 2010


On Mon, 06 Dec 2010 13:44:41 -0500, Michel Fortin  
<michel.fortin at michelf.com> wrote:

> On 2010-12-06 13:35:27 -0500, Andrej Mitrovic  
> <andrej.mitrovich at gmail.com> said:
>
>> Actually I really like the optional usage of ref here because it might
>> help to disambiguate between a class and a struct object:
>>  E.g.:
>>  class Foo
>> {
>> }
>>  struct Bar
>> {
>> }
>>  void main()
>> {
>>     Foo ref foo;  // I can be sure Foo is a class
>>     Bar bar;
>> }
>>  Sometimes it happens that I forget to 'new' a class object, and to the
>> naked eye the code doesn't appear wrong, but using the optional ref
>> keyword helps in tracking this kind of bug down. I guess the compiler
>> should throw an error in cases where Foo is a struct.
>
> It does (throw an error in cases where Foo is a struct). I never though  
> of this usage. :-)
>
> 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?

alias Object ref B should be the same as alias Object B, since Object ref  
is the same type as Object.

So there should be no error.

Now, what happens for this:

alias const(Object) ref A;

A ref a;

My gut says this should be the same error as trying to ref a struct.  But  
I can see it also being allowed.

-Steve


More information about the Digitalmars-d mailing list