<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dw="https://www.dreamwidth.org">
  <id>tag:dreamwidth.org,2009-04-14:61366</id>
  <title>jack</title>
  <subtitle>jack</subtitle>
  <author>
    <name>jack</name>
  </author>
  <link rel="alternate" type="text/html" href="https://jack.dreamwidth.org/"/>
  <link rel="self" type="text/xml" href="https://jack.dreamwidth.org/data/atom"/>
  <updated>2017-07-03T10:14:23Z</updated>
  <dw:journal username="jack" type="personal"/>
  <entry>
    <id>tag:dreamwidth.org,2009-04-14:61366:1037020</id>
    <link rel="alternate" type="text/html" href="https://jack.dreamwidth.org/1037020.html"/>
    <link rel="self" type="text/xml" href="https://jack.dreamwidth.org/data/atom/?itemid=1037020"/>
    <title>Pointer-to-owner: rust, java, and other languages</title>
    <published>2017-07-03T10:14:23Z</published>
    <updated>2017-07-03T10:14:23Z</updated>
    <category term="rust"/>
    <category term="java"/>
    <category term="tech"/>
    <dw:security>public</dw:security>
    <dw:reply-count>6</dw:reply-count>
    <content type="html">I previously talked about accessing the scope of an owning class from a class declared and instantiated within it. Latest post here: &lt;a href="https://jack.dreamwidth.org/1017241.html"&gt;https://jack.dreamwidth.org/1017241.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The possible approaches seem to be: the owned class has a pointer back to the owning class; or functions dealing with the owned class get an extra parameter to the owning class. Whether that's implemented manually, or automatically by the language, or somewhere between.&lt;br /&gt;&lt;br /&gt;Thinking some more about this, various things occurred to me:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Java&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;I hadn't realised, but I learned Java and C# (and maybe other recent/managed languages) do this automatically, presumably implemented by the owned class automatically having a pointer to the owning class, and checked where its instantiated that it's only instantiated by an instance of the owning class.&lt;br /&gt;&lt;br /&gt;&lt;rust&gt;&lt;br /&gt;&lt;br /&gt;I was naturally drawn to a "owning pointer is passed in alongside or as part of a this pointer" implementation as it seemed more conceptually correct. However, the actual benefit of this is a lot smaller in most languages other than rust. I first started thinking about these options in a rust example, where having a pointer to the owning class needed some fancy dancing, because rust prefers to keep tight limits on how many pointers to a class at once (ideally one only).&lt;br /&gt;&lt;br /&gt;This hopefully makes memory management safer, and means you can usually move classes around in memory using a raw memcpy, because they don't usually have internal pointers to different parts of them. But most other languages don't even try to do that, just assume that a non-trivial class is fixed in place in memory (or moved only by a garbage collector that knows where all the pointers are).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Implementation&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;If you try to avoid having a permanent pointer back to the owning class, and if you ever need a pointer to the owned class (this is common if you use it as a callback), you need to accept your pointers would actually be a pair (or more) of pointers, to the owning class, and to the owned class. The owned pointers might be an offset rather than a complete pointer. That's clunky, but wouldn't necessarily take up that much space if the language supports it. You could do a similar thing for iterators, like pointers to members of a collection, rather than having a bare pointer that only makes sense if you already know what the collection is.&lt;br /&gt;&lt;br /&gt;That seems a useful concept, but I'm not sure how useful it would be in practice.&lt;/rust&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=jack&amp;ditemid=1037020" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
</feed>
