Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Forward referencing a class?
Mon, Mar 13 2017 10:39 AMPermanent Link

Matthew Jones

Is it possible to forward reference a class type?

I am stuck with an interesting logic twist.

I have a generic tree class TTreeItem. I have a specific tree class TFolderItem. This works fine, and I can call a virtual function in the tree and it goes to the TFolderItem (which can call the inherited code too).

Now, I want to be able to clone the tree. So I added a parameter to the Create which is the class of the tree, so I can specify the class at creation, and it can then use that class in the cloning. Which all nearly worked, except that the clone code has to be:

Result := TTreeItem(m_classChild).Create(m_classChild);

Basically, I have to pass in the class to the created item, which is fine, but the cast is causing the compiler to ignore the actual class itself in the creation, and casts it to TTreeItem which defeats the use of the class reference. So my tree is not TFolderItems, but TTreeItems. That then causes later code to fail.

Now, part of this is that the m_classChild variable is defined as:
TClassPersistent = class of TPersistent;
m_classChild : TClassPersistent;

But that doesn't allow me to use the Create with the class type. So I figured I would define the create as a TTreeItem class, but I cannot define
TClassTreeItem = class of TTreeItem;
until I have defined TTreeItem, and that needs TClassTreeItem to be defined. So I'm stuck. At least, I can't work out how to define a reference to a class before the type definition, and the class details later (which you can do with Delphi)

Any thoughts welcome as I will be working on this...

--

Matthew Jones
Mon, Mar 13 2017 10:45 AMPermanent Link

Matthew Jones

Matthew Jones wrote:

> Result := TTreeItem(m_classChild).Create(m_classChild);

I have replaced that line with these:

   Result := TTreeItem(m_classChild.Create);
   Result.m_classChild := m_classChild;

And it is now working. I'd tried it before, but until I wrote all that, I'd not looked hard. Without the cast in the first line, it was showing an error in the Create. Not because it was having a problem with it, but because it wasn't returning what it expected.

Might be useful to know how to forward reference a class still, as that would eliminate the cast.

--

Matthew Jones
Wed, Mar 15 2017 8:58 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< Is it possible to forward reference a class type? >>

No, not at this time.  I'm not entirely sure and would have to check, but I think that there's an issue with emitting when doing so, which is why it hasn't been done yet.  JS is a little particular about the ordering of such types, and EWB's compiler itself can also be sensitive to this, so I'll have to approach this carefully.

Tim Young
Elevate Software
www.elevatesoft.com
Thu, Mar 16 2017 5:27 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

>  there's an issue with emitting when doing so

I'd sort of assume that nothing would be emitted, just that the compiler would "know" that the class existed, and thus not complain when it saw the reference. A placeholder only.

Of course it is a long time since I've written compilers, and we didn't have objects in those days, so I expect it is not simple. 8-)

--

Matthew Jones
Thu, Mar 16 2017 8:15 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< I'd sort of assume that nothing would be emitted, just that the compiler would "know" that the class existed, and thus not complain when it saw the reference. A placeholder only. >>

For Object Pascal, sure.  But, the way that the Object Pascal classes are declared control how the JS prototypes are created, and in which order, and JS has some special rules regarding what order those prototypes have to be in when referencing each other.

Tim Young
Elevate Software
www.elevatesoft.com
Thu, Mar 16 2017 8:55 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> control how the JS prototypes are created

I guess I'd not thought that it would be needed to be reflected in the underlying output, but of course it must. Fair enough. You have better things to work on...

--

Matthew Jones
Image