Does anyone use 'with' statement?

Jarrett Billingsley kb3ctd2 at yahoo.com
Sat Dec 8 17:30:35 PST 2007


"serg kovrov" <sergk at mailinator.com> wrote in message 
news:fjfchm$u14$1 at digitalmars.com...
> Hi,
>
> Recently I have studied a (proprietary language) code. The language 
> features 'with' statement in the same sense that D does. To understand 
> what this code sample really do I had to remove all 'with's and 
> fix/compile code many times before it start working as original.
>
> I don't know why never tried to use it in my own code before, but now I 
> definitely will NOT use it ever. Even if it could save me some typing. I 
> found 'with' far more evil than even 'goto' (which I don't use as well).
>
> -- serg.

It could probably be overused, but it certainly has its uses.  I don't use 
it often, but when I do it usually makes for more elegant and less 
crufty-looking code.  Especially with UI stuff:

with(myButton = new Button())
{
    text = "OK";
    position = Point(150, 300);
    size = Size(75, 40);
    onClick ~= &okHandler;
}

Much nicer-looking than the alternatives:

myButton = new Button();
myButton.text = "OK";
myButton.position = Point(150, 300);
myButton.size = Size(75, 40);
myButton.onClick ~= &okHandler;

or:

myButton = new Button();
myButton.text("OK")
    .position(Point(150, 300))
    .size(Size(75, 40))
    .onClick ~= &okHandler;

I'd say using more than one level of a 'with' at a time is probably bad 
form. 





More information about the Digitalmars-d mailing list