Jigsaw Development

From IPRE Wiki
Jump to: navigation, search

Using Objects in Jigsaw


Currently, there is no way to use blocks to call methods on an object. This makes using objects hard, as you must use a statement-block or assignment-block to call the method.

Possible solution 1

  1. Create a with-block that takes an object
  2. Create a manner to select methods related to that object

Here is a sample with-block:


The idea is that robot1 and robot2 are robot objects. The forward and backward blocks are somehow selected and added to the do list.

We could have a naive export to Python:

# with robot1 do:
_ = robot1
_.forward(1, 1)
_.backward(1, 1)

# with robot2 do:
_ = robot2
_.forward(1, 1)
_.forward(1, 1)

But, in Python we could use the real with-statement. This would require that we wrap any plain value (not a proper Context Manager):

class Context():
    def __init__(self, value):
        self.value = value
    def __exit__(self, type, value, traceback):
    def __enter__(self):
        return self.value

with Context(23) as _:

The above blocks could be exported to Python with something like:

with Context(robot1) as _:
    _.forward(1, 1)
    _.backward(1, 1)

with Context(robot2) as _:
    _.forward(1, 1)
    _.forward(1, 1)

There are a number of Context Managers already defined in Python, including files. These would not need to be wrapped in a Context.

See [1] for more information on the Python with-statement.

Is there an easy way to quickly add this? Perhaps on the with-block, there could be a right click to select from the methods of the with-block object.

Possible solution 2

Here is a simpler version. It is just a block, for methods.


Notice that you have to give the Object instance, and the type. The type is only needed to be able to get methods from a picklist for adding method blocks. You can do that with "type(robot)" once you have a robot, but it is more complicated as the robots are dynamically loaded in Myro. Thus, the Myro.getRobotType(name) form.

We should change the jigsaw shape to what blocks you can drop in here. These will be "instance method blocks" that can't go anywhere else. [The ones in the tell block are currently just static method blocks, used just for the mock-up].