fist class function blocks, how to do parameters the fancy way?

497 views
Skip to first unread message

amigojapan

unread,
Feb 7, 2016, 11:10:54 AM2/7/16
to Blockly
Hi, thanks in advanced for your help! I am trying to make a block that implements a first class function, I have the basics done, but I can't figure out (in the block factory at least) how to do the fancy way of passing parameters to the functions, the way the normal function blocks have,  like you can add as many parameters as you want and call them what you like. 

I am looking for this:

here is what I have up to now:


Blockly.Blocks['functional_first_class_function'] = {
 init: function() {
   this.appendStatementInput("FUNCTION")
       .appendField("set")
       .appendField(new Blockly.FieldVariable("item"), "VARIABLE")
       .appendField("to the following function:");
   this.appendDummyInput()
       .appendField("return")
       .appendField(new Blockly.FieldVariable("item"), "RETURN");
   this.setInputsInline(true);
   this.setColour(20);
   this.setTooltip('');
   this.setHelpUrl('http://www.example.com/');
 }
};
Blockly.JavaScript['functional_first_class_function'] = function(block) {
 var variable_variable = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('VARIABLE'), Blockly.Variables.NAME_TYPE);
 var statements_function = Blockly.JavaScript.statementToCode(block, 'FUNCTION');
 var variable_return = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('RETURN'), Blockly.Variables.NAME_TYPE);
 // TODO: Assemble JavaScript into code variable.
 var code = variable_variable+'= function(){\n+'+statements_function+' return'+variable_return+'\n}';
 return code;
};

amigojapan

unread,
Feb 7, 2016, 11:21:44 AM2/7/16
to Blockly
hmmm, or are the blockly variables always global anyway? so it seems kind of pointless to have that fancy parameter passing , people could always just use the global variables instead of parameters right?

Carlos Pereira

unread,
Feb 8, 2016, 3:13:34 PM2/8/16
to Blockly
Well, depending on the specific language used there could be inner scope variables. But anyway, if you want to have arguments you'll have to use mutators, the best way to achieve that is to copy and modify the ones in the procedures file: https://github.com/google/blockly/blob/master/blocks/procedures.js It might not be very straight forward at the beggining, but you could probably ask more questions from that point.

Padow Usmar

unread,
Feb 11, 2016, 7:09:36 AM2/11/16
to blo...@googlegroups.com
thanks, I will take a look
--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/uqr_f-WkDMI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages