Basic reflection question

About Monkey 2 Forums Monkey 2 Programming Help Basic reflection question

Tagged: 

This topic contains 8 replies, has 2 voices, and was last updated by  Mark Sibly 2 years, 1 month ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #7428

    Ethernaut
    Participant

    If I have a string that contains the name of the class I want (let’s say, retrieved from a text file), how do I create an instance of that class?

    Something along the lines of:

    [/crayon]

    Cheers.

    #7443

    Mark Sibly
    Keymaster

    Have you looked at the reflectiontest banana? There’s an exampole dynamically creating an object there…

    #7444

    Ethernaut
    Participant

    Ah, I had not looked into the examples.
    Looking at it from Github now, seems like it will work like a charm.

    Thanks!

    #7445

    Mark Sibly
    Keymaster

    There is still some work to do on reflection, mostly ‘selective’ reflection. Currently reflection is ‘all or nothing’, and adds considerable size overhead to builds.

    But this will happen eventually and apart from that I reckon it works well.

    #7465

    Ethernaut
    Participant

    One thing I also noticed is that the Reflection module doesn’t seem to be included at all in the “Modules Reference” page, so it’s hard to figure out which methods, properties, etc. are available.

    Thanks Mark!

    #7469

    Ethernaut
    Participant

    Ok, still having some trouble, this time when trying to read the fields from a .json file and assign the values to the class fields.

    It works fine if I just print the values and don’t actually assign them, but if you uncomment the lines that do the assignment I keep getting Memory access violations.

    Here’s the json file. The class names are set for each Json object, in this format: “objectName:namespace.Class”

    And here’s the Monkey2 code. Forgive me if it’s a little messy, I was in the middle of experimenting with this.

    Any help appreciated!
    Cheers.

    #7476

    Mark Sibly
    Keymaster

    Ok, haven’t actually tried it but  I think the problem is here:

    Local d:= newObj.GetDecl( key )

    In the case of objects, ‘key’ will contain a ‘:’ char so GetDecl will be returning an ‘invalid’ decl as fields don’t have ‘:’ in their names.

    This isn’t easy to spot, as the debugger is struggling a bit with ‘internal’ types like Decl, Variant etc!

    A better approach may be to use a ‘class’ key in the json and leave field names ‘unmangled’.

    #7479

    Ethernaut
    Participant

    Yay, that worked! (after I cleaned it up and fixed another issue, the Vec class had X and Y fields capitalized, while the json file didn’t… took me forever to catch that one… 😛 )

    Here’s the cleaned up json file:

    [/crayon]

    And here’s the example that uses it:

    [/crayon]

    Mark, if you think this is good enough, feel free to use it as an example for the Reflection module (or for the Json module).

    Do you think Reflection will ever allow generics? I’ll make special non-generic classes for now.
    Thanks!

    #7487

    Mark Sibly
    Keymaster

    Nice!

    Do you think Reflection will ever allow generics?

    Not sure yet, possibly, but it’ll never be able to ‘instantiate’ types that haven’t already been instantiated at compile time, so it’ll be more limited. eg: You wont be able to use Stack<Vec> if there isn’t a ‘compiled in’ one to begin with. But this is probably OK for many uses.

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

You must be logged in to reply to this topic.