Modify ↓
Opened 21 months ago
#3938 new Feature Request
ByRef inside the same scope
Reported by: | mars(autoit.de) | Owned by: | |
---|---|---|---|
Milestone: | Component: | AutoIt | |
Version: | Severity: | None | |
Keywords: | ByRef | Cc: |
Description
; Feature Request: ; ------------------------------- ; ByRef inside the same scope ; ------------------------------- ; The Code below is just "one" example that I encountered lately. ; The description of the problem/request is inside the comments. ; The Code is only there to show the problem, it will not run. ; Example: ; You have a Geometry consisting of Vertices, Indices, Colors, etc. Func Geometry() Local $aVertex[3] = [0, 1, 2] ; xyz coordinates Local $aVertices = [$aVertex1, $aVertex2, ...] Local $aTriangle[3] = [0, 1, 2] ; Indices of 3 vertices inside $aVertices Local $aIndices = [$aTriangle1, $aTriangle2, ...] Local $aRGB = [0.5, 0.6, 0.7] ; RGB Color Local $aColors = [$aRGB1, $aRGB2, ...] ; One Color for every Triangle Local $aGeo = [$aVertices, $aIndices, $aColors] Return $aGeo EndFunc Func ProjectOnSphere(ByRef $aGeo) ; To do anything without copying, you will need to wrap things inside a function: __ProjectOnSphere($aGeo[0]) EndFunc Func __ProjectOnSphere(ByRef $aVertices) ; To do anything without copying, you will need to wrap things inside a function again: For $i = 0 To UBound($aVertices) - 1 Step 1 VertexNormalize($aVertices[$i]) Next EndFunc Func VertexNormalize(ByRef $aVertex) ; 3rd layer of functioncalls Local $l = $aVertex[0]^2 + $aVertex[1]^2 + $aVertex[2]^2 $aVertex[0] /= $l $aVertex[1] /= $l $aVertex[2] /= $l EndFunc ; The problems now are: ; - The arrays can be expensive to copy (10 vertices? ok, 1000 vertices -> not ok) so you MUST use functions to access byref functionality ; - What if you have a "Group of Geometries" (an Array of "Geometry") -> you will need more and more layers of wrapping then because you cannot access anything directly ; - The Overhead is always a tradeoff between copying something (this may be good for vertices because they are only of type Array[3]) and using another layer of functions ; - There is code duplication or many function parameters (to pass down previous calculated stuff that is needed in the lower levels) this way. ; ---------------------------------------------------------------- ; Suggestion: ByRef inside the same scope. ; ---------------------------------------------------------------- ; - Makes functions that are only used for their ByRef obsolete ; - Improves readability ; - Improves performance (the code has less overhead, so it will propably be faster) ; - Greatly improves the comfort when dealing with nested arrays ; ---------------------------------------------------------------- ; Example usage (with the above example): Func ProjectOnSphere(ByRef $aGeo) ; -> You can pass the Geometry directly -> Minus 1 Layer of calls Local ByRef $aVertices = $aGeo[0] ; Syntax suggestion 1: "Local ByRef $var1 = $var2", another variation would be "Local $var1 = ByRef $var2" Local ByRef $aVertex ; Syntax suggestion 2: "Local ByRef $var1" -> works like a function ByRef Parameter. Local $l ; normal local var For $i = 0 To UBound($aVertices) - 1 Step 1 $aVertex = $aVertices[$i] ; You can access the internals directly -> Minus 1 Layer of calls $l = $aVertex[0]^2 + $aVertex[1]^2 + $aVertex[2]^2 $aVertex[0] /= $l $aVertex[1] /= $l $aVertex[2] /= $l ; $aGeo is updated automatically without any copying or any additional function calls. Next EndFunc ; I know that the parentheses symtax can be used to access nested arrays, but it only works for "read" access. Global $a[3], $b = [$a, $a, $a] ($b[0])[0] = 1 ; this does not work ConsoleWrite(($b[0])[0] & @CRLF) ; this works ; enabling write access here would propably also help a lot, but adding ByRef is far superior
Attachments (0)
Guidelines for posting comments:
- You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
- In-depth discussions should take place on the forum.
For more information see the full version of the ticket guidelines here.
Note: See
TracTickets for help on using
tickets.