const(Object)ref is here!

spir denis.spir at gmail.com
Mon Dec 6 11:46:51 PST 2010


On Mon, 6 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?

As I understand the (non-)semantics of 'ref', there should be no error. This code should considered by the compiler as equivalent to:
	Object a;
	Object b;

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d mailing list