API for object creation
jleeothon opened this issue · comments
Johnny Lee-Othon commented
As of now, tests do only use the cortopy.declare_child
method.
There are more cases of object creation that need to be addressed, although they will not be implemented immediately:
- create child
- declare child
- define
- create (anonymous)
- declare (anonymous)
- create object that is not a Corto object (only value, see example)
Examples
For the types:
struct Point::
x, y, z: int64
struct Line::
start, end: Point
Proposal 1
# declare child, any of two are equivalent
p = Point.declare_child(None, "p")
q = cortopy.declare_child(None, "q", Point)
# create child
p = Point.create_child(None, "p", {"x": 1, "y": 2})
q = cortopy.create_child(None, "q", Point, {"x": 1, "y", 2})
# that notation with a dictionary though, is not very nice;
# now consider keyword arguments (this requires significant more effort to implement)
p = Point.create_child(None, "p", x=1, y=2)
q = cortopy.create_child(None, "p", x=1, y=2)
# define, any of two are equivalent
p.define()
cortopy.define(q)
# declare (anonymous)
p = Point.declare()
q = cortopy.declare(Point)
# create (anonymous)
p = Point.create({"x": 1, "y": 2})
q = cortopy.create(Point, {"x": 1, "y": 2})
# create values that are not Corto objects
p = Point({"x": 1, "y": 2})
q = Line()
q.start = p
Notice, however that the shortest "syntax" is held true only for the poorest of cases, a value not held by an object in the object store.
Proposal 2
We could use a "syntax" based on the C++ binding draft here.
The intent would be to reduce the total number of functions, instead using the function arguments as discriminants for which object-creation pattern to use. Consider:
o = Point() # declare
o = Point(*, define: bool) # create child
o = Point(*, parent: cortopy.object|str, name: str) # declare child
o = Point(*, parent: cortopy.object|str, name: str, define: bool) # create child