Polymorphism: Difference between revisions

→‎{{header|jq}}: def set(keyname;value)
(→‎{{header|jq}}: def set(keyname;value))
Line 2,047:
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, one could define a type-checkingpolymorphic "set_xsetter" as follows:
 
<lang jq>
# keyname should be (or evaluate to) a string
def set_x(x):
def set(keyname; value):
if type == "Pointobject" orand .type ==and "Circle"has(keyname) then .x[keyname] = xvalue
else error("set_xset: invalid type: \(.)")
end;
</lang>
Example:
2,442

edits