Structs and foreach loops

About Monkey 2 Forums Monkey 2 Programming Help Structs and foreach loops

This topic contains 11 replies, has 6 voices, and was last updated by  nerobot 1 year, 6 months ago.

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

    cocon
    Participant

    I have a List<Vec2f> and I want to iterate it and change it’s contents, after a few minutes I realized that structs because are handled by value would not be mutated as referenced classes do.

    If you have any workaround on this I would be interested to know.

    #8893

    abakobo
    Participant

    You may use a temporary pointer.
    Local structPtr:myStruct Ptr
    structPtr=varptr myStructInstance
    structPtr->x=123

    Never tried it though..
    If you want a non temporary pointer you’ll have to keep your structs alive.

    #8894

    Ethernaut
    Participant

    Hmmm, couldn’t figure it out with List… here’s how I do it with Stacks, though.

    #8896

    Danilo
    Participant

    Ethernaut’s code changed to use a list of Vec2f:

    Assign new value:

    #8919

    cocon
    Participant

    Oh, I see!

    By looking at Monkey’s List source code, my guessing is I see that the All() method might cooperate with the Eachin statement in the background. The All method however returns a new Iterator, not the existing (the essense of the elements) one.

    I did this experiment and I see that I can’t mutate the element of the list directly, however I can mutate the local copy. Is this behavior valid or not?

    #8920

    Mark Sibly
    Keymaster

    I did this experiment and I see that I can’t mutate the element of the list directly, however I can mutate the local copy. Is this behavior valid or not?

    Yes, because structs are always passed ‘by value’ (ie: copied), so this line…

    Local item := list.FirstNode().Value

    …copies FirstNode().Value to item.

    One way to deal with thinking about this stuff is to mentally replace ‘some struct type’ with, say, ‘int’ (also a struct type) in which case you’ve simply got a list of Ints, and hopefully it’s clear that…

    Local item := list.FirstNode().Value

    …will set item to a *copy* of FirstNode().Value as, like all struct types, ints are copied when passed to functions, returned from functions or assigned to variables.

    #8923

    Danilo
    Participant

    List extension ‘ForEach’ & ‘ForEachBackwards’ for use with structs:

    #8929

    Mark Sibly
    Keymaster

    Nice!

    #10803

    cocon
    Participant

    Another question is how can I send the struct to a function for updating?

    This currently does not work:

    #10806

    Mark Sibly
    Keymaster

    Same problem – Current is returning a *copy* of the iterator item. Modifying this copy has no effect.

    Actually, if you just use Vec2’s ‘x’ and ‘y’ fields directly (not ‘X’ and ‘Y’ properties), eg: iter.Current.x=0, you will get a compile time error, because the compiler ‘knows’ you are trying to modify a copy. However, the compiler doesn’t know exactly what X or Y properties do – they may or may not actually modify Self – and I didn’t want to just disallow property/method calls in general on returned copies or subject people to the joys of ‘const correctness’, so it’s a bit of a half-assed check.

    The solution is to get current item, modify it, and then set it back (aka read-modify-write), eg:

    Same applies to Stacks etc.

    However, you don’t need to do this with plain arrays, eg: myarray[i].X=0 will work fine, as an array element is really just a special type of variable (which is why you can Varptr it).

    This means you can in fact do a little hack with stacks where you can set elements directly:

    mystack.Data[i].X=0

    This is because the Data property of Stack returns the underlying array. You do need to be careful ‘i’ is in range though, as array may be longer than Stack!

    #10814

    cocon
    Participant

    Oh, I see! So this assignment statement is really important from what it seems.

    #10815

    nerobot
    Participant

    This means you can in fact do a little hack with stacks where you can set elements directly

    Hm, very interesting!

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

You must be logged in to reply to this topic.