Skip to content

Instantly share code, notes, and snippets.

@GeePawHill
Created June 28, 2017 20:19
Show Gist options
  • Save GeePawHill/ecdb0953a912809dace5d5c11ed3e843 to your computer and use it in GitHub Desktop.
Save GeePawHill/ecdb0953a912809dace5d5c11ed3e843 to your computer and use it in GitHub Desktop.
Desired code inside a thing that writes scripts:
Script writeIt()
{
Format boxFormat = new Format(...);
LabelBox box1 = new LabelBox("Box 1", new Point(400d,400d), boxFormat);
LabelBox box2 = new LabelBox("Box 2", new Point(600d,600d), boxFormat);
Arrow arrow = new Arrow(box1,false,box2,true,boxFormat):
// here's the script proper
cue(3); // wait until the clock says it's 3.
sketch(500d, box1); // sketch the box in a half-second
sketch(500d, box2);
sketch(2000d, arrow); // slowly draw the arrow
swipe(0); // move & scale everything to fit in position 0 (lower right corner);
}
old-school:
just derive every writer of scripts from class ScriptWriter which has all those cue/sketch/etc methods, and use them.
with NII meaning 'never override a method'
same as old-school
with NII meaning 'never use extends'
1) define a helper, embed it in the person who writes scripts, prepend every line with "helper."
2) define helper with all statics, use import static ...Helper.*;
@jbrains
Copy link

jbrains commented Jun 28, 2017

It looks like you want the semicolon operator in Smalltalk. The closest you have in Java is

Script myScript = new Script() {{
  cue(3); // wait until the clock says it's 3.
  sketch(500d, box1); // sketch the box in a half-second
  sketch(500d, box2);
  sketch(2000d, arrow); // slowly draw the arrow
  swipe(0); // move & scale everything to fit in position 0 (lower right corner);
}};
myScript.run(canvas); // I'm just guessing here. canvas might just be this.

Yes, this uses extends (at least in the background), but it doesn't override a concrete method and it elides the implicit receiver primitiveOperationsLibrary. It's a cheat, but I'd consider it here. It extends Script in such a way that avoids the risk of breaking a contract, since using Script this way has no contract, except for run() which runs the script.

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