JakAttack909 / blockly

Automatically exported from code.google.com/p/blockly
0 stars 0 forks source link

Add a 'return' block #87

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hey,

This isn't a bug. It's just a suggestion for an additional block which allows 
the user to be more flexible.

At the moment a procedure allows as to return a value exactly at one point in 
time. Namely at the end. So if there are lots of if clauses each clause has to 
hold a variable which sets a value and is then put into the return part at the 
end.

But wouldn't it be benefiting to have an independent return block?
That could be used within procedured, but also in the main code itself.

Suggested language block:
Blockly.Language.return_ = {
    category: Blockly.LANG_CATEGORY_X,
    helpUrl: null,
    init: function() {
        this.setColour(Blockly.LANG_CATEGORY_X_COLOUR);
        this.appendValueInput('RETURN')
                .setCheck(null).appendTitle('return');
        this.setPreviousStatement(true);
        this.setNextStatement(true); //this could be false, because no statement behind that return block will be executed. 
        this.setTooltip('');
    }
};

suggested Python generated code:

Blockly.Python.return_ = function() {
    var value = Blockly.Python.valueToCode(this, 'RETURN') || '';
    var code = 'return ' + value + '\n';

    return code;
};

This is just a small block and I think the confusion of a newbie user will be 
minimal since the return statement is also used in one of the procedure blocks.

Another additional block could be a try and catch block. But only if this is 
not to much. 

best regards
Marc 

Original issue reported on code.google.com by borntob...@googlemail.com on 8 Nov 2012 at 8:11

GoogleCodeExporter commented 8 years ago
p.s. I hope the title is not rude or demanding.

If so: Sorry for that.

Original comment by borntob...@googlemail.com on 8 Nov 2012 at 8:12

GoogleCodeExporter commented 8 years ago
And it's me once again.

Another useful block is a block which returns the value 'null' :)
e.g. like:

Blockly.Language.null_ = {
    category: Blockly.LANG_CATEGORY_X,
    helpUrl: null,
    init: function() {
        this.setColour(Blockly.LANG_CATEGORY_X_COLOUR);
        this.appendDummyInput().appendTitle('null');
        this.setOutput(true,null);
        this.setTooltip('Return null');
    }
};

Python generated code:
Blockly.Python.null_ = function() {
    return ['null',Blockly.Python.ORDER_NONE]; //check to which order category null belongs to?!
};

Original comment by borntob...@googlemail.com on 8 Nov 2012 at 8:33

GoogleCodeExporter commented 8 years ago
added in r508

Original comment by gasolin on 13 Nov 2012 at 3:02

GoogleCodeExporter commented 8 years ago
Since "null" doesn't have much to do with procedures, I've moved this block to 
the logic category.  (r509)

With respect to "return", I searched through hundreds of thousands of lines of 
code at Google and found that in virtually all cases where return is used 
before the end of a function (i.e. not the last line), the return is wrapped in 
an if statement.  This is known as a "guard clause": 
http://c2.com/cgi/wiki?GuardClause

Accordingly, I have replaced the "return" block with an "if-return" block.  It 
also warns if it is not enclosed in a procedure, and it automatically mutates 
to have (or not have) a return value depending on the enclosing procedure.  
(r511)

Original comment by neil.fra...@gmail.com on 17 Nov 2012 at 11:49