forBlock can't return double quote

21 views
Skip to first unread message

Peter Cheung

unread,
Nov 26, 2024, 12:47:45 PMNov 26
to Blockly
Hi
   forBlock can't return double quote, it disappeared. Any hints please?

forBlock['sout'] = function (block) {
var statement = block.getFieldValue('statement');
console.log(statement);
const code = `\tSystem.out.println(${statement});`;
return '"'+code;
};

thanks
Peter

Christopher Allen

unread,
Nov 27, 2024, 6:15:25 AMNov 27
to blo...@googlegroups.com
Hi Peter,

   forBlock can't return double quote, it disappeared. Any hints please?

I'm not sure what you mean by "disappeared".  Block generator functions absolutely can return double quotes—they can return more or less string value, since they need to be able to generate any text that might appear in a program in any programming language.
 
forBlock['sout'] = function (block) {
var statement = block.getFieldValue('statement');
console.log(statement);
const code = `\tSystem.out.println(${statement});`;
return '"'+code;
};

I'm not sure what you're trying to do here.  There are a few things that look like they are probably mistakes to me:
  • You have a variable called statement that seems confusingly named: it is used to store the value of a field (a raw value, not a statement or code), and then gets inserted as the argument to println as if it were code that evaluates to a value.  If the field contains text to be printed then you will need to pass it through the quote function on your code generator object (see e.g. here for the text block JavaScript generator function).
  • You include a tab character in the generated code, but (as I already explained in another thread) you don't need to do that because the code that calls this function will do the indentation for you.
  • The last line prepends a (lone) double quote to the generated code, which seems likely to make it syntactically invalid.

All in all, if the field named statement contained the value 'Hello, World!', then this function would return '"\tSystem.out.println(Hello, World);'—not valid code in any programming language that I'm aware of.


Best wishes,

Christopher

Reply all
Reply to author
Forward
0 new messages