Polymorphism: Difference between revisions

Content added Content deleted
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
Line 2,026: Line 2,026:
}</lang>
}</lang>


=={{header|jq}}==

<lang jq>
def Point(x;y): {"type": "Point", "x": x, "y": y};
def Point(x): Point(x;0);

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

def print:
if .type == "Circle" then "\(.type)(\(.x); \(.y); \(.r))"
elif .type == "Point" then "\(.type)(\(.x); \(.y))"
else empty
end;
</lang>

Example:
<lang julia>
Circle(0;1;2) | print
</lang>
=={{header|Julia}}==
=={{header|Julia}}==
There is no obvious inheritance hierarchy here to get polymorphism. Julia has multiple dispatch, so the appropriate implementation of the print function will be called at runtime depending on the type of the arguments provided. The import of Base.print is done to allow the print function to dispatch on some new types you define.
There is no obvious inheritance hierarchy here to get polymorphism. Julia has multiple dispatch, so the appropriate implementation of the print function will be called at runtime depending on the type of the arguments provided. The import of Base.print is done to allow the print function to dispatch on some new types you define.