Skip to content

Opaque robot type #212

Closed
Closed
@byorgey

Description

Currently, all commands which need to identify a robot (e.g. install, give, reprogram) take a string identifying the robot by name. We also store robots by name internally in the robotMap. However, this has several disadvantages: it's not very type safe (for example, are you supposed to give "base" "tree" or give "tree" "base"? And if you misspell the name of a robot, it fails at runtime instead of typechecking time), and necessitates frequent, relatively costly string comparisons.

The basic idea is to add a new type robot which is opaque to the user but is internally represented by Int. Here are my current thoughts:

  • Add a new base type robot.
  • Change the type of any commands that currently take or return the name of a robot:
    • give : robot -> string -> cmd ()
    • install : robot -> string -> cmd ()
    • reprogram : robot -> cmd a -> cmd ()
    • build : cmd a -> cmd robot
  • Note that robots can still have a name, which is displayed when they are viewed. But the name no longer needs to be unique.
  • However, note that the build command doesn't take a name any more; in many cases players are probably fine with giving it a descriptive variable name (e.g. lambda_getter <- build get_lambda), but never plan to view it so don't really care what its "display name" is.
  • However, we can add a command setname : string -> cmd () which lets a robot set its display name.
  • Internally, we can add a new type of Value, VRobot :: Int -> Value. It can pretty-print as something like r105 or <105>, it doesn't really matter that much.
  • What about robots that want to e.g. give something back to, say, the base? How can they get a reference to it? I propose adding a special variable parent : robot which is always a reference to a robot's parent (i.e. the robot that built it). So the code for building a robot to get you a thing might look like this:
    child <- build {move; thing <- grab; turn back; move; give parent thing}
    
    While we're at it we could also add a special global variable base : robot which is a reference to the base.
  • We might also want to add a way to get the results of previous commands entered at the REPL, so if you did build and forgot to bind the result to something, it's not lost forever.

Metadata

Assignees

Labels

C-Moderate EffortShould take a moderate amount of time to address.G-RobotsAn issue having to do with robots.L-Language designIssues relating to the overall design of the Swarm language.S-Nice to haveThe bug fix or feature would be nice but doesn't currently have much negative impact.Z-FeatureA new feature to be added to the game.Z-RefactoringThis issue is about restructuring the code without changing the behaviour to improve code quality.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions