Generics puzzle

About Monkey 2 Forums Monkey 2 Programming Help Generics puzzle

This topic contains 14 replies, has 5 voices, and was last updated by  wiebow 2 years, 8 months ago.

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #3267

    Ethernaut
    Participant

    I’m usually intimidated by more “advanced” OO concepts like Generics and Interfaces, but have been studying it lately. I ran into this following issue, which may be trivial to anyone more familiar with Generics, but is puzzling me.

    Imagine that I have a generic Entity class that can have components. Normally the components are of the “Component” class, but in this case I want to create an “AdvancedEntity” class that only uses “AdvancedComponent”  components (both still share most of their inner workings with their respective base classes, so it’s justifiable to have this structure).

    This simple code example works:

    However, notice how I’m accessing the “components” map  directly, and I wanted to make that private.

    I want to wrap that into an “AddComponent( name:String, comp:T ) method that is supposed to add whatever component subclass that the AdvancedEntity uses, in this case an Advanced component. I tried this, but it doesn’t work.

    It actually works as intended if I remove the “comp.entity = Self” line inside the AddComponent method, but that’s not good, since I need to provide the component with that information.

    Maybe the ‘entity’ field could be a Generic type as well? I tried that, but ran into “cyclic declaration” errors.

    Help!

    #3271

    Mark Sibly
    Keymaster

    I think you need to make Component generic too – I tried this but the compiler complained about cyclic declarations, which I don’t think is quite right. Will look into it a bit later.

    #3272

    Mark Sibly
    Keymaster

    To simplify things a bit, is this more or less what you’re after?

    It doesn’t compile due to cyclic declaration error, but I *think* it should be possible:

    (Just added some hypothetial ‘Wheres’ that wont work yet, but I think they make it clearer!)

    #3273

    Ethernaut
    Participant

    Yes! That’s exactly what I’m looking for.

    Thanks Mark, I also ran into the cyclic error when I tried something similar and thought it was actually invalid.

    #3274

    Jesse
    Participant

    why would anybody want to torture themselves with that? I think it’s overcomplicating something that can easily be solved with a different approach.

    #3275

    Mark Sibly
    Keymaster

    Such as?

    #3278

    Ethernaut
    Participant

    At the moment I’m just trying to learn, and this seemed like a good “stress test” for the technique. I had never used Generics before, and I like objected oriented style a lot, so this felt like a gap in what I could do.

    The particular case that prompted me to use this (not my test example code) can be solved with casting, but it’s a method that would be called a lot and I’d rather avoid casting if possible.

    My next step is to learn interfaces properly. I understand how they work, but I’ve never thought of a practical situation where I’d rather use interfaces than classes. Also keep in mind that I’m an artist, not a programmer, and was never formally taught any of that, so I really appreciate that M2 is simple enough for me to learn it but still offers those possibilities.

    Thanks!

    #3279

    Jesse
    Participant

    Such as?

    I don’t know. but a first object that needs another object to be created and the other object needing the first object for that one to be created is kind of Pointless to me. I know it’s just my thinking and  I am not an authority on this but It doesn’t make any sense to me.  I don’t want to start a big argument about this but I would like to see a good implementation of it to change my mind.

    and Yes my point can be ignored.

    @ethernaut

    you won’t need casting if you apply good use of Polymorphy and may be useful for generics as well.

    #3280

    Simon Armstrong
    Participant

    you won’t need casting if you apply good use of Polymorphy. and may be useful for generics as well.

    What does this even mean? If you are going to post in a negative manner at least try and do it constructively.

    Generics are like macro assembler and can generate very optimal code. They are not pointless.

    #3281

    Jesse
    Participant

    I never said generics are pointless, just that particular use of them.

    don’t get so defensive, as I said, Ignore. it’s just an opinion by this humble programmer that learned to program mostly by self.

    #3282

    Simon Armstrong
    Participant

    Your opinion is that the use case is pointless? The fact is inlining of method AdvancedUpdate by high volume iterators of AdvancedEntities is a worthy request.

    #3283

    Jesse
    Participant

    @Simon,

    I am not going to question your opinion you are quite a bit smarter and talented than I am. but until I see a good example of it (the reason it’s getting cyclic redundancy) i won’t change my mind. When you show me a good example I might change my mind, apologize and start using it that way. I am a bit of a hard head that way. Sorry.

    #3309

    Mark Sibly
    Keymaster

    That’s cool, I wasn’t getting at you or anything, but it *is* an interesting problem IMO.

    The traditional way to solve this would almost inevitably involve a dynamic downcast somewhere, possibly eventually in quite a few places (that’s why I wanted to see your solution, perhaps there’s a way to do it without..?) and downcasts are both dangerous and slow.

    I think what fascinates me about generics and problems like this is that they can provide ‘almost dynamic’ like typing behaviour, but with 0 runtime overhead since everything’s done at compile time.

    Are they confusing? Perhaps, although I think I presented the fundamental problem pretty clearly above. Aand perhaps by messing around with problems like this we can come up ways to make generics easier to use? Maybe not though…

    And I’d still be interested to see what people could come up with in terms of a non-generic solution – perhaps there’s a non-generic way to solve this too?

    #3310

    Jesse
    Participant

    Yes, you presented the problem pretty clearly. I just don’t like it. My response is that  I wrote a thought  that came out way to loud,  An opinion that maybe I should have kept to myself.

     

    It was interesting to see Simon get on the defensive as well. 🙂

    #3316

    wiebow
    Participant

    There are quite a few advanced properties of the language I don’t use, and I still solve my problems so yes, there is probably a better solution to this problem.  I also worked with component systems and extending components more than one time is asking for problems. But this IS an interesting discussion, even if I , like Jesse, don’t see myself jumping into this rabbit hole 🙂

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

You must be logged in to reply to this topic.