Polymorphism: Difference between revisions

Content added Content deleted
(→‎{{header|jq}}: def set_x)
Line 2,031: Line 2,031:
def Point(x;y): {"type": "Point", "x": x, "y": y};
def Point(x;y): {"type": "Point", "x": x, "y": y};
def Point(x): Point(x;0);
def Point(x): Point(x;0);
def Point: Point(0);


def Circle(x;y;r): {"type": "Circle", "x": x, "y": y, "r": r};
def Circle(x;y;r): {"type": "Circle", "x": x, "y": y, "r": r};
def Circle(x;y): Circle(x;y;0);
def Circle(x;y): Circle(x;y;0);
def Circle(x): Circle(x;0);
def Circle(x): Circle(x;0);
def Circle: Circle(0);


def print:
def print:
Line 2,043: Line 2,045:
</lang>
</lang>


In practice, it's unlikely one would want to write accessors, as .x will retrieve "x", etc; similar remarks apply to setters (.x = VALUE). `.` will copy, and empty could serve as a destructor, in that `Point(0) | empty` produces the empty stream.
In practice, it's unlikely one would want to write accessors, as .x will retrieve "x", etc; similar remarks apply to setters (.x = VALUE). `.` will copy, and `empty` could serve as a kind of destructor, in that `Point(0;0) | empty` produces the empty stream.


For the sake of illustration, though, one could define a type-checking "set_x" as follows:
For the sake of illustration, one could define a type-checking "set_x" as follows:


<lang jq>
<lang jq>
Line 2,055: Line 2,057:
Example:
Example:
<lang julia>
<lang julia>
Circle(0;1;2) | print
Circle(0;1;2) | .x = 1 | print
</lang>
</lang>