Use the hardlink analogy, not the symlink analogy, to think about objects

Helpful script writing tricks and HowTo's
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Use the hardlink analogy, not the symlink analogy, to think about objects

Post by 20170201225639 » 02 Oct 2021, 03:22

This is just my attemp to wrap my head around objects so they won't trip me up anymore. Hopefully it isn't seriously misleading!

Code: Select all

 ; __G__1 has a deeply nested innermost object we are interested in accessing, 
 ; but accessing it is inconvenient because it has a really long "path" ("__G__1.keyOuter.keyInner.keyInnerX2")
__G__1 := {keyOuter: {keyInner: {keyInnerX2: {keyInnerX3: "ha!"} }} }

; so we attempt to give that innermost object an "alias"/"shortcut"
__G__2 := __G__1.keyOuter.keyInner.keyInnerX2 

; we attemp to put the alias to use. First, we try to ALTER the innermost object  by the alias
__G__2.keyInnerX3 := "??"  
msgbox % __G__1.keyOuter.keyInner.keyInnerX2.keyInnerX3  
This produces "??", showing the attempt succeeded

Our initial success may enbolden us think perhaps we can use the alias to not just ALTER of the innermost object but also DESTROY it. Let's see if that's so.

Code: Select all

__G__1 := {keyOuter: {keyInner: {keyInnerX2: {keyInnerX3: "ha!"} }} }
__G__2 := __G__1.keyOuter.keyInner.keyInnerX2
__G__2 := ""  ; here we attemp to "DESTORY" the innermost object by the alias
msgbox % __G__1.keyOuter.keyInner.keyInnerX2.keyInnerX3   
This produces "ha!", showing the attempt failed!!


At this point, the conclusion we may be tempted to draw is, we can use the alias/shortcut to ALTER the content of the innermost object, but in order to DESTROY it, we must use the "full path" as it were. We may be tempted to use the analogy with e.g. symlinks. If you create a symlink from one file X to another Y, you can operate on X and pretend you're dealing with Y --- most of the time, but not always. That is, you can change Y by changing X, but if you were to delete X, you would not have thereby deleted Y. Rather, you'd only have deleted the link from X to Y. Let's see if this analogy works in this case..

Code: Select all

__G__1 := {keyOuter: {keyInner: {keyInnerX2: {keyInnerX3: "ha!"} }} }
__G__2 := __G__1.keyOuter.keyInner.keyInnerX2
__G__1.keyOuter.keyInner.keyInnerX2 := ""
msgbox % __G__2.keyInnerX3
This produces "ha!", showing the symlink analogy is wrong!

So how should we wrap our head around this? The better analogy is not the symlink (=softlink) analogy but the HARDLINK analogy. When you create a hardlink "from X to Y", it really is more a case of creating another reference to the same file to which "Y" is also no more than JUST a reference, so the language of "from X to Y" is totally misleading. There's no such thing as creating a hardlink from "C:\fdr1\x.txt" to "C:\fdr2\y.txt", rather you create a hardlink from "C:\fdr1\x.txt" to point to the same physical file that "C:\fdr2\y.txt" also points to. And it does nothing more than just pointing to the file -- it enjoys no special status compared to "C:\fdr1\x.txt", the physical file considered in itself cannot be said to "reside at C:\fdr1\". Deleting a hardlink is just deleting a reference to the physical file, which may or may not result in the physical file itself being deleted, depending on whether there still exist other references to it. Thus, if you have a file in C:\desktop, and you creating a hardlink in C:\Program to the file, you can go into C:\Desktop and delete that file, but if you go back to C:\Program you see the file still exists completely intact! This is why the 3rd example above turned out the way it did: because when we tried to delete the 'innermost object', we were really deleting the "original" reference to it, which usually result in the object being deleted, but not always, because we may have created another reference to it in the meantime.

neogna2
Posts: 589
Joined: 15 Sep 2016, 15:44

Re: Use the hardlink analogy, not the symlink analogy, to think about objects

Post by neogna2 » 01 Nov 2021, 07:22

I found this analogy helpful, thank you.

Post Reply

Return to “Tutorials (v1)”