Thread overview
The 'with' construct
Jan 27, 2003
Jeroen van Bemmel
Jan 27, 2003
Jeroen van Bemmel
Jan 27, 2003
Sean L. Palmer
January 27, 2003
Comes from pascal I believe

It does not seem like a useful feature to me, since it breaks object encapsulation. Any code written like this:

class X
{
    int a,b;
}

X x;
with (x)
{
    a = 1;
    b = 2;
}

should be rewritten to:

class X
{
    int a,b;

    void setAB( int a, int b ) {
        this.a = a;
        this.b = b;
    }
}

The compiler can always choose to inline such methods to get eventually the
same code as 'with'.
One question: why can't I use 'with' in combination with structs?


January 27, 2003
I have to come back on my previous post, in some cases 'with' might come in handy. Namely when you want to intermingle the code with statements that are not part of a would-be method of the class (when rewritten as described previously).

In my case I have:

struct S {
    ubyte flag1;
    ubyte flag2;
    ...
}

code:

void f()
{
    S s;
    with (s)
    {
        if (flag1 != 12) return;
        // can do many things here...
        if (flag2 != 34) return;
    }

without 'with' I'd have to write a 'bit checkFlags()' that returns false/true when the calling function should return (inlining would still result in the same code though). But intermixing of code that is not related to s' class could be an argument in favor of 'with'


January 27, 2003
Not only that, but consider this:

class Foo
{
public:
    void Method1();
    void Method2();
    void Method3();
}

void Test(Foo**** nastypointer)
{
    with (****nastypointer)
    {
        Method1();
        Method2();
        Method3();
    }
}

No violation of encapsulation there.  I'm just using nastypointer to demonstrate a point, which is that it could be incredibly inconvenient to get at the object you use "with" with.  It should save the optimizer some hassle lifting all the common pointer math out of the accesses.

One could argue, with classes, that you should just make a local reference to the object and manipulate it through that.  That's C++'s way.  It works too.

With is just syntax sugar;  it doesn't add anything new really, but it could occasionally be convenient.  It might cause issues with name lookup rules. That all said, I'm not particularly partial to it;  if it got yanked from the spec, I wouldn't lose sleep.  ;)

Sean

"Jeroen van Bemmel" <anonymous@somewhere.com> wrote in message news:b12neq$1m10$1@digitaldaemon.com...
> I have to come back on my previous post, in some cases 'with' might come
in
> handy. Namely when you want to intermingle the code with statements that
are
> not part of a would-be method of the class (when rewritten as described
> previously).
>
> In my case I have:
>
> struct S {
>     ubyte flag1;
>     ubyte flag2;
>     ...
> }
>
> code:
>
> void f()
> {
>     S s;
>     with (s)
>     {
>         if (flag1 != 12) return;
>         // can do many things here...
>         if (flag2 != 34) return;
>     }
>
> without 'with' I'd have to write a 'bit checkFlags()' that returns false/true when the calling function should return (inlining would still result in the same code though). But intermixing of code that is not
related
> to s' class could be an argument in favor of 'with'
>
>
>