Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Class instance variable access from class method not rejected
Wed, Nov 8 2017 7:25 AMPermanent Link

Michael Dreher

I have made a mistake and accidental referenced a class instance variable from a class method.
It would be fine if the compiler will report an error in this case. Example:

type
 TMyClass = class public
   i : integer;
   procedure foo;
   class procedure cFoo;
   property GetI : integer read i write i;
 end;

implementation
procedure TMyClass.foo;
begin
end;

class procedure TMyClass.cFoo;
begin
 // This all compiles but should be rejected
 i := 42;
 foo;
 GetI := i*2;
end;

Michael Dreher
Wed, Nov 8 2017 10:52 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

<< I have made a mistake and accidental referenced a class instance variable from a class method.
It would be fine if the compiler will report an error in this case. >>

I think I may have done this on purpose, but I'm not sure and will have to confirm it.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Dec 11 2017 2:28 AMPermanent Link

Michael Dreher

I just updated to 2.06B11 and noticed, access of class instance members from a class method is not possible anymore. Fine. However, this now is also true when accessing instance members of a concrete instance. Example:

type
 TMyClass = class public
   // ...
   class procedure cBar;
 end;

class procedure TMyClass.cBar;
var c : TMyClass;
   iValue : integer;
begin
 c := TMyClass.Create;
 c.Foo; // [Error] Cannot access the Foo() instance member from within a class method
 iValue := c.GetI; // [Error] Cannot access the GetI instance member from within a class method
 iValue := c.i; // [Error] Cannot access the i instance member from within a class method
end;

Is this intentionally or accidentally?

Michael Dreher
Mon, Dec 11 2017 3:41 AMPermanent Link

Matthew Jones

Michael Dreher wrote:

> Is this intentionally or accidentally?

Your example is incomplete, not having mention of those methods etc. At first look, I think it should be intentional, as you do not have a "self" to provide the reference. But in your example, the class method is creating an instance, so it should work for the references off that. I suspect that the compiler is being clever but not clever enough.

What happens if you move your code into an ordinary procedure and call that from your class procedure? That may be a viable work around to save the compiler getting too complex.

--

Matthew Jones
Mon, Dec 11 2017 4:44 AMPermanent Link

Michael Dreher

"Matthew Jones" wrote:

 // Your example is incomplete, not having mention of those methods...

Sorry, it's an extension to the class of my first post, the missing members are there.

  // What happens if you move your code into an ordinary procedure
  // and call that from your class procedure? That may be a viable work around....

This works and is a work around, thanks!

M. Dreher
Mon, Dec 11 2017 1:23 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

<< I just updated to 2.06B11 and noticed, access of class instance members from a class method is not possible anymore. Fine. However, this now is also true when accessing instance members of a concrete instance.  >>

Yeah, I think that the compiler checks are not being fine-grained enough and aren't looking at the qualifying references.  I'll have a fix for this in the next build.

Tim Young
Elevate Software
www.elevatesoft.com
Image