Professional Documents
Culture Documents
Prototype Your Design!: Robert Griesemer
Prototype Your Design!: Robert Griesemer
Robert Griesemer
dotGo 2016, Paris
1
Getting to good software design
● Usually involves
○ Design docs
○ Feedback from reviewers
○ Iterative process
○ etc.
3
Elsewhere, design thinking requires prototyping
Try stuff
Reframe problems
Define Prototype
Test
j
Multi-dimensional slices for Go
(issue #6282)
matrix[i,j]
var matrix [,]float64
5
High-level goals
6
Observation
● Slice representation
⇨ Define an (abstract data) type
7
Key missing feature: Nice notation
m.At(i, j)
m.AtSet(i, j, x)
instead of
a[i, j] ⇨ a.At(i, j)
We can do this by hand, or automatically, via a source-to-source rewriter.
9
A prototype allows us to
explore the design space.
10
Design the prototype
● Semantics
○ x[i] means x.[](i)
○ x[i, j] means x.[](i, j)
○ x[i, j, k] = y means x.[]=(i, j, k, y)
○ x + y means x.+(y)
11
Implement the prototype
BEFORE AFTER
func (a Point) +(b Point) Point { func (a Point) ADD__(b Point) Point {
return Point{…} return Point{…}
} }
rewrite
+ CallExpr
x y
. y
14
Type-checking to the rescue
Approach:
1. Use go/types to determine operands types
2. Rewrite x + y if type of x has ADD__ method
15
Syntax tree for x + y + z after parsing
We have no type information.
z
+ z
x y
16
After type-checking
Several unknown types;
assume it’s because x + y should be x.ADD__(y). z
T? +
T? + z T[z]
T[x] x y T[y]
17
Rewrite where we can
Replace x + y with x.ADD__(y)
if type of x implements ADD__. + z
CallExpr z
T? +
T? + z T[z] . y
18
… and type-check again
Still have unknown type;
do another round. T? + z
. y T[y]
T[x] x ADD__
19
One more time: Determine what to rewrite
Replace (x.ADD__(y)) + z with
(x.ADD__(y)).ADD__(z) T? + z
. y T[y]
T[x] x ADD__
20
… rewrite CallExpr
. z
CallExpr ADD__
. y
x ADD__
21
… and type-check T[x.ADD__(y).ADD__(z)] CallExpr
. y T[y]
T[x] x ADD__
22
A concrete implementation
allows us to judge a design.
23
An implementation of a 2D “slice”
BEFORE AFTER
26
Are index operator methods good enough?
27
Conclusion
28
Plan to throw one away;
you will, anyhow.
29
Thank you!
https://github.com/griesemer/dotGo2016/
30