[Gambas-user] Nasty bug in "With" instruction!

Benoît Minisini gambas at ...1...
Mon Apr 20 11:18:24 CEST 2009


> Some more testing...
> With this code:
>
> With tmp
>   .y = 0
>   tmp = funcx()
>     With tmp ' Extra with instruction.
>     Debug .y
>     Debug tmp.y
>     End With
> End With
>
> Or with this:
>
>  With tmp
>   .y = 0
>   funcx2(tmp) ' By reference (unfortunately this solution doesn't fit
> to my code).
>    Debug .y
>    Debug tmp.y
>  End With
>
>   Public Sub funcx2(test as Class1)
>   test.y = 1
>   End
>
> Result is what is expected!
>
> I tried to look at the gambas source code to figure out what is
> wrong... beats me up.
> But maybe the problem is in how archiver or interpreter handles "tmp =
> ...". Extra "with" instruction is workaround, but it's little troublesome
> to find where it is needed.
> And when "tmp = funcx()" is in "if then else" structure, it is very
> troublesome! Maybe I need temporarily to get rid of "with" instructions!
> But I hope it is easy to fix!
>
> Regards,
> Jussi
>
> On Thu, Apr 16, 2009 at 16:17, Jussi Lahtinen <jussi.lahtinen at ...626...> 
wrote:
> > Hi!
> > Confirmed on Gambas2 and Gambas3.
> >
> > Code:
> >
> > Dim tmp As New Class1
> >
> > With tmp
> >  .y = 0
> >  tmp = funcx()
> >  Debug .y
> >  Debug tmp.y
> > End With
> >
> >
> > Public Function funcx() As Class1
> > Dim test As New Class1
> >
> > test.y = 1
> >
> > Return test
> > End
> >
> >
> > Output of debug is;
> > 0
> > 1
> >
> > I think .y and tmp.y should be same!
> >
> > Regards,
> > Jussi
>

WITH is actually managed at runtime, not at compile time.

I mean:

WITH var
  .x = ...
  .y = ...
END WITH

is equivalent to:

tmp = var
tmp.x = ...
tmp.y = ...

and not to:

var.x = ...
var.y = ...

So, by changing the value of tmp inside the WITH instruction, you are not 
changing the object WITH works on!

Regards,


-- 
Benoît




More information about the User mailing list