"Class Thing[T] Extends T" won't compile

About Monkey 2 Forums Monkey 2 Programming Help "Class Thing[T] Extends T" won't compile

This topic contains 11 replies, has 3 voices, and was last updated by  arpie 2 years, 4 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #5492

    arpie
    Participant

    I can’t get pointy brackets to appear in the title.  It should be :

    Class Thing<T> Extends T

    When I try to build, I get the error :

    Error : Type ‘T?’ is not a valid super class type

    This worked in Monkey1.  Will it one day work in Monkey2?

    #5494

    nerobot
    Participant

    I dont know is it bug or not, but I am interested in example of such using.

    Can you post code from monkey1?

    For me it looks like incorrect – you cant overload methods and using other members of template T.

    Maybe you forget to add ‘Where T…’ keyword to refine T?

    #5509

    arpie
    Participant

    I’ll try to put together a trivial example.  While you’re waiting, could I ask you to explain what you mean in your last sentence? As far as I can see, ‘Where’ isn’t a keyword in Monkey1 or 2.  What do you mean by refine T?

    #5511

    arpie
    Participant

    Here is a Monkey1 implementation of the ‘composte‘ pattern (commonly used for GUI widgets, amongst other things).  The classes here have a bare minimum of functionality – the Container class needs Remove(), Sort(), etc… and the Widget() probably wants some coordinates, size, etc. but that’s not the point.  What I am trying to demonstrate is that the Container Class is basically a mixin and can turn any class into a container for itself (it turns a component into a composite).  I could have missed out the Interface but I think it adds to the usefulness of the concept by not forcing components to inherit from the Widget class.

    I haven’t yet used this in any real apps so I may have missed some fundamental issue that renders it useless… let me know if you spot something!

    #5512

    nerobot
    Participant

    Where is a keyword of monkey2 and it has highlighting in Ted2.

    Used like this:

    In your example code you don’t use T inside of Container, so why to use it at all?

    What do you mean by refine T?

    something like this:

    if you use where you prevent incorrect type of elements that can be added to container – now only elements with interface IWidget would work, for other you get compiler error. Profit! 🙂

    #5528

    arpie
    Participant

    Where is Where documented?

    I don’t use T directly inside Container, no, but used as I have, it allows me to turn any class into a Container without having to rewrite the Container methods.  It allows me to use Container as a mixin with any other class.  The key point is that in my example Frame extends both Container and Widget – it inherits all methods from both classes.  I guess you could call it multiple inheritance but that has a bad reputation so it’s a term I prefer to avoid.

    #5529

    nerobot
    Participant

    There is no docs for this yet (I never seen). I hope it will be added later.

    You can find a few topics on this forum by request ‘Where T’.

    “Where” in mx2 works like in c#, google “c# where” for more info.

    I don’t use T directly inside Container, no, but used as I have, it allows me to turn any class into a Container without having to rewrite the Container methods.  It allows me to use Container as a mixin with any other class.  The key point is that in my example Frame extends both Container and Widget – it inherits all methods from both classes.  I guess you could call it multiple inheritance but that has a bad reputation so it’s a term I prefer to avoid.

    It looks like that solution for you is using

    So you can add any widget into container (they all implement IWidget), and you also can add frames into container (it implements IWidget too).

    #5536

    arpie
    Participant

    It looks like that solution for you is using

    Class Container<I> Where I Implements IWidget

    So you can add any widget into container (they all implement IWidget), and you also can add frames into container (it implements IWidget too).

    That’s not really the same thing. In a way it’s the opposite of what I am trying to achieve. I want the Container class to be as generic and reusable as possible. What I am trying to avoid is having to re-implement the Container interface in the Frame class. I want to inherit the implementations (not just the interfaces) of Widget and Container into one Frame class. I don’t want a Container into which I can put Frames and Widgets. I want a Frame that IS a container (into which I can put Frames, Widgets, etc…)

    Of course, I could achieve what I want by avoiding the Container class completely – just implement the IContainer interface in the Frame class but…

    I also want to be able to reuse the Container class/mixin elsewhere, for example, for a Phrase composite that can contain Word components (or other Phrases). Or a Task class that can be extended into a Project container, allowing a heirarchy of Projects, Subprojects and Tasks….. all using the same Container Mixin to implement a generic heirarchy of objects.

    Your suggestions help to enforce the relationship between the interfaces but they don’t avoid the problem of having to rewrite the implementations of those interfaces in many places. I hate having to rewrite the same code twice!

    The ideal solution to all this would be for Mark to implement Mixins (or Traits, or whatever you want to call them).

    #5539

    nerobot
    Participant

    I have more attentively looked at your code.

    You want to extend Container with template type. So my previous answer is wrong.

    The ideal solution to all this would be for Mark to implement Mixins (or Traits, or whatever you want to call them).

    It looks like the only way.:)

    #5576

    abakobo
    Participant
    #5589

    arpie
    Participant

    Woohoo! Mark, you are a superstar.

    #5628

    arpie
    Participant

    Sorry, @Mark, I’m removing the super from my last comment, thus demoting you to ‘star’ until you get round to making this work when the Classes in question are in different source files (see below).  Tested with a namespace declaration, too, with the same results.  On the other hand, if you were to implement a dedicated Mixin/Traits syntax then I would have to promote you to ‘demigod’!

    File test.monkey2 :

    File testmixin.monkey2 :

    Compilation error :

    Mx2cc version 1.1.02

    ***** Building app ‘/home/arpie/Projects/BiaB/MazeM2/test.monkey2’ *****

    Parsing…
    Semanting…
    Translating…
    Compiling…
    Build error: System command ‘g++ -I”/home/arpie/Coding/Monkey/Monkey2/modules/” -I”/home/arpie/Coding/Monkey/Monkey2/modules/monkey/native” -I”/home/arpie/Projects/BiaB/MazeM2/” -std=c++11 -c -o “/home/arpie/Projects/BiaB/MazeM2/test.buildv1.1.02/linux_debug/build/_1src_2test_0testmixin.cpp.o” “/home/arpie/Projects/BiaB/MazeM2/test.buildv1.1.02/linux_debug/src/test_testmixin.cpp”‘ failed.

    g++ -I”/home/arpie/Coding/Monkey/Monkey2/modules/” -I”/home/arpie/Coding/Monkey/Monkey2/modules/monkey/native” -I”/home/arpie/Projects/BiaB/MazeM2/” -std=c++11 -c -o “/home/arpie/Projects/BiaB/MazeM2/test.buildv1.1.02/linux_debug/build/_1src_2test_0testmixin.cpp.o” “/home/arpie/Projects/BiaB/MazeM2/test.buildv1.1.02/linux_debug/src/test_testmixin.cpp”

    In file included from /home/arpie/Projects/BiaB/MazeM2/test.buildv1.1.02/linux_debug/src/../include/test_testmixin.h:12:0,
    from /home/arpie/Projects/BiaB/MazeM2/test.buildv1.1.02/linux_debug/src/test_testmixin.cpp:2:
    /home/arpie/Projects/BiaB/MazeM2/test.buildv1.1.02/linux_debug/src/../include/test_test.h:40:37: error: invalid use of incomplete type ‘struct t_default_TestMixin_1Tt_default_Thing_2’
    struct t_default_ThingList : public t_default_TestMixin_1Tt_default_Thing_2{
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In file included from /home/arpie/Projects/BiaB/MazeM2/test.buildv1.1.02/linux_debug/src/test_testmixin.cpp:2:0:
    /home/arpie/Projects/BiaB/MazeM2/test.buildv1.1.02/linux_debug/src/../include/test_testmixin.h:7:8: note: forward declaration of ‘struct t_default_TestMixin_1Tt_default_Thing_2’
    struct t_default_TestMixin_1Tt_default_Thing_2;
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ***** Fatal mx2cc error *****

    Internal mx2cc build error

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.