Break OO privacy: Difference between revisions

m
→‎{{header|Phix}}: added to Phix/Class
(Added D example)
m (→‎{{header|Phix}}: added to Phix/Class)
Line 1,056:
 
=={{header|Phix}}==
{{libheader|Phix/Class}}
Privacy operates by having a context of routine_id(<i>"nameclassname"</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 fairlyreasonably confident that no-one is ever going to manage to achieve that by accident.
Needs 0.8.1+
<lang Phix>class test
Line 1,071 ⟶ 1,072:
constant ctx = routine_id("test") -- magic/context
--constant ctx = "test" -- also works
--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>
<small>(Obviously you could inline the ctx values rather than create a separate constant.)</small>
{{out}}
<pre>
7,820

edits