Best Practice: Class vs Struct and Array vs List

About Monkey 2 Forums Monkey 2 Programming Help Best Practice: Class vs Struct and Array vs List

Tagged: 

This topic contains 5 replies, has 4 voices, and was last updated by  Anatol 1 year, 6 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #10907

    Anatol
    Participant

    Hi,

    this is a question for the seasoned Monkey2 coders. I’m a bit confused as to what type makes the most sense. If I take this example:

    At first the control Class at the top was a Struct, and both Struct or Class work fine, but I’m wondering which is the better option.

    The documentation says:

    “A struct is a ‘value type’, whereas a class is a ‘reference type’. This means that when you assign a struct to a variable, pass a struct to a function or return a struct from a function, the entire struct is copied in the process.”

    I’d think that generally (or at least in the code above) a reference type is the better option, but I’m not sure if that’s the right conclusion.

    The other question is about Arrays vs Lists. I guess Arrays are somewhat leaner, but with a list I could just do a

    instead of this whole

    What’s the better choice if that can even be generalised? Thanks!

    #10919

    Mark Sibly
    Keymaster

    Hi,

    I recommend  Stack<T> as a good general purpose container. It behaves very much like an array you can add things too, eg:

    You can use Insert and Erase to insert and erase elements by index, Find to find the index of an element, Remove to an element by value etc.

    #10928

    Anatol
    Participant

    Thanks Mark. That’s good advice.

    In this case I actually changed it to a key map so that I have easy access to individual collection items via its key.

    Also, very nice that we have Lamda functions now. It’s really fun to use Monkey 2, now that I got more used to the syntax and strict types again.

    #10931

    nerobot
    Participant
    1.  Is your Trigger() kind of CanExecute() equivalent?
    2.  Inside of DisableAll / EnableAll you do double work – go through keys and then find by keys. But you can directly Eachin by values
    #10934

    cocon
    Participant

    From what I think, the “Enabled” field can be controlled by configuration, the trigger is actually where the input is updated.

    Interesting bit of code, I will use it as well.

    #10975

    Anatol
    Participant

    Hi,

    thanks for the hint nerobot. I’m just doing the “double work” out of habit, so should I ever change the way I disable controllers I only need to change it in one place, i.e. in Method Disable. I think here the performance impact should be negligible.

    And cocon, I just came up with this manager class to keep it as flexible as possible. In the setup I basically just create controls with a condition and a callback. I could add this stuff into the render loop but I prefer to have it packaged in this class not to “pollute” the render loop too much, and also to easily disable/enable controls later. That’s where the Lamda functions are really useful.

    So in the main program this is how I’m using this class:

    I could later then just do something like

    <div id=”crayon-59d4ac7531a47621632511-36″ class=”crayon-line”></div>

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

You must be logged in to reply to this topic.