Handling variable types within an equality condition

35 views
Skip to first unread message

roberto porcelli

unread,
Sep 1, 2025, 3:03:59 PM (6 days ago) Sep 1
to Blockly
Hi all, within the construct representing the equality condition (in an if-then-else block), I need the right-hand operand to display a drop-down menu with a list of variables of the left-hand operand's type. Do you think this is possible? In other words, is it possible to know the type of the variable inserted within the left-hand operand of the equality construct when the user enters it, so the system can appropriately construct the right-hand operand's drop-down menu?
Thanks

Christopher Allen

unread,
Sep 2, 2025, 9:54:48 AM (5 days ago) Sep 2
to blo...@googlegroups.com
Hi Roberto,

Hi all, within the construct representing the equality condition (in an if-then-else block), I need the right-hand operand to display a drop-down menu with a list of variables of the left-hand operand's type. Do you think this is possible? In other words, is it possible to know the type of the variable inserted within the left-hand operand of the equality construct when the user enters it, so the system can appropriately construct the right-hand operand's drop-down menu?

If you're creating a block to compare two variables, like this:
Screenshot 2025-09-02 at 14.35.17.png
then that should be pretty straight forward, and you should be able to implement this in a subclass of FieldVariable using a similar technique (field validators) as is used in the field-dpendent-dropdown plugin.

On the other hand, if you want a block that can compare an arbitrary value to a variable, like this:
Screenshot 2025-09-02 at 14.40.21.png
then this will be rather more difficult.  In fact, it's not in general guaranteed to even be possible to ascertain the value of the expression on the left hand side of the comparison: if the user can connect a procedures_callreturn block to the input then determining the type of the returned value might be equivalent to solving the halting problem: the only general way to know is to generate the code and run it, and hope that the procedure eventually terminates and returns a value…

(Of course if you are using typed variables you may well also have typed procedure returns, and if so (and you're willing to ignore the possibility that the procedure never returns) then it should be possible to do reliable static analysis of the type of the connected expression.  It will still be a non-trivial, though, because you'll need to be able to analyse any combination of connected value blocks.)

A reasonable compromise might be to attempt to do some limited static analysis of the connected value expression—say, by seeing if the block directly connected has a defined type for its output—and showing all available variables if not.


Best wishes,

Christopher 

Max Stephen Russell

unread,
Sep 2, 2025, 10:23:07 AM (5 days ago) Sep 2
to Blockly
“and hope that the procedure eventually terminates and returns a value…”

Ha ha!
Reply all
Reply to author
Forward
0 new messages