Reflection/List methods: Difference between revisions

Content added Content deleted
m (→‎{{header|Ring}}: Remove vanity tags)
Line 531: Line 531:
bar, 3, 3, (Foo $: $x, $y, *%_)
bar, 3, 3, (Foo $: $x, $y, *%_)
baz, 2, 3, (Foo $: $x, $y?, *%_)
baz, 2, 3, (Foo $: $x, $y?, *%_)
</pre>

=={{header|Phix}}==
Phix is not object orientated, but this sort of thing is fairly easy to emulate.
<lang Phix>enum METHODS, PROPERTIES

sequence all_methods = {}

function method_visitor(object key, object /*data*/, object /*user_data*/)
all_methods = append(all_methods,key)
return 1
end function

function get_all_methods(object o)
all_methods = {}
traverse_dict(routine_id("method_visitor"),0,o[METHODS])
return all_methods
end function

--class X: Xmethods emulates a vtable
constant Xmethods = new_dict()

function exists()
return "exists"
end function

setd("exists",routine_id("exists"),Xmethods)

--class X: destructor
procedure destructor(object o)
destroy_dict(o[PROPERTIES])
end procedure
constant r_destroy = routine_id("destructor")

--class X: create new instances
function newX(object x,y)
integer Xproperties = new_dict()
setd("x",x,Xproperties)
setd("y",y,Xproperties)
object res = delete_routine({Xmethods,Xproperties},r_destroy)
return res
end function

object x = newX(2,"string")

?get_all_methods(x)</lang>
{{out}}
<pre>
{"exists"}
</pre>
</pre>