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

Robert Atkinson Robert.Atkinson at NO.gmail.com.SPAM
Tue Sep 5 09:30:30 PDT 2006


I'm a fan of the local keyword, or perhaps combining with the existing 
scope keywords as follows:

local:

local File file = new File(); // local RAII
local auto file = new File(); // local RAII + auto typing

or

scope(local):

scope(local) File file = new File();
scope(local) auto file = new File();

scope(local) is a little long for a single line, but it begins to look 
better if you have a group of RAII variables:

scope(local)
{
	File file;
	RecordSet rs;
	// etc
}


I believe that if we keep the auto/auto syntax, it will be a feature 
that everyone will love at first, but years from now it'll be the 
feature that we'll be cursing when we all have to maintain code. 
Scanning code quickly its hard to determine with the current auto/auto 
syntax if its RAII or auto typing.

Rob


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. 
> 
> 




More information about the Digitalmars-d mailing list