Reflection questions

About Monkey 2 Forums Monkey 2 Development Reflection questions

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

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #5694

    nerobot
    Participant

    Don’t know is it bug or impossible to implement..

    I need to get name of template type, to do it I wrote this code:

    and I got “Error : Identifier ‘T’ not found”.

    Theory. When we call this method then ‘T’ *is* one of concrete type, so Typeof() *can* get it type. But error is at semant time not at compile.

    For my luck, I find a solution – by adding optional parameter that we don’t need to use – it works with null-value.

    #5695

    nerobot
    Participant

    Also I intrested in further improvement of reflection (generics, instance creation).

    Is it possible to add into reflection module function like NewInstance( stringNameOfType:String )?

    and use like this

    it looked not easy…

    With this stuff we can get object-serializer.

    #5696

    Mark Sibly
    Keymaster

    Use Typeof<> for ‘type expressions’ (eg: Typeof<mojo.Canvas>) and GetDecl( “New” )/Invoke() to create a class instance at runtime.

    Check out my reflection blog post and the reflectiontest banana which should cover the basics, including creating a new class instance at runtime.

    #5701

    nerobot
    Participant

    Really.. thanks!

    New question. Mark, please check this code. If we comment import of reflection – code works, else – don’t work.

    #5706

    codifies
    Participant

    just a question

    [/crayon]

    you are casting something to an interface?

    #5709

    nerobot
    Participant

    Yes. Why not.

    #5715

    Mark Sibly
    Keymaster

    Can you try and narrow it down?

    Have no idea what it’s supposed to do and am feeling too lazy to try harder…

    #5723

    Mark Sibly
    Keymaster

    Ok, definitely seems to be related to the stack ptr param for RemoveFromStack, just passing a plain stack works fine.

    This should not of course cause a c++ error and I’ll check it out, but at the same time what’s up with the pointer?

    Object Ptr’s (and array and function…well, ALL ptrs!) are dangerous. They can mean there’s nothing’s keeping an object alive so the object can be GC’d unexpectedly. They should only be used in extreme situations – generally dealing with extern libs or something.

    I have kind of struggled with whether to or allow ptrs to GC-able objects at all or not but they’re in now so are staying, but be aware that if you use them needlessly in code posted here (without explanation) you will likely receive this lecture every time!

    #5737

    nerobot
    Participant

    I didn’t explain, sorry.

    All I need is remove value from stack. But as stack is struct it passed in function as copy-of-stack, and my remove have no effect for original stack. I tried to use inerator, but I got error and did not understand this. And using of reference (via ptr) seems to be the easiest way for me. But now I tried iterator again and understand what I was doing wrong.

    So, new code:

    And usage:

    Yesterday I forgot to add prefix here ^

    and got unresolved types error (“Can’t find overload for …. with argument types ….)

    #5742

    Mark Sibly
    Keymaster

    Stack is not a struct!

    Stack, List, Map and Deque are all classes so instances are always passed by reference.

    Also, I recently added a RemoveIf to stack (and list) which may be useful…

    #5743

    nerobot
    Participant

    Oh.. really. I keep opened stack.monkey2 file and don’t see that it’s a class, omg..

    I found another error in my code, which I began to doubt in the stack work.

    Big Thanks, Mark!

    #5744

    Mark Sibly
    Keymaster

    I’m quite impressed you’ve done ted2go without knowing that!

    #5745

    nerobot
    Participant

    🙂 🙂 🙂

    I know.. just need to be more attentive.

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

You must be logged in to reply to this topic.