Walter: Before you go and implement the new RAII syntax..

Chris Nicholson-Sauls ibisbasenji at gmail.com
Mon Sep 4 23:46:05 PDT 2006


Jarrett Billingsley wrote:
> This is what I'm talking about:
> 
> Foo f = Foo(); // RAII
> Foo g = new Foo(); // Normal
> 
> Can we all discuss this?  I'm sorry, but that syntax is just too easy to 
> mess up / miss.  I'm surprised that you, someone who is very much against 
> "secret things happening" would be a proponent of this syntax.  It's just 
> too nonobvious.  (And as a minor point, this also makes it impossible to 
> make a static opCall with a class.)
> 
> How about instead, we keep auto to mean a RAII reference, and we take the C# 
> route for type inference and come up with a "var" keyword which would be a 
> "type placeholder".  That is:
> 
> var f = new Foo(); // Normal
> auto var g = new Foo(); // RAII
> 
> And as you can see, this gets rid of thee "auto auto" problem.  Not to 
> mention it gets rid of the very common misunderstanding that currently, 
> "auto" is somehow used for type inference, when in fact it's simply the use 
> of a storage class without a type that invokes type inference.
> 
> This, or maybe we could use a "stack" or "local" or "raii" keyword in place 
> of the "new":
> 
> Foo f = stack Foo();
> Foo g = local Foo();
> Foo h = raii Foo();
> 
> Something.  Anything would be better than the "new or nothing" syntax. 
> 
> 

I despise writing "me too" posts, but in this case I just cannot avoid it.  So... me too!

I also find it intriguing that Phobos' RegExp module includes the following:
#    public static RegExp opCall(rchar[] pattern, rchar[] attributes = null)
#    {
#        return new RegExp(pattern, attributes);
#    }

Yes in this case it is nothing but a wrapper around the constructor, and essentially 
useless so far as I can tell.  However, the fact that Walter felt compelled to provide 
such a creature is telling.

Personally I favor either 'local' or 'scope'/'scoped' as a new keyword for this purpose, 
and do believe as others that it belongs in 'new's current position.  The option of 
'scope'/'scoped' has the added benefit of consistancy with current keywords and usage.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list