How to know what kind of data we have when we set a variable

98 views
Skip to first unread message

Adarsh C

unread,
May 2, 2021, 4:55:59 AM5/2/21
to Blockly
Hello All,

I have this standard block of code where I get we set the variable when we convert the code to Python. How can we know is the variable is holding List(Array in Blockly), Number or String in the following function?


Blockly.Python.variables_set = function (a) {
var b = Blockly.Python.variables(a, "VALUE", Blockly.Python.ORDER_NONE) || "0";

return Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.VARIABLE_CATEGORY_NAME) + " = " + b + "\n";
};

Thank you for the support,
Adarsh C

Adarsh C

unread,
May 2, 2021, 5:10:39 AM5/2/21
to Blockly
Above code had a bug this is the code


Blockly.Python.variables_set = function (a) {
var b = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "0";

return Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.VARIABLE_CATEGORY_NAME) + " = " + b + "\n";
};



Jason Schanker

unread,
May 2, 2021, 8:59:46 AM5/2/21
to Blockly
Hi Adarsh,

This won't be possible in general.  These language generator functions are merely responsible for translating blocks of code to strings of Python code.  They do *not* run any code.  For example, consider a contrived example of blocks generating the following Python code:

def f(): return "abc" if input("(s)tring or (n)umber?")=="s" else 42
x = f()
print(x)

Blockly.Python.variables_set would be passed a reference to a function call block.  The value of this code which would be stored in b, would then be set to the string "f()".  Without running the code, it would be impossible to know whether f returns a string or a number as it depends on the user input.

However, you can determine the types of blocks with safety checks (ones that pop out when they are placed in spots that do not make sense) such as the `math_number` block.  To get this information, you could use `a.outputConnection.getCheck()` which will return an Array of all specified compatible types such as ["Number"] for the block referred to by `a` or or null if no safety checks have been specified (See: https://github.com/google/blockly/blob/master/core/connection.js#L595)

Please feel free to follow up with any additional questions.

Best,
Jason

Adarsh C

unread,
May 2, 2021, 9:46:51 AM5/2/21
to Blockly

Hello Jason,

 Ya that point is clear for me.

But when we define the blocks we mention what is the output type like

output: "Number"
output: "String"

etc, and these kind of blocks will be set as input to the set var block. I wanted to know the output type of the block we set to var?


Jason Schanker

unread,
May 2, 2021, 1:34:56 PM5/2/21
to Blockly
Sorry, I made a mistake in the code excerpt I had given before.  It should be:

a.getInputTargetBlock("VALUE").outputConnection.getCheck() 

This way you're doing a check on the output connection of the block that the variable is being set to instead of the variable block itself.  This should give you an Array of output types so if you set it to a single type in your block definition, it should be at index 0 in this Array.  E.g., if output: "Number", you'd get ["Number"].

Does this answer your question or am I misunderstanding?
Reply all
Reply to author
Forward
0 new messages