Polymorphic copy: Difference between revisions
Content added Content deleted
(Added Kotlin) |
|||
Line 1,482: | Line 1,482: | ||
{{out}} |
{{out}} |
||
<pre>I'm a cool Rat+Fink!</pre> |
<pre>I'm a cool Rat+Fink!</pre> |
||
=={{header|Phix}}== |
|||
The object type is the ultimate polymorph - there is <i>nothing</i> that you can store anywhere else that you cannot store in an object. |
|||
For this demonstration (not that this really proves much), our types T and S (overkill really) are just going to contain a string name and a method (routine_id). |
|||
<lang Phix>enum NAME, METHOD |
|||
procedure me_t() |
|||
puts(1,"I is a T\n") |
|||
end procedure |
|||
constant r_t = routine_id("me_t") |
|||
procedure me_s() |
|||
puts(1,"I is an S\n") |
|||
end procedure |
|||
constant r_s = routine_id("me_s") |
|||
type T(object o) |
|||
-- as o[METHOD] can be overidden, don't verify it! |
|||
return sequence(o) and length(o)=2 and string(o[NAME]) and integer(o[METHOD]) |
|||
end type |
|||
type S(T t) |
|||
return t[METHOD] = r_s |
|||
end type |
|||
S this = {"S",r_s} |
|||
T that = {"T",r_t} |
|||
call_proc(that[METHOD],{}) |
|||
that = this |
|||
call_proc(that[METHOD],{})</lang> |
|||
{{out}} |
|||
<pre> |
|||
I is a T |
|||
I is an S |
|||
</pre> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |