I try to "learn more" and "learn from" functional programming, and the idea of immutability is good for concurrency, etc.
As a mental exercise, I imagined a simple game in which a character like Mario-esque can run and jump with enemies shooting at him ...
Then I tried to imagine that it was written functionally using immutable objects.
This raised some questions that puzzled me (as an imperative OO programmer).
1) If my little guy is at position x10, y100 moves 1 unit to the right, do I just recreate it using his old values from +1 to his position x (e.g. x11, y100)?
2) (If my first assumption is correct) If my input stream moves the right guy 1 unit and my enemy thread AI shoots the little guy and the enemy thread allows before input-thread, then my guy will lose health, and then when the input is allowed threads will return it and move it to the right ..
Does this mean that I can’t shoot - and forget my flows even with immutability? Do I need to send my threads to do my work, and then a new () little guy synchronously when I have the results of both threaded operations? or is there a simple “functional” solution?
This is a slightly different threading problem than I face day after day. Usually I have to decide if I worry about which order flows are allowed or not. Where, as in the aforementioned case, I technically do not care if it deals damage or moves first. But I do not care if the race conditions during the creation of the instance lead to a complete loss of these threads.
3) (Again, if my first guess is correct). Constantly created object instances (like Mario) have terrible overhead, which makes it a very serious / important design decision?
EDIT Sorry for this extra editing, I was not that good practice here, about the following questions ...
4) If immutability is what I should strive for and even jump, although the hoops of creating new versions of objects that have changed ... And if I create an instance of my boyfriend every time he moves (only with a different position), me the exact same problems as if it were volatile? is that something that referred to it at some point in time really looks at old values? .. The more I delve into it, the more my head rotates, creating new versions of the same thing with different by values, it just seems volatile, via hack: ¬
I think my question is: how much should it work? and how beneficial has it just changed its position?
for(ever)//simplified game-loop update or "tick" method { if(Keyboard.IsDown(Key.Right) guy = new Guy(guy){location = new Point(guy.Location.x +1, guy.Location.y)}; }
Also confusing: the above code means the guy is volatile! (even if its properties are absent)
4.5) Is this possible with a completely unchanged guy?
Thanks,
J.