vopwn55 / powerlang-issues

0 stars 0 forks source link

"and" operator will compile all conditions at once rather than in order #22

Closed Paracosm-Daemon closed 3 weeks ago

Paracosm-Daemon commented 4 weeks ago

Unlike most programming languages, using and in an if statement will cause an error to be thrown if the second part of the operator relies on the first. As an example, here is the bug I encountered while cleaning up connections:

01:59:14 - Game.Players.Paracosm_Daemon.PlayerGui.JointEditor.$Runtime.PowerlangImported:instop-471: Expected event, got table

I traced this error back to this particular if statement:

image

Originally, I chalked this up to an error in how RetroStudio handles nil values, but when I looked closer, I realized that both conditions were being checked at once, despite the rig connection not being of the type RBXScriptConnection, thus causing this exception to pop up.

This is likely also the case for or, where both sides will be compiled simultaneously, rather than going in order of which is the last condition to check. This distinction is very important, given it can complicate seemingly basic code, e.g the following:

image

vopwn55 commented 4 weeks ago

Hi, this is intentional. The Powerlang compiler has no clue by itself what value a variable might have, and it relies on the RetroStudio runtime to determine that. As the and keyword directly translates to the AND block, it's up to RetroStudio to handle that.

A workaround for this is to use two nested IF directives. If this feature is heavily requested by other members of the community, I might re-implement the way logic gates are handled.

Paracosm-Daemon commented 4 weeks ago

As the and keyword directly translates to the AND block, it's up to RetroStudio to handle that.

Noted! If it's not mentioned in the docs, I would prefer it is as this is a direct contradiction to how these conditions work in Lua

vopwn55 commented 4 weeks ago

Done! https://vopwn55.gitbook.io/powerlang-docs/beginners-guide/conditions