Break OO privacy: Difference between revisions

m (Add R)
Line 1,026:
{{out}}
<pre>42</pre>
 
=={{header|Phix}}==
Privacy operates by having a context of routine_id(<i>"name"</i>) in force between the class..end class.<br>
We can easily break that privacy mechanism via low-level routines with the required simulated/fake context,<br>
and at the same time be fairly confident that no-one is ever going to manage to achieve that by accident.
Needs 0.8.1+
<lang Phix>class test
private string msg = "this is a test"
procedure show() ?this.msg end procedure
end class
test t = new()
t.show()
--?t.msg -- illegal
--t.msg = "this is broken" -- illegal
include builtins\structs.e as structs
constant ctx = routine_id("test") -- magic/context
--constant ctx = "test" -- also works
?structs:fetch_field(t,"msg",ctx)&" (with some magic)"
structs:store_field(t,"msg","this breaks privacy",ctx)
t.show()</lang>
{{out}}
<pre>
"this is a test"
"this is a test (with some magic)"
"this breaks privacy"
</pre>
 
=={{header|PicoLisp}}==
7,813

edits