Browsing this Thread:
1 Anonymous Users
|
About the undocument PASS & NULL keywords, and the barely documented OWNS !
|
||||
|---|---|---|---|---|
|
Home away from home
![]()
|
(Originally posted on 15-11-2016 to amigacoding.de)
If you are interested in understanding the code of my recently-posted cBasicList.e module, or possibly some of my other modules in PEmodules:CSH, then it may help to understand the "OWNS", "PASS" & "NULL" keywords. "OWNS" only get's a brief mention in the Standard Functionality guide, while "PASS" & "NULL" have been completely UNdocumented until now... If you see "OWNS" as part of a variable's (or object member's) type, you can safely ignore it, because that is what PortablE currently does! However, it is a hint that variable has ownership (i.e. deallocation responsibility) for that particular pointer (or array or e-string or e-list). If it's part of a local variable, then usually that variable should be ENDed when the procedure finishes. If it's part of an object member, then that member should be ENDed as part of the object's end() method. If you see "OWNS" as part of a parameter or return variable, then it's a hint that ownership (i.e. deallocation responsibility) is being passed into or out of that procedure. What about "PASS"? You cannot ignore this, because it actually does something! Here is an example: Quote: DEF var1:OWNS PTR TO example, var2:OWNS PTR TO example What this does is to pass ownership from var1 to var2. "PASS" does this by evaluating to the original value of "var1" after it has assigned NIL to var1. This ensures that the pointer is only ever owned by one variable (var1 or var2), and so you cannot get into a situation where your program tries to destroy the same object twice. Most of the time you can imagine the above code as equivalent to this: Quote: DEF var1:OWNS PTR TO example, var2:OWNS PTR TO example HOWEVER, this simple story is not quite accurate, and so here's what actually happens: Quote: DEF var1:OWNS PTR TO example, var2:OWNS PTR TO example, temp:PTR TO example When would this make a difference? If you tried to be clever & do something like this: Quote: var1.member := PASS var1 You MIGHT imagine that var1 get's stored in it's own member, before it get's NILed... but in actual fact it will lead to a crash, because var1 get's NILed before the assignment occurs, so the code would end-up trying to assign to the member of a NIL pointer! I should probably make PortablE detect & warn about these kinds of situations, at least once "PASS" becomes a properly documented part of the language. The "NULL" keyword is somewhat similar to "OWNS" in that it is used as part of a variables type, but is currently ignored by PortablE! It is only used with procedure parameters (or possibly return variables). When you see it used with a procedure parameter, it is an indication that parameter is allowed to be NIL. i.e. The procedure will function just fine if you pass it NIL or NILA (as appropriate). For example: Quote: PROC example(param1:NULL PTR TO cBasicNode)
Posted on: 2018/10/7 12:56
|
|||
|
||||
You can view topic.
You cannot start a new topic.
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.




