-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implements Via not allowing member override #1840
Comments
I think you are misunderstanding how Implements via works. The way in which you define your method for Add is also incorrect as they Key, Before and After parameters should be Optional. I've updated your code below and it works fine. I leave it to you to define how to handle the optional parameters. Class MyCollection
Implements Collection Via Base = New Collection
Public Sub Add(Item As Variant, Optional Key As Variant, Optional Before As Variant, Optional After As Variant)
Debug.Print "MyCollection.Add"
Base.Add Item 'line corrected ater first post
End Sub
End Class
Module MyApp
Sub Main()
Dim MyCol As MyCollection = New MyCollection
Debug.Cls
Debug.Print "========================================================"
Debug.Print "Test MyCollection.Add"
Debug.Print "--------------------------------------------------------"
Debug.Print "(Should print 'MyCollection.Add' for each function call)"
Debug.Print "--------------------------------------------------------"
MyCol.Add "Element 1"
MyCol.Add "Element 2"
Debug.Print "========================================================"
End Sub
End Module |
You are absolutely correct about the Add declaration and the optional parameters. I have already edited the post and corrected it. By the way, the declaration was automatically generated by the IDE (Implements without the Via ...), so this is probably an issue to report. As far as the the code correction you suggested, with all my respect, I do not agree that this is the correct way to go, nor does it follow the original specification that was given from @WaynePhillipsEA during the initial discussion about inheritance twinbasic/lang-design#10 (comment). The example code in the above link has the DoSomething sub declared as private just to replace the original implementation and not be exposed, according to the code comments, as well. I can also recall that, during the early stages of Implements Via, member Overrides were allowed the way I suggested. Maybe @WaynePhillipsEA could shed some light on it. |
Its probably not best to pick your definition from the middle of a discussion but to look at what was finaly implemented. Fafalone does a great job in documenting the new features of twinBasic. https://github.com/twinbasic/documentation/wiki/twinBASIC-Features Although I'd have to admit that the example on 'implements via' likely needs updating to differentiate overriding methods in an interface vs overriding methods of a class. But, as ever, Wayne is the only person who can provide a definitive answer. |
In your updated question this line is still problematical as myCol refers to the native tb collection and not your myCollection class.
In my response you can see I updated it to
|
IMO I tried to hack it declaring both types of
... and it compiles and intercepts Compiler error reporting is currently very lax on |
This is exactly what I want to achieve. A custom collection like MyCollection is still a Collection. It exposes all the methods of the native VB Collection, plus any other additional methods. The Add method in question (as well as any other method of the native collection) should allow custom implementation (override). To make may point clear, consider the following 2 examples, the first with Implements, and the second with Implements Via
The above examples show the following: If we use the Implements without the Via syntax
If we use the Implements Via syntax
My conclusion is that Implements Via provides a really smart and useful solution for simple inheritance. If the direct override of any member is achieved, it would make it even more powerfull. |
I could not have agreed more... This is exactly my point.
I have not checked the VTable yet, but what I guess is done, in the case of Implements Via at the moment, is that the function pointers of the Base Class members are put to both the primary interface and the implemented interface positions, to achieve both interface implementation and inheritance. It would be absolutely possible to instruct the compiler to recognize the Implements Interface.Method syntax for selective overrides and replace the specific original pointers. Of cource, I understand that this is not a top priority at the moment, since the primary goal is to achieve 100% VBA/VBx compatibility. |
I must admit that I'm struggling to follow your arguments MyCollection using 'Implements via' with no additional methods is essentially aliasing a type, which can be useful. All methods of collection appear as methods of myCollection instances and all usage is referred back to the Base collection instance. If I want to override the add method from collection in the mycollection class then my understanding is that I need to add a public add method to MyCollection. Then any instances of mycollection will use the new add method and if required I simply use Base.add to access the add method of the Base instance. e.g. me.add vs base.add. I've not encountered any significant issues with using the inheritance model provided by implements via. I've not yet come across an instance where I need to define a private class as this, I think, is just a VBx workaround for hiding Interface methods, hence the need for _ in Interface method names. Given that the discussion is moving beyond my limited understanding, this is the last I'll say on this subject. |
Unfortunately, You'll need to wait for proper inheritance support for this feature. |
Nobody said that it's not useful. It has many uses and, I think, we are all more than grateful to have this feature in tb.
This works, under the condition that the declaration is The following example, will show a case where the Public Sub ProcessCollection(Col As Collection) Because the method signature explicitly declares the
As already said,
As Wayne has already stated, member overrides are not easy and will not be accomodated using the |
Btw, this seems like a pretty opportunistic implementation without proper aggregation :-)) For instance this is failing a basic COM invariant:
... because "inside" ICollection::QueryInterface doesn't know how to cast to "outside" MyCollection interface. Probably known limitation but still should I file this as a separate issue? On a second note, do TB produced coclasses support aggregation per se? This might help |
Yes, a known limitation. No, aggregation is not yet supported. |
Describe the bug
When Implements Via was first introduced, it was possible to override any member of the base class directly (https://github.com/twinbasic/lang-design/issues/10#issuecomment-1194965243)., which was really useful, and made it very easy to mimic inheritance, without having to re-implement every single member and forward it to the base class manually. The possibility was removed after some beta update (I cannot recall when, but it should be more than one year ago), and has never came back.
To Reproduce
Use the following code to reproduce the behavior:
Expected behavior
Implements Via should allow the override of any member, either directly, or by introducing an Overrides keyword. Direct overrides were enough, for most cases and should be re-enabled, except if the removal was a necessary part of of the plan to support full inheritance in the future.
Screenshots
Desktop (please complete the following information):
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: