Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default arguments in function blocks #2384

Open
pavi2410 opened this issue Jan 21, 2021 · 16 comments
Open

Support default arguments in function blocks #2384

pavi2410 opened this issue Jan 21, 2021 · 16 comments

Comments

@pavi2410
Copy link
Member

pavi2410 commented Jan 21, 2021

Describe the desired feature
Allow components and extensions to define default values for any/all/none of the parameters.

Give an example of how this feature would be used
This will ease the use of functions with many parameters. Users won't have to fill every parameter by themselves. This will also allow extensions to define a default argument for functions.

Why doesn't the current App Inventor system address this use case?
It does with the Notifier.ShowChooseDialog block but that is hardcoded

{matchingMutatorAttributes:{component_type:"Notifier", method_name:"ShowChooseDialog"},

Why is this feature beneficial to App Inventor's educational mission?
Make blocks easy to use and save users time.

@Vishwas-Adiga
Copy link
Member

It might also be worth looking into using shadow blocks for these default parameters

@SuPythony
Copy link

@Vishwas-Adiga
What are shadow blocks?

@ewpatton
Copy link
Member

We have previously discussed using shadow blocks among the team and the power users. At the time we decided against it. At the very least, there are a number of changes here that need to be made to support annotating this information in the parameters and storing that information in simple_components.json. Drawer.js will also need to be modified to accommodate this. We'll also want to change the existing components to use this new approach rather than the special code paths currently used. All these things can be done before a decision on whether shadow blocks are used or not.

@ewpatton
Copy link
Member

Before anyone takes on this task I'd prefer that we discuss the design first w.r.t. the annotations and changes to simple_components.json.

@halatmit
Copy link
Contributor

halatmit commented Jan 21, 2021 via email

@ewpatton
Copy link
Member

@halatmit Yes, I'd like to see a more formal document of what this will look like prior to investing a lot of time implementing it.

@pavi2410 Do you have concrete thoughts on this? I have my own and can start drawing something up.

@pavi2410
Copy link
Member Author

I do not have any concrete thought on this except a few ideas on the components side.

Here's how Thunkable X implements this using shadow blocks
image

@ewpatton
Copy link
Member

I've started a design doc here: https://docs.google.com/document/d/1is90WHxQxX3g6wiq46Vnzb6QkDeX1e4tw7X1bBk2-pk/edit#

@pavi2410
Copy link
Member Author

Could this be extended to support block properties as well? Or is it beyond the scope for now?

@ewpatton
Copy link
Member

I'm not sure much if anything needs to be done for properties. We should be able to infer this information from the designer property default value and the type information for the property. I did a prototype of this back in 2017 in fact.

@BeksOmega
Copy link
Collaborator

Actually I think I did most of the work to support this last summer =) When I created the system to support dropdown blocks I tried to make it extensible enough so that new annotations could be defined and associated with new types of helper blocks (eg booleans, text inputs, etc). See my documentation on Adding New Types of Helper Blocks to the Blocks Editor. You should be able to create a parameterized annotation (representing a helper block) that takes in information about the default value and displays it on the helper block.

But I could also be missing the point of this issue hehe.

If I'm not missing the point, I recommend closing this issue in favor of creating issues for specific types of helper blocks & their associated annotations.

Also with regard to shadow blocks, during the summer I was told that the main issue with them was that they enlarged the save file size, because each shadow block needs to have at least two xml nodes associated with it (the block node and the field value node). There was a bit of talk about implementing some custom behavior so that shadow blocks wouldn't save their values (eg if you replaced a shadow with a real block, the shadow would forget the value input into its field) but we decided it was outside the scope of my summer project.

@ewpatton
Copy link
Member

@BeksOmega Your documentation I do think is helpful for the annotations piece, but I am imagining that these would not be new blocks but that the engine would construct the constructor block for the given type (logic_boolean, text, math_number, etc.) rather than defining a brand new block.

@BeksOmega
Copy link
Collaborator

@BeksOmega Your documentation I do think is helpful for the annotations piece, but I am imagining that these would not be new blocks but that the engine would construct the constructor block for the given type (logic_boolean, text, math_number, etc.) rather than defining a brand new block.

Coolio, if I understand what you're saying, that should work fine! You'd just skip section 5 of the doc and move straight to section 6. For example:

Blockly.Util.xml.helperKeyToXML= function(helperKey) {
  switch(helperKey.type) {
    case 'OPTION_LIST':
      return this.blockTypeToXML('helpers_dropdown', {key: helperKey.key});
    case 'BOOLEAN':
      // You could either pass the default value as a mutation (like above),
      // and add a mutator to the logic_boolean block.
      // Or you could add a function to utils_xml.js to create field XML.
      return this.blockTypeToXML('logic_boolean');
  }
}

@pavi2410
Copy link
Member Author

Question: Should we allow default values that are not primitive or complex?

IMO, we shouldn't. What is your take on this?

@BeksOmega
Copy link
Collaborator

Question: Should we allow default values that are not primitive or complex?

IMO, we shouldn't.

Hmm I don't really have an opinion. But I am curious, why don't you think they should be allowed? Is it just for XML size reasons, or is it something else? Also, just to be clear, by not primitive/complex do you mean things like lists/dictionaries?

@RajaHarshVardhanSolanki
Copy link

I want to give a proposal to the developers, It is very useful feature in Extension making, Set a default value of Property Blocks in @DesignerProperty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants