const(Object)ref is here!

Michel Fortin michel.fortin at michelf.com
Mon Dec 6 10:44:41 PST 2010


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?


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



More information about the Digitalmars-d mailing list