emojicode / emojicode

πŸ˜€πŸ˜œπŸ”‚ World’s only programming language that’s bursting with emojis

Home Page:https://emojicode.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected result using Operator Assignment with methods causing side effects πŸ€”

Snoothy opened this issue Β· comments

I encountered an unexpected result related to Operator assignments and methods which has side effects on the mutable variable on the same instance.

I have tried to make a short example of what I have encountered and some variations thereof:

πŸ’­ Simplified example
🏁 πŸ‡
    πŸ†•πŸ–₯οΈπŸ†• ❗ ➑️ computer 
    πŸ”„ computer ❗
πŸ‰

πŸ‡ πŸ–₯️ πŸ‡

    πŸ–οΈπŸ†• pointer πŸ”’ ⬅️ 0

    πŸ†• πŸ‡πŸ‰

    ❗ πŸ”„ πŸ‡
        πŸ’­ Scenario A, unexpected result
        πŸ’­ Set the instances pointer to 123 (side effect) and increment with 4, expecting 127
        pointer β¬…βž• πŸ“ πŸ• 123 ❗

        πŸ’­ Pointer: 4
        πŸ˜€ πŸͺ πŸ”€Pointer: πŸ”€ πŸ”‘ pointer 10 ❗ πŸͺ ❗

        πŸ’­ Reset pointer
        πŸ“ πŸ• 0 ❗

        πŸ’­ Scenario B, works as expected
        πŸ’­ Split operations
        πŸ“ πŸ• 123 ❗ ➑️ ret 
        pointer β¬…βž• ret

        πŸ’­ Pointer: 127
        πŸ˜€ πŸͺ πŸ”€Pointer: πŸ”€ πŸ”‘ pointer 10 ❗ πŸͺ ❗

        πŸ’­ Reset pointer
        πŸ“ πŸ• 0 ❗

        πŸ’­ Scenario C, works as expected
        πŸ’­ Similar approach but more elaborate
        πŸ“ πŸ• 123 ❗ βž• pointer ➑️ πŸ“ πŸ• ❗ 

        πŸ’­ Pointer: 127
        πŸ˜€ πŸͺ πŸ”€Pointer: πŸ”€ πŸ”‘ pointer 10 ❗ πŸͺ ❗
    πŸ‰

    ❗ πŸ“ value πŸ”’ ➑️ πŸ”’ πŸ‡
        value ➑️ πŸ–οΈ pointer 
        ↩️ 4
    πŸ‰

    ➑ πŸ“ value πŸ”’ ➑️ πŸ”’ πŸ‡
        value ➑️ πŸ–οΈ pointer 
        ↩️ 4
    πŸ‰
πŸ‰

The unexpected result relates to pointer β¬…βž• πŸ“ πŸ• 123 ❗. My assumption is that the πŸ“β— mutates the pointer variable and the Operator Assignment then increments it by 4 resulting in a pointer of 127.

However, it seems that the Operator Assignment has already copied the value of pointer before the πŸ“β— has been evaluated.

I'm not sure if this is a bug or if I'm misunderstanding the scopes πŸ€”

commented

Thanks for bringing this up. I see how this is confusing, but this is the way it is intended to work.

pointer β¬…βž• πŸ“ πŸ• 123 ❗ is equivalent to pointer βž• πŸ“ πŸ• 123 ❗ ➑️ pointer (which is not equivalent to C). Emojicode always evaluates expressions in statements strictly from left to right. The result 123 is correct therefore.

I realize the documentation is pretty vague here when saying "an operator is applied to the value of a variable and another operand and the result of the operation is then stored into the variable" but the last example actually implies this: "finally divided by 3" i β¬…οΈβž— 3. This should probably be clarified in the documentation.

We could consider introducing a right operator assignment tough: πŸ“ πŸ• 123 β—βž•βž‘οΈ pointer, which would do what you want.

Just to clarify, the expression pointer β¬…βž• πŸ“ πŸ• 123 ❗ evaluates to 4 not 123. (Pointer (0) + return (4))

Reordering the equivalence to πŸ“ πŸ• 123 ❗ βž• pointer ➑️ pointer would give the desired side effect result of 127 correctly applying changes to pointer before accessing it for addition.

It might be worth considering changing the right operator assignment to πŸ“ πŸ• 123 β—βž•βž‘οΈ πŸ– pointer including πŸ– to keep consistency for mutable variables

commented

Sorry, for the confusion. It appears I accidentally wrote 123 above.

Including πŸ– would indeed be very reasonable as far as consistency is concerned. I don’t really see the need for a right operator assignment though, as code as in your example is confusing anyway.