Skip to content

Dynamically typed variable getters and setters #1281

Description

@rachel-fenichel

Blockly should provide two additional blocks: variables_get_dynamic and variables_set_dynamic. These blocks should dynamically change the types on their output/input connections based on the type of the currently selected variable.

We should also provide a custom tag for toolbox categories that populates the toolbox with the dynamic variable blocks instead of the standard ones. Probably VARIABLES_DYNAMIC.

Why: It is now possible to create variables with different types, and to get the type of the currently selected variable. In general on blocks with typed variable fields we can't infer the type of the output statically, but we can infer it dynamically for the general getter and setter blocks. This saves developers from having to make separate getters and setters for each variable type they create. (They still may want to in order to be explicit about their types.)

Why create new blocks instead of changing variables_get and variables_set: backwards compatibility.

These blocks will have the same shape as the variables_get and variables_set blocks:
image

They will have the same basic definitions as the variables_get and variables_set blocks (see below) but have onchange handlers to set their connection types appropriately.

// Block for variable getter.
  {
    "type": "variables_get",
    "message0": "%1",
    "args0": [
      {
        "type": "field_variable",
        "name": "VAR",
        "variable": "%{BKY_VARIABLES_DEFAULT_NAME}"
      }
    ],
    "output": null,
    "colour": "%{BKY_VARIABLES_HUE}",
    "helpUrl": "%{BKY_VARIABLES_GET_HELPURL}",
    "tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}",
    "extensions": ["contextMenu_variableSetterGetter"]
  },
  // Block for variable setter.
  {
    "type": "variables_set",
    "message0": "%{BKY_VARIABLES_SET}",
    "args0": [
      {
        "type": "field_variable",
        "name": "VAR",
        "variable": "%{BKY_VARIABLES_DEFAULT_NAME}"
      },
      {
        "type": "input_value",
        "name": "VALUE"
      }
    ],
    "previousStatement": null,
    "nextStatement": null,
    "colour": "%{BKY_VARIABLES_HUE}",
    "tooltip": "%{BKY_VARIABLES_SET_TOOLTIP}",
    "helpUrl": "%{BKY_VARIABLES_SET_HELPURL}",
    "extensions": ["contextMenu_variableSetterGetter"]
  }

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions